How to concatenate cells if same value exists in another column in 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.
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
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))
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)
xRes(I + 1, 2) = Mid(xRes(I + 1, 2), 2)
Set xRg = xRg.Resize(UBound(xRes, 1), UBound(xRes, 2))
xRg.NumberFormat = "@"
xRg = xRes
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.
Before applying Kutools for Excel, please download and install it firstly.
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.
- 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, and then go to apply the operation according above steps.
Easily concatenate cells if same value with Kutools for Excel
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 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.
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!