Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to concatenate unique values in Excel?

If I have a long list of values which populated with some duplicate data, now, I want to find only the unique values and then concatenate them into a single cell. How could I deal with this problem quickly and easily in Excel?

Find unique values in a column and concatenate them into one cell with VBA code

Find unique values in a column and concatenate them into one cell with Kutools for Excel

List unique values and concatenate corresponding values with VBA code

List unique values and concatenate corresponding values with Kutools for Excel


arrow blue right bubble Find unique values in a column and concatenate them into one cell with VBA code


The following User Defined Function can help you to concatenate only the unique values into a single cell from a list, please do as follows:

1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste the following code in the Module Window.

VBA code: Concatenate unique values into a single cell:

Function ConcatUniq(xRg As Range, xChar As String) As String
'updateby Extendoffice 20151228
    Dim xCell As Range
    Dim xDic As Object
    Set xDic = CreateObject("Scripting.Dictionary")
    For Each xCell In xRg
        xDic(xCell.Value) = Empty
    Next
    ConcatUniq = Join$(xDic.Keys, xChar)
    Set xDic = Nothing
End Function

3. Then go back to your worksheet, and enter this formula: =ConcatUniq (A1:A17,",") into a blank cell where you want to output the concatenated result, and press Enter key to get the unique concatenated result, see screenshot:

doc concatenate uniques 1

Note: In the above formula, A1:A17 is the cells range that you want to concatenate, and the comma “,” is the separator you need to separate the combined values, you can change them to your need.


arrow blue right bubble Find unique values in a column and concatenate them into one cell with Kutools for Excel

If you are not familiar with the VBA code, Kutools for Excel also can help you, you can combine the Select Duplicate & Unique Cells and Combine utilities to achieve the result you need.

Kutools for Excel : with more than 300 handy Excel add-ins, free to try with no limitation in 60 days.

After installing Kutools for Excel, please do as follows:( Free Download Kutools for Excel Now! )

1. Select the list of cells that you want to combine only the unique values.

2. Click Kutools > Select > Select Duplicate & Unique Cells, see screenshot:

doc concatenate uniques 2

3. In the Select Duplicate & Unique Cells dialog box, select All unique(Including 1 st duplicates) option under the Rule section, see screenshot:

Tips: If you want to combine the unique values excluding first appearing duplicate, check Unique values only option.

doc concatenate uniques 3

4. Then click OK button, and only the unique values have been selected, and then press Ctrl + C to copy them, and paste them into a specific location by pressing Ctrl + V, see screenshot:

doc concatenate uniques 4

5. Select the pasted unique cells, and then click Kutools > Combine, see screenshot:

doc concatenate uniques 5

6. In the Combine Columns or Rows dialog box, choose Combine into single cell under the To combine selected cells according to following options, and then specify a separator for your combined data, see screenshot:

doc concatenate uniques 6

7. Then click Ok or Apply button, the extracted unique values have been concatenated into one single cell as following screenshot shown:

doc concatenate uniques 7

Free Download Kutools for Excel Now!


arrow blue right bubble List unique values and concatenate corresponding values with VBA code

In another case, you have a range of data which includes two columns, and now, you want to list only the unique values and concatenate their corresponding values of another column to get the following result, do you have any good ways to finish this job in Excel?

doc concatenate uniques 8

Here, you can apply the following VBA code to deal with this job, please do as follows:

1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste the following code in the Module Window.

VBA code: List unique values and concatenate corresponding data

Sub test()
'updateby Extendoffice 20151228
    Dim xRg As Range
    Dim xArr As Variant
    Dim xCell As Range
    Dim xTxt As String
    Dim I As Long
    Dim xDic As Object
    On Error Resume Next
    xTxt = ActiveWindow.RangeSelection.Address
    Set xRg = Application.InputBox("Please select the data range", "Kutools for Excel", xTxt, , , , , 8)
    Set xRg = Application.Intersect(xRg, xRg.Worksheet.UsedRange)
    If xRg Is Nothing Then Exit Sub
    If xRg.Areas.Count > 1 Then
        MsgBox "Does not support multiple selections", , "Kutools for Excel"
        Exit Sub
    End If
    If xRg.Columns.Count <> 2 Then
        MsgBox "There must be only two columns in the selected range", , "Kutools for Excel"
        Exit Sub
    End If
    xArr = xRg
    Set xDic = CreateObject("Scripting.Dictionary")
    xDic.CompareMode = 1
    For I = 1 To UBound(xArr)
        If Not xDic.Exists(xArr(I, 1)) Then
            xDic.Item(xArr(I, 1)) = xDic.Count + 1
            xArr(xDic.Count, 1) = xArr(I, 1)
            xArr(xDic.Count, 2) = xArr(I, 2)
        Else
            xArr(xDic.Item(xArr(I, 1)), 2) = xArr(xDic.Item(xArr(I, 1)), 2) & "," & xArr(I, 2)
        End If
    Next
    Sheets.Add.Cells(1).Resize(xDic.Count, 2).Value = xArr
End Sub

3. Then press F5 key to run this code, and a prompt box is popped out to remind you select the data range that you want to concatenate based on the unique values, see screenshot:

doc concatenate uniques 9

4. Click OK, the unique values have been extracted and their corresponding values in another column has been concatenated together into a new worksheet.

Note: In the above code, you can change the red comma xArr(xDic.Item(xArr(I, 1)), 2) = xArr(xDic.Item(xArr(I, 1)), 2) & "," & xArr(I, 2) in this script to any other delimiters that you want to separate the combined data.


arrow blue right bubble List unique values and concatenate corresponding values with Kutools for Excel

If you have Kutools for Excel, with its Advanced Combine Rows feature, you can solve this problem quickly and easily without any code.

After installing Kutools for Excel, please do as follows:( Free Download Kutools for Excel Now! )

1. First, you should make a copy of your original data if you want to keep the original data. And then select the data range which you want to concatenate based on the unique values.

2. Then click Kutools > Content > Advanced Combine Rows, see screenshot:

doc concatenate uniques 10

3. In the Advanced Combine Rows dialog box, click the column name that you want to list only the unique values, and click Primary Key, see screenshot:

doc concatenate uniques 11

4. Then click another column name that you want to concatenate, and click Combine, and choose one delimiter for separating your combined data, see screenshot:

doc concatenate uniques 12

5. And then click Ok button, the values have been concatenated based on the unique values of another column as following screenshot shown:

doc concatenate uniques 13

Free Download Kutools for Excel Now!


Recommended Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 71%, and Help You To Stand Out From Crowd!

Would you like to complete your daily work quickly and perfectly? Kutools For Excel brings 300+ cool and powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) for 1500+ work scenarios, helps you solve 82% Excel problems.

  •  Deal with all complicated tasks in seconds, help to enhance your work ability, get success from the fierce competition, and never worry about being fired.
  •  Save a lot of work time, leave much time for you to love and care the family and enjoy a comfortable life now.
  •  Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands, and give you a healthy body.
  •  Become an Excel expert in 3 minutes, and get admiring glance from your colleagues or friends.
  •  No longer need to remember any painful formulas and VBA codes, have a relaxing and pleasant mind, give you a thrill you've never had before.
  •  Spend only $39, but worth than $4000 training of others. Being used by 110,000 elites and 300+ well-known companies.
  •  60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  •  Change the way you work now, and give you a better life immediately!

Office Tab Brings Efficient And Handy Tabs to Office (include Excel), Just Like Chrome, Firefox, And New IE

  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Open and create documents in new tabs of same window, rather than in new windows.
  • Help you work faster and easily stand out from the crowd! One second to switch between dozens of open documents!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    MSBrown · 2 years ago
    I was successfully using the VBA function but something has gone wrong with it - I am only having a #VALUE! error returned now in places where it was working previously. I have combed my data for errors and tried removing and re-creating the VBA module, and renaming the function, nothing seems to be working. Any help would be appreciated, thanks!
  • To post as a guest, your comment is unpublished.
    Marek · 2 years ago
    Thank you! Great and simple function works perfect for me!

    https://www.extendoffice.com/documents/excel/3418-excel-concatenate-unique-values.html#a1