How to concatenate cells if same value exists in another column in Excel?
As the left screenshot shown, you need to concatenate the second column cells if the first column values are same. How can you achieve it?
In this article, we will introduce three methods to achieve it.
Concatenate cells if same value with formulas and filter
Excel Productivity Tools
Office Tab: Bring powerful tabs to Office (include Excel), just like Chrome, Safari, Firefox and Internet Explorer. Save you half the time, and reduce thousands of mouse clicks for you. 30-day Unlimited Free Trial
Kutools for Excel: Save 70% of your time and solve 80% Excel problems for you. 300+ advanced features designed for 1500+ work scenario, make Excel much easy and increase productivity immediately. 60-day Unlimited Free Trial
With the below formulas, you can concatenate corresponding cell contents if another column contains same value in Excel.
1. Select a blank cell besides the second column (here we select cell C2), enter formula =IF(A2<>A1,B2,C1 & "," & B2) into the formula bar, and then press the Enter key.
2. Then select cell C2, and drag the Fill Handle down to cells you need to concatenate.
3. Enter formula =IF(A2<>A3,CONCATENATE(A2,",""",C2,""""),"") into cell D2, and drag Fill Handle down to the rest cells.
4. Select cell D1, and click Data > Filter. See screenshot:
5. Click the drop-down arrow in cell D1, uncheck the (Blanks) box, and then click the OK button.
You can see the cells are concatenated if the first column values are same.
Note: To use the above formulas successfully, the same values in column A must be continuous.
Besides formula, in this section, we will show you how to use VBA code to concatenate cells if same value.
1. Press Alt + F11 keys to open the Microsoft Visual Basic Applications window.
2. In the Microsoft Visual Basic Applications window, click Insert > Module. Then copy and paste below code into the Module window.
VBA code: concatenate cells if same values
Sub ConcatenateCellsIfSameValues() Dim xCol As New Collection Dim xSrc As Variant Dim xRes() As Variant Dim I As Long Dim J As Long Dim xRg As Range xSrc = Range("A1", Cells(Rows.Count, "A").End(xlUp)).Resize(, 2) Set xRg = Range("D1") On Error Resume Next For I = 2 To UBound(xSrc) xCol.Add xSrc(I, 1), TypeName(xSrc(I, 1)) & CStr(xSrc(I, 1)) Next I On Error GoTo 0 ReDim xRes(1 To xCol.Count + 1, 1 To 2) xRes(1, 1) = "No" xRes(1, 2) = "Combined Color" For I = 1 To xCol.Count xRes(I + 1, 1) = xCol(I) For J = 2 To UBound(xSrc) If xSrc(J, 1) = xRes(I + 1, 1) Then xRes(I + 1, 2) = xRes(I + 1, 2) & ", " & xSrc(J, 2) End If Next J xRes(I + 1, 2) = Mid(xRes(I + 1, 2), 2) Next I Set xRg = xRg.Resize(UBound(xRes, 1), UBound(xRes, 2)) xRg.NumberFormat = "@" xRg = xRes xRg.EntireColumn.AutoFit End Sub
1. D1 in line Set xRg = Range("D1") means that the result will be placed in cell D1.
2. No and Combined Color in line xRes(1, 1) = "No" and xRes(1, 2) = "Combined Color" are the headers of concatenated columns. You can change them as you need.
3. Press the F5 key to run the code, then you will get the concatenated results in specified range.
If the above two methods are complicated for you, here we will show you the Advanced Combine Rows utility of Kutools for Excel. You can easily concatenate cells by comma, semicolon and so on if same values exist in another column.
1. Select the range with cells you need to concatenate, and then click Kutools > Content > Advanced Combine Rows. See screenshot:
2. In the Combine Rows Based on Column dialog box, you need to do as follows.
A. Select the column with same value you want to concatenate cells according to, and then click the Primary Key button.
B. Select the column you want to concatenate, click Combine button, and then specify a separator from the context menu (here we select Comma).
C. Click the OK button. See screenshot:
Now the cells are concatenated according to the primary key column.
Kutools for Excel includes more than 300 handy Excel tools. Free to try with no limitation in 60 days. Download the free trial now!
Excel Productivity Tools
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
To post as a guest, your comment is unpublished.· 7 months agoI use this VBA for lots of my spreadsheets and its great. But the spreadsheets have become very large 50k+ rows and it doesnt seem to be working any more. If I use it on 1000 rows it works fine but large sets of data it cant seem to cope with. No errors just no results. Any help would be appreciated.
To post as a guest, your comment is unpublished.· 8 months agoUsing the VBA macro and getting great results, I have tried tweaking it slightly for my needs but cant get it to work so I hope you can help.
Which bit do I change to make it concat a specific column, not the one directly to the right of the xSrc = Range?
Thanks for your great work!
To post as a guest, your comment is unpublished.· 8 months agoOr as a better option, if you had 3 columns instead of 2 and found duplicates in column A (like your example) can you concat column B into a cell and column C into a seperate cell? So if you had columns of Number, Colour, Age, could you concat colour and age into different columns upon finding duplicates in Number? Hope that makes sense!
To post as a guest, your comment is unpublished.· 11 months agoThis is exactly what I needed...thank you
To post as a guest, your comment is unpublished.· 1 years agoI am so thankful to you for these formulas. Fantastic work!
To post as a guest, your comment is unpublished.· 1 years agoThe concatenate feature does not work. Combines all the rows into one cell despite different keys.