Concatenate unique distinct values
This article demonstrates Excel formulas that extract unique distinct values from a cell range and concatenates the values using any delimiting characters.
Unique distinct values are all values, however, duplicates are merged into one value. For example, cell range B3:B6 contains four values. Item "AA" is displayed twice.
The formula in cell D3 returns item "AA" only once.
Table of Contents
1. Concatenate unique distinct values - Excel 2019 formula
The new TEXTJOIN function in Excel 2019 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 2019 and can't use the TEXTJOIN function.
Recommended article
Recommended articles
Joining multiple cell values in Excel is not easy, for example, the CONCATENATE function allows you to only reference a […]
1.1 How to enter an array formula
- Double press with left mouse button 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 the above steps.
Recommended article
Recommended articles
Array formulas allows you to do advanced calculations not possible with regular formulas.
1.2 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
Recommended articles
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))
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 value
IF(MATCH(B3:B6, B3:B6, 0)=MATCH(ROW(B3:B6), ROW(B3:B6)), B3:B6, "")
returns {"AA";"BB";"";"CC"}
Recommended article
Recommended articles
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, ""))
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
Recommended articles
The TEXTJOIN function is a relatively new function introduced in Excel 2019, it is like the CONCATENATE function on steroids […]
2. 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.
2.1 VBA code
'Name User Defined Function (UDF) and specify parameters Function UniqConcat(rng As Range, str As String) 'Dimension variables and declare data types Dim ucoll As New Collection, Value As Variant, temp As String 'Enable error handling, this is necessary because an error is returned when a duplicate string is added to the collection variable On Error Resume Next 'Iterate through all values in range object rng For Each Value In rng 'Check if number of characters in variable Value is more than 0 (zero), if so convert value to string and then add converted value to collection variable If Len(Value) > 0 Then ucoll.Add Value, CStr(Value) 'Continue with next value Next Value 'Disable error handling On Error GoTo 0 'Iterate through values in collection variable ucoll For Each Value In ucoll 'Append value to variable temp and delimiting character temp = temp & Value & str 'Continue with next value Next Value 'Remove last character from variable temp which is a delimiting character. temp = Mid(temp, 1, Len(temp) - Len(str)) 'Return string to cell in worksheet UniqConcat = temp End Function
2.2 Where to put the code?
- Press Alt + F11 to open the VB Editor.
- Press with mouse on "Insert" on the menu.
- Press with mouse on "Module".
- Copy above code.
- Paste to code module.
- Exit VB Editor and return to Excel.
3. Concatenate unique distinct values - Excel 365
Formula in cell D3:
=TEXTJOIN(",", TRUE, UNIQUE(B3:B6))
4.1 Explaining formula in cell D3
Step 1 - Extract unique distinct values
The UNIQUE function returns unique distinct values and is a new function for Excel 365 users.
UNIQUE(B3:B6) returns {"AA";"BB";"CC"}
Step 2 - Concatenate unique distinct values
The TEXTJOIN function allows you to combine text strings from multiple cell ranges and also use delimiting characters if you want.
TEXTJOIN(delimiter, ignore_empty, text1, [text2], ...)
TEXTJOIN(",", TRUE, UNIQUE(B3:B6)) returns "AA,BB,CC" in cell D3.
4. Concatenate unique distinct values - Earlier Excel versions
Formula in cell D3:
=LOOKUP(2,1/(COUNTIF($D$2:D2,$B$3:$B$6)=0),$B$3:$B$6)
This formula is explained here. Copy cell D3 and paste to cells below as far as needed.
4.1 Concatenate values manually
Step 1 - Create a cell reference to unique distinct values
Double press with left mouse button on cell D8. Press = (equal sign) and select with mouse cell range D3:D5. Alternatively, type D3:D5.
=D3:D5
Don't press Enter yet.
Step 2 - Convert values in cell range to array values
Press F9 on your keyboard to convert cell reference D3:D5 to actual values.
={"CC";"AA";"BB"}
Step 3 - Remove curly brackets and equal sign
={"CC";"AA";"BB"} becomes "CC";"AA";"BB"
Press Enter.
Step 4 - Find and Replace
Select cell D8.
Press CTRL + H to open the "Find and Replace" dialog box.
Press with the left mouse button on tab "Replace" located on the dialog box.
Type ";" in "Find what:" field, see image below.
Type , in "Replace with:" field, see the image below.
Press with left mouse button on "Replace" button.
Press with left mouse button on "Close" button.
Step 4 - Remove double quotes
Concatenate category
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 […]
Question: Is there a way to have a unique list generated from a list? Meaning I have a sheet that […]
Excel categories
12 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.
There's a link for textjoin based on 1 criteria, and there's another link for textjoin with the condition that they're unique values, but how do I do both? For the sake of argument, my formula cell is J5. G5 has a customer ID. My formula is looking for a matching customer ID in range 'Reject Details'!$B$1:$B$500 to retrieve the corresponding part# in 'Reject Details'!$E$1:$E$500.
TEXTJOIN(", ",TRUE,IF($G5='Reject Details'!$B$1:$B$500,'Reject Details'!$E$1:$E$500,""))
The issue is that I'm trying to show what a customer buys, without getting "basket, basket, basket, basket, basket".
I have the same issue. Did you get any solution?
Thanks for the VBA code. It solved my problem
Thank you for VBA code!!! Greetings from Lithuania! ;)