## Concatenate unique distinct values

The new TEXTJOIN function in excel 2016 lets you do some amazing things with arrays. This post demonstrates how to extract unique distinct values from a single column and concatenate the result into one cell.

The following picture shows you 4 values in column B. Value "AA" has a duplicate in cell B5. Unique distinct values are all values except duplicates.

Array formula in cell D3:

**Tip!** You can use the UDF I made if you don't have Excel 2016 and the TEXTJOIN function.

Recommended article

Joining multiple cell values in Excel is not easy, for example, the CONCATENATE function allows you to only reference a [โฆ]

#### How to enter an array formula

- Double click on cell D3
- Copy/Paste above formula to cell D3
- Press and hold CTRL + SHIFT
- Press Enter once
- Release all keys

Your formula has now a beginning and ending curly bracket, like this:

Don't enter these characters yourself, they appear automatically if you followed above steps.

Recommended article

Array formulas allows you to do advanced calculations not possible with regular formulas.

#### Explaining array formula in cell D3

**Step 1 - Match values with themselves**

MATCH(B3:B6, B3:B6, 0)

becomes

MATCH({"AA"; "BB"; "AA"; "CC"}, {"AA"; "BB"; "AA"; "CC"},0)

and returns {1;2;1;4}

The MATCH function returns the relative position of a value in an array. It only returns the position of the first found value.

Value "AA" is found on the first position, so the first value in the array is 1. {**1**;2;1;4}

Value "BB" is found on the second position, so the second value in the array is 2. {1;**2**;1;4}

Value "AA" is found on the first position, so the third value in the array is 1. {1;2;**1**;4}

Value "CC" is found on the fourth position, so the fourth value in the array is 4. {1;2;1;**4**}

Recommended article

Identify the position of a value in an array.

**Step 2 - Compare array with relative positions**

MATCH(B3:B6, B3:B6, 0)=MATCH(ROW(B3:B6), ROW(B3:B6))

becomes

{1;2;1;4}={1;2;3;4}

and returns {TRUE;TRUE;FALSE;TRUE}

This array tells us if a value is a duplicate or not. FALSE indicates a duplicate and we don't want that in the final result.

**Step 3 - Filter unique distinct values**

IF(MATCH(B3:B6, B3:B6, 0)=MATCH(ROW(B3:B6), ROW(B3:B6)), B3:B6, "")

becomes

IF({TRUE;TRUE;FALSE;TRUE}, B3:B6, "")

becomes

IF({TRUE;TRUE;FALSE;TRUE}, {"AA";"BB";"AA";"CC"}, "")

and returns {"AA";"BB";"";"CC"}

Recommended article

Checks if a logical expression is met. Returns a specific value if TRUE and another specific value if FALSE.

**Step 4 - Concatenate values**

TEXTJOIN(", ", TRUE, IF(MATCH(B3:B6, B3:B6, 0)=MATCH(ROW(B3:B6), ROW(B3:B6)), B3:B6, ""))

becomes

TEXTJOIN(", ", TRUE, {"AA";"BB";"";"CC"})

and returns AA,BB,CC.

The first argument in the TEXTJOIN function lets you specify the delimiting character. The second argument if you want to ignore empty strings. The third argument is the values you want to concatenate.

Recommended article

How to use the TEXTJOIN function

The TEXTJOIN function is a new function for office 365 subscribers, it is like the CONCATENATE function on steroids or what the CONCATENATE function [โฆ]

How to use the TEXTJOIN function

#### Functions in this article

Make sure you check out these links if you want to know more about the excel functions in the array formula above: TEXTJOIN, MATCH, IF, ROW

#### Download excel *.xlsx file

Concatenate unique distinct values.xlsx

### Concatenate unique distinct values (VBA)

The formula in cell D3 contains a User defined Function that extracts unique distinct values concatenated, you have the option to specify the delimiting character in the second argument.

The formula is a regular formula, however, you need to copy the code below and paste it to a code module in your workbook before you use it.

#### VBA code

Function UniqConcat(rng As Range, str As String) Dim ucoll As New Collection, Value As Variant, temp As String On Error Resume Next For Each Value In rng If Len(Value) > 0 Then ucoll.Add Value, CStr(Value) Next Value On Error GoTo 0 For Each Value In ucoll temp = temp & Value & str Next Value temp = Mid(temp, 1, Len(temp) - Len(str)) UniqConcat = temp End Function

#### Where to put the code?

- Press Alt + F11 to open the VB Editor.
- Click on "Insert" on the menu.
- Click on "Module".
- Copy above code.
- Paste to code module.
- Exit VB Editor and return to Excel.

### Download Excel file

unique-distinct-values-concatenated-to-one-cell-with-a-delimiting-character.xlsm

Use a drop down list to filter and concatenate unique distinct values

Question: Is there a way to have a unique list generated from a list? Meaning I have a sheet that [โฆ]

Concatenate cell values based on a condition [No VBA]

Add cell values to a single cell with a condition, no VBA in this article.

Lookup and return multiple values concatenated into one cell

This article demonstrates how to find a value in a column and concatenate corresponding values on the same row. The [โฆ]

Joining multiple cell values in Excel is not easy, for example, the CONCATENATE function allows you to only reference a [โฆ]

How to use the TEXTJOIN function

The TEXTJOIN function is a new function for office 365 subscribers, it is like the CONCATENATE function on steroids or what the CONCATENATE function [โฆ]

### 8 Responses to โConcatenate unique distinct valuesโ

### Leave a Reply

### How to comment

**How to add a formula to your comment**

<code>Insert your formula here.</code>

**Convert less than and larger than signs**

Use html character entities instead of less than and larger than signs.

< becomes < and > becomes >

**How to add VBA code to your comment**

[vb 1="vbnet" language=","]

Put your VBA code here.

[/vb]

**How to add a picture to your comment:**

Upload picture to postimage.org or imgur

Paste image link to your comment.

**Contact Oscar**

You can contact me through this contact form

https://www.youtube.com/watch?v=QJ2O07EB80Q&feature=youtu.be

Great video!

This is great. Is there a way to combine it with vlookup, please?

Gustavo,

Concatenate cell values based on a condition [No VBA]

Hello, nice, but can you try your formula with with 10000 records? It is working with 1000 but failing with 10000 as it is not removing the empty cells any more...

Tamas

Weird, you probably need a UDF.

If you are looking to remove the blanks, change the formula to the following:

=TEXTJOIN(", ", TRUE, IF(E8:E38="", "", IF(MATCH(E8:E38, E8:E38, 0)=MATCH(ROW(E8:E38), ROW(E8:E38)), E8:E38, "")))

I was doing this for range E8 to E38, should work for any range though with any number of blanks.

How can I Lookup and return unique distinct values concatenated into one cell. I prefer to have the VBA code as most users may not have latest office.

I also want it to update if a new data in eneterd in the source.