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
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
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:
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.
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 30 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:
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.
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:
5. Select the pasted unique cells, and then click Kutools > Merge & Split > Combine Rows, Columns or Cells without Losing Data, see screenshot:
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:
7. Then click Ok or Apply button, the extracted unique values have been concatenated into one single cell as following screenshot shown:
Free Download Kutools for Excel Now!
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?
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
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:
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.
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 > Merge & Split > Advanced Combine Rows, see screenshot:
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:
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:
5. And then click Ok button, the values have been concatenated based on the unique values of another column as following screenshot shown:
Free Download Kutools for Excel Now!
Best Office Productivity Tools
Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time. Click Here to Get The Feature You Need The Most...
Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier
- Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
- Open and create multiple documents in new tabs of the same window, rather than in new windows.
- Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
