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 cells if same value exists in another column in Excel?

combine if same

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

Concatenate cells if same value with VBA code

Easily concatenate cells if same value with Kutools for Excel


Advanced Combine Rows: easily combine cells if same value exists in another column

With Kutools for Excel's Advanced Combine Rows utility, you can easily combine cells in a column if same value exsits in another columns in Excel. See screenshot:

contatinate of same

Kutools for Excel: with more than 200 handy Excel add-ins, free to try with no limitation in 60 days. Download and free trial Now!

Office Tab Enable Tabbed Editing and Browsing in Office, and Make Your Work Much Easier...
Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%
  • Reuse Anything: Add the most used or complex formulas, charts and anything else to your favorites, and quickly reuse them in the future.
  • More than 20 text features: Extract Number from Text String; Extract or Remove Part of Texts; Convert Numbers and Currencies to English Words.
  • Merge Tools: Multiple Workbooks and Sheets into One; Merge Multiple Cells/Rows/Columns Without Losing Data; Merge Duplicate Rows and Sum.
  • Split Tools: Split Data into Multiple Sheets Based on Value; One Workbook to Multiple Excel, PDF or CSV Files; One Column to Multiple Columns.
  • Paste Skipping Hidden/Filtered Rows; Count And Sum by Background Color; Send Personalized Emails to Multiple Recipients in Bulk.
  • Super Filter: Create advanced filter schemes and apply to any sheets; Sort by week, day, frequency and more; Filter by bold, formulas, comment...
  • More than 300 powerful features; Works with Office 2007-2019 and 365; Supports all languages; Easy deploying in your enterprise or organization.

Concatenate cells if same value with formulas and filter


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.


Concatenate cells if same value with VBA code

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

Notes:

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.


Easily concatenate cells if same value with Kutools for Excel (several clicks)

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.

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

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.


Concatenate multiple cell values and add space or other delimiters between words:

With Kutools for Excel's Combine utility, you can quickly combine multiple rows, columns or cells into a column, row or a cell with a specific separator as you need in Excel. See screenshot:

contatinate of same

Kutools for Excel: with more than 200 handy Excel add-ins, free to try with no limitation in 60 days. Download and free trial Now!


Easily concatenate cells if same value with Kutools for Excel

Kutools for Excel includes more than 300 handy Excel tools. Free to try with no limitation in 60 days. Download the free trial now!


Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial.
kte tab 201905

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!
officetab bottom
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.
    Jade · 4 months ago
    Hi, first of all thanks for creating this resource. I have been trying to figure this out for a couple of hours and I'm stuck. I'm using your 'concatenate cells if same value' but my script is looking at column "D" instead of "A. I can't figure out how to get it to use a different column for the data besides the one right next to it. In my cases I want it to look at column "D" to see if the value is the same and if so, it will grab the data from column "H" and put that data from column "H' into a cell in column "J". How do I switch this to use column "H" for the data? Thx


    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("D1", Cells(Rows.Count, "D").End(xlUp)).Resize(, 2)
    Set xRg = Range("J1")
    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) = "Products"
    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) & vbCrLf & 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
  • To post as a guest, your comment is unpublished.
    Harry Vyvey · 6 months ago
    Hi,

    looks like 2 of your formulas are wrong :

    =IF(A2<>A3,CONCATENATE(A2,",""",C2,""""),""). You need to change "A2" to "D1". As you'll want to add the string to the previous cell.

    same goes for this formula :

    =IF(A2<>A1,B2,C1 & "," & B2) : Change C1 to D1.


    kind regards


    Harry
  • To post as a guest, your comment is unpublished.
    James · 1 years ago
    I 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.
      crystal · 1 years ago
      Hi James,
      I tested the code as you mentioned, but it still works well in my case even I set the rows to 1000+.
  • To post as a guest, your comment is unpublished.
    Matt Carrington · 1 years ago
    Using 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.
      Matt Carrington · 1 years ago
      Or 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.
    Ram · 1 years ago
    This is exactly what I needed...thank you
  • To post as a guest, your comment is unpublished.
    RS · 1 years ago
    I am so thankful to you for these formulas. Fantastic work!
  • To post as a guest, your comment is unpublished.
    Marissa W · 2 years ago
    The concatenate feature does not work. Combines all the rows into one cell despite different keys.
    • To post as a guest, your comment is unpublished.
      crystal · 2 years ago
      Dear Marissa,
      Sorry I am not sure I got your question. There are three methods in the article, which concatenate feature do you mean?
  • To post as a guest, your comment is unpublished.
    Geoffrey saya · 2 years ago
    How can i merge collums with the same names eg Column one reads Kenya throughout and collumn 2 reads western, Nyanza( appearing more than ten times) and column 3 three names of districts. I want to get how mang districts do we have in every province but keep the columns.
    • To post as a guest, your comment is unpublished.
      crystal · 2 years ago
      Dear Geoffrey,

      Please try the Advanced Combine Rows utility we mentioned in above method two. If you want to keep the columns, please make a copy of the original range and do everything in the copied range.

      Specify the province column as Primary Key, set the Column 1 as Keep 1st, and then set the district column as Calculate > Count.

      After that, you will get how many district you have in every province.
  • To post as a guest, your comment is unpublished.
    cathy postmus · 3 years ago
    Can't seem to concatenate with more than 2 cells using formulas and filters