Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

How to concatenate cells if same value exists in another column in Excel?

1/7/22, 12:09

Note: The other languages of the website are Google-translated. Back to English
(https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-same-
value.html)

Log in Register (/register.html)

! ExtendOffice (https://www.extendoffice.com)
Products Download (/download.html) Purchase
Resources Support (/support.html) Languages
!

Excel Tips (/documents/excel.html) Excel Functions (/excel/functions.html)

Excel Formulas (/excel/formulas.html) Excel Charts (/excel/excel-charts.html)

Word Tips (/documents/word.html) Outlook Tips (/documents/outlook.html)

How To Concatenate Cells If Same Value Exists In


Another Column In Excel?

https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-same-value.html Page 1 of 21
How to concatenate cells if same value exists in another column in Excel? 1/7/22, 12:09

As the left screenshot shown, suppsing you need


to concatenate the cells in the second column
based on the same value cells in the first column.
How can you achieve it?

In this article, we will introduce three methods to


hel you 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 "

! 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.

https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-same-value.html Page 2 of 21
How to concatenate cells if same value exists in another column in Excel? 1/7/22, 12:09

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.

https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-same-value.html Page 3 of 21
How to concatenate cells if same value exists in another column in Excel? 1/7/22, 12:09

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.

Easily combine cells in a column if same value exists in another column:

With Kutools for Excel's Advanced Combine Rows (/product/kutools-for-


excel/excel-combine-duplicate-rows.html) utility, you can easily combine cells in
a column if same value exsits in another columns in Excel as the below demo

https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-same-value.html Page 4 of 21
How to concatenate cells if same value exists in another column in Excel? 1/7/22, 12:09

shown. Download Kutools for Excel now! ( 30-day free trail)


(/download/kutools-for-excel.html)

! 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

https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-same-value.html Page 5 of 21
How to concatenate cells if same value exists in another column in Excel? 1/7/22, 12:09

1 Sub ConcatenateCellsIfSameValues()
2 Dim xCol As New Collection
3 Dim xSrc As Variant
4 Dim xRes() As Variant
5 Dim I As Long
6 Dim J As Long
7 Dim xRg As Range
8 xSrc = Range("A1", Cells(Rows.Count, "A").End(xlUp)).Resiz
9 Set xRg = Range("D1")
10 On Error Resume Next
11 For I = 2 To UBound(xSrc)
12 xCol.Add xSrc(I, 1), TypeName(xSrc(I, 1)) & CStr(xSrc(I,
13 Next I
14 On Error GoTo 0
15 ReDim xRes(1 To xCol.Count + 1, 1 To 2)
16 xRes(1, 1) = "No"
17 xRes(1, 2) = "Combined Color"
18 For I = 1 To xCol.Count
19 xRes(I + 1, 1) = xCol(I)
20 For J = 2 To UBound(xSrc)
21 If xSrc(J, 1) = xRes(I + 1, 1) Then

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.

https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-same-value.html Page 6 of 21
How to concatenate cells if same value exists in another column in Excel? 1/7/22, 12:09

! 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.

Before applying Kutools for Excel, please download and install it firstly
(/download/kutools-for-excel.html).

1. Select the range with cells you need to concatenate, and then click Kutools >
Merge & Split > Advanced Combine Rows. See screenshot:

2. In the Advanced Combine Rows dialog box, you need to do as follows.

https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-same-value.html Page 7 of 21
How to concatenate cells if same value exists in another column in Excel? 1/7/22, 12:09

2.1) Select the column with same value you want to concatenate cells based on,
and then click the Primary Key button.
2.2) Select the column you want to concatenate the cells, click Combine button,
and then specify a separator from the context menu (here I specify Comma).
2.3) Click the OK button.

Now the cells are concatenated according to the primary key column.

  If you want to have a free trial ( 30-day) of this utility, please click to
download it (/download/kutools-for-excel.html), and then go to apply the
operation according above steps.

https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-same-value.html Page 8 of 21
How to concatenate cells if same value exists in another column in Excel? 1/7/22, 12:09

! Easily Concatenate Cells If Same Value With Kutools For Excel

How to concatenate cells if same value exists in another c…


ExtendOffice - Professional Office Add-ins Follow · Share

Facebook Watch

The Best Office Productivity Tools

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

https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-same-value.html Page 9 of 21
How to concatenate cells if same value exists in another column in Excel? 1/7/22, 12:09

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. 60-day money back guarantee.

(/download/kutools-for-excel.html)

Read More... (/product/kutools-for-excel.html) Free


Download... (/download/kutools-for-excel.html) Purchase...
(/order/kutools-for-excel.html)

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!

(/download/office-tab.html)

Read More... (/product/office-tab.html) Free Download...


(/download/office-tab.html) Purchase... (/order/office-
tab.html)

Oldest First #

Comments (17) !  !  !  !  ! No ratings yet. Be the first to rate!

https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-same-value.html Page 10 of 21
How to concatenate cells if same value exists in another column in Excel? 1/7/22, 12:09

cathy postmus
about 6 years ago
(https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-same-
· #16504
value.html#comment-16504,0T

Can't seem to concatenate with more than 2 cells using formulas and
filters

Reply

Geoffrey saya
about 4 years ago
(https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-
· #20935
same-value.html#comment-20935,0T

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.

Reply

https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-same-value.html Page 11 of 21
How to concatenate cells if same value exists in another column in Excel? 1/7/22, 12:09

(/forum/profile/4614b
crystal (/forum/profile/4614-crystal.html)  $  Geoffrey saya
crystal.html)
about 4 years ago
(https://www.extendoffice.com/documents/excel/3153-excel-concatenate-
· #20936
if-same-value.html#comment-20936,20935T

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.

Reply · Report

Marissa W
about 4 years ago
(https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-same-
· #21720
value.html#comment-21720,0T

The concatenate feature does not work. Combines all the rows into
one cell despite different keys.

Reply

(/forum/profile/4614b
crystal (/forum/profile/4614-crystal.html)  $  Marissa W
crystal.html)
about 4 years ago
(https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-
· #21721
same-value.html#comment-21721,21720T

Dear Marissa,
Sorry I am not sure I got your question. There are three methods
in the article, which concatenate feature do you mean?

Reply · Report

https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-same-value.html Page 12 of 21
How to concatenate cells if same value exists in another column in Excel? 1/7/22, 12:09

RS
about 4 years ago
(https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-same-
· #23817
value.html#comment-23817,0T

I am so thankful to you for these formulas. Fantastic work!

Reply

Ram
about 4 years ago
(https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-
· #25283
same-value.html#comment-25283,0T

This is exactly what I needed...thank you

Reply

(/forum/profile/12212b
Matt Carrington (/forum/profile/12212-travelermatt.html)
travelermatt.html)
about 3 years ago
(https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-same-
· #26110
value.html#comment-26110,0T

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 f Range?

Thanks for your great work!

Reply · Report

https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-same-value.html Page 13 of 21
How to concatenate cells if same value exists in another column in Excel? 1/7/22, 12:09

(/forum/profile/12212b
Matt Carrington (/forum/profile/12212-travelermatt.html)  $ 
travelermatt.html)
Matt Carrington (/forum/profile/12212-travelermatt.html)
about 3 years ago
(https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-
· #26111
same-value.html#comment-26111,26110T

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!

Reply · Report

James
about 3 years ago
(https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-
· #26698
same-value.html#comment-26698,0T

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.

Reply

(/forum/profile/4614b
crystal (/forum/profile/4614-crystal.html)  $  James
crystal.html)
about 3 years ago
(https://www.extendoffice.com/documents/excel/3153-excel-concatenate-
· #26699
if-same-value.html#comment-26699,26698T

Hi James,
I tested the code as you mentioned, but it still works well in my
case even I set the rows to 1000l.

Reply · Report

https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-same-value.html Page 14 of 21
How to concatenate cells if same value exists in another column in Excel? 1/7/22, 12:09

(/forum/profile/18549b
Harry Vyvey (/forum/profile/18549-harryvyvey.html)
harryvyvey.html)
about 3 years ago
(https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-
· #29669
same-value.html#comment-29669,0T

Hi,

looks like 2 of your formulas are wrong :

fIFmA2n\A3,CONCATENATEmA2,",""",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 :

fIFmA2n\A1,B2,C1 & "," & B2T s Change C1 to D1.

kind regards

Harry

Reply · Report

(/forum/profile/18954b
Jade (/forum/profile/18954-jadee.html)
jadee.html)
about 2 years ago
(https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-
· #30225
same-value.html#comment-30225,0T

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

https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-same-value.html Page 15 of 21
How to concatenate cells if same value exists in another column in Excel? 1/7/22, 12:09

Dim I As Long
Dim J As Long
Dim xRg As Range
xSrc f Range("D1", Cells(Rows.Count, "D").End(xlUp)).Resize(, 2)
Set xRg f Range("J1")
On Error Resume Next
For I f 2 To UBound(xSrc)
xCol.Add xSrc(I, 1), TypeName(xSrc(I, 1)T & CStr(xSrc(I, 1))
Next I
On Error GoTo 0
ReDim xRes(1 To xCol.Count l 1, 1 To 2T
xRes(1, 1) = "No"
xRes(1, 2) = "Products"
For I f 1 To xCol.Count
xRes(I l 1, 1T f xCol(I)
For J f 2 To UBound(xSrc)
If xSrc(J, 1) = xRes(I l 1, 1T Then
xRes(I l 1, 2T f xRes(I l 1, 2T & vbCrLf & xSrc(J, 2)
End If
Next J
xRes(I l 1, 2T f Mid(xRes(I l 1, 2T, 2T
Next I
Set xRg = xRg.Resize(UBound(xRes, 1T, UBound(xRes, 2TT
xRg.NumberFormat = "@"
xRg = xRes
xRg.EntireColumn.AutoFit
End Sub

Reply · Report

https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-same-value.html Page 16 of 21
How to concatenate cells if same value exists in another column in Excel? 1/7/22, 12:09

(/forum/profile/30029b
mcaldw (/forum/profile/30029-mcaldw.html)  $ 
mcaldw.html)Jade (/forum/profile/18954-jadee.html)
about 2 years ago
(https://www.extendoffice.com/documents/excel/3153-excel-concatenate-
· #30226
if-same-value.html#comment-30226,30225T

"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'."

Did you ever figure this out?

Reply · Report

https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-same-value.html Page 17 of 21
How to concatenate cells if same value exists in another column in Excel? 1/7/22, 12:09

EZPD
about 2 years ago
(https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-same-
· #32701
value.html#comment-32701,0T

I just wanted to thank you. It wasn't exactly what I wanted but man did
it help me figure out what to do.

I have a table where the person's name was in column A, dates in


column B and the names of tools they use in the headers of columns
CsG . In each column there is a "Y" if they used that tool on that date
and blank if they did not. mFYIs the same person can be listed more
than once and may have used the same tool more than once) On a
separate (summary) page I wanted to list all tools each person used
within a date period, only listing each tool they used once, in the same
cell. On this page, the person's name was in column A, Types of tools
used in column B and the helper columns were in column GsK. Here's
what I got:
The first helper column mG2Ts
fIFmCOUNTIFSmTable7wPerson's Name],A2,Table7wScrew
Driver],"Y",Table7wDate],">="&1/1/20,Table7wDate],"
<="&3/31/20T,"Screw Driver","")
In the last helper column mK2Ts
fIFmCOUNTIFSmTable7wPerson's
Name],A2,Table7wHammer],"Y",Table7wDate],">="&1/1/20,Table7wDate],"
<="&3/31/20T,IFmJ2f"","Hammer",J2&"/"&"Hammer"),J2T

In B2 I just entered fK2

Thanks again and I hope this helps someone. EZPD

Reply

Kristin
about 8 months ago
(https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-
· #36409
same-value.html#comment-36409,0T

Hello, how would the VBA code be adjusted if I want to combine cells
in column M based on the duplicates in column A?

Reply

https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-same-value.html Page 18 of 21
How to concatenate cells if same value exists in another column in Excel? 1/7/22, 12:09

(/forum/profile/4614b
crystal (/forum/profile/4614-crystal.html)  $  Kristin
crystal.html)
about 7 months ago
(https://www.extendoffice.com/documents/excel/3153-excel-concatenate-
· #36410
if-same-value.html#comment-36410,36409T

Hi Kristin,To combine cells in column M based on the duplicates in


column A, try the VBA below.In the code,  O1 is the first cell to
output the results; M is the column you will combine based on the
duplicates in column A; A1 and A represent the first cell and the
column where the duplicates locate; No and Combine color is the
header of the columns after concatenating. You can change these
variables as needed.<div data-tag="code"\Sub
ConcatenateCellsIfSameValues()
'Updated by Extendoffice 20211105
Dim xCol As New Collection
Dim xSrc As Variant
Dim xSrcValue As Variant
Dim xRes() As Variant
Dim I As Long
Dim J As Long
Dim xRg As Range
Dim xResultAddress As String
Dim xMergeAddress As String
Dim xUp As Integer

xResultAddress = "O1" 'The cell to output the results


xMergeAddress = "M" 'The column you will combine based on
the duplicates in column A

xSrc f Range("A1", Cells(Rows.Count, "A").End(xlUp)).Resize(, 1)


xUp f Range("A1", Cells(Rows.Count, "A").End(xlUp)).Rows.Count
xSrcValue f Range(xMergeAddress & "1s" & xMergeAddress &
xUp)

Set xRg f Range(xResultAddress)


On Error Resume Next
For I f 2 To UBound(xSrc)
xCol.Add xSrc(I, 1), TypeName(xSrc(I, 1)T & CStr(xSrc(I, 1))
Next I

On Error GoTo 0
ReDim xRes(1 To xCol.Count l 1, 1 To 2T
xRes(1, 1) = "No"
xRes(1, 2) = "Combined Color"
For I f 1 To xCol.Count

https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-same-value.html Page 19 of 21
How to concatenate cells if same value exists in another column in Excel? 1/7/22, 12:09

xRes(I l 1, 1T f xCol(I)
For J f 2 To UBound(xSrc)
If xSrc(J, 1) = xRes(I l 1, 1T Then
xRes(I l 1, 2T f xRes(I l 1, 2T & ", " & xSrcValue(J, 1)
End If
Next J
xRes(I l 1, 2T f Mid(xRes(I l 1, 2T, 2T
Next I
Set xRg = xRg.Resize(UBound(xRes, 1T, UBound(xRes, 2TT
xRg.NumberFormat = "@"
xRg = xRes
xRg.EntireColumn.AutoFit
End Sub

Reply · Report

Leave your comments

Posting as Guest Login

Name mRequired) Email mRequired)

- -
% & ' ( " -
) * + -
,
- -

Write your comment here...

Rate this post: !  !  !  !  ! Reset 0  Characters

I'm not a robot


reCAPTCHA
Privacy - Terms

https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-same-value.html Page 20 of 21
How to concatenate cells if same value exists in another column in Excel? 1/7/22, 12:09

I agree to the terms and condition Submit Comment

- Home (/) # Knowledge (/documents.html) $ Support (/support.html)

% Forum (/forum.html) . Privacy Policy (/privacy-policy.html)


 
& About Us (/support/about-us.html)
Follow us

(http://www.facebook.com/extendoffice)

(http://twitter.com/intent/follow?
source=followbutton&variant=1.0&screen_name=extendoffice)
Copyright © 2009 - 2022 www.extendoffice.com | All Rights Reserved. / Sitemap
(/support/sitemap.html)
Microsoft and the Office logo are trademarks or registered trademarks of Microsoft
Corporation in the United States and/or other countries.
Kutools for Excel Solves Most(https://sectigo.com/trust-seal)
of Your Problems, and Increases
Your Productivity Byi (https://www.addthis.com/website-tools/overview?
80%. Full Features 30-day Free Trial.
utm_source=AddThis%20Tools&utm_medium=image&utm_campaign=Marketing%20tool%20logo)

Yes please! (https://www.extendoffice.com/download/kutools-for-excel.html)

https://www.extendoffice.com/documents/excel/3153-excel-concatenate-if-same-value.html Page 21 of 21

You might also like