How to easily concatenate text based on criteria in Excel?
Supposing I have a column of ID numbers which contains some duplicates and a column of names, and now, I want to concatenate the names based on the unique ID numbers as left screenshot shown, to quickly combine the text based on criteria, how could we do in Excel?
Concatenate text based on criteria with User Defined Function
Concatenate text based on criteria with Kutools for Excel
Concatenate text based on criteria with User Defined Function
To combine text with the unique ID numbers, you can extract the unique values first and then create a User Defined Function to combine the names based on the unique ID.
1. Take the following data as example, you need to extract the unique ID numbers first, please apply this array formula: =IFERROR(INDEX($A$2:$A$15, MATCH(0,COUNTIF($D$1:D1, $A$2:$A$15), 0)),""),enter this formula into a blank cell, D2 for example, then press Ctrl + Shift + Enter keys together, see screenshot:
Tip: In the above formula, A2:A15 is the list data range you want to extract unique values from, D1 is the first cell of the column you want to put out the extracting result.
2. And then drag the fill handle down to extract all unique values until blanks displayed, see screenshot:
3. In this step, you should create a User Defined Function to combine the names based on the unique ID numbers, please hold down the ALT + F11 keys, and it opens the Microsoft Visual Basic for Applications window.
4. Click Insert > Module, and paste the following code in the Module Window.
VBA code: concatenate text based on criteria
Function ConcatenateIf(CriteriaRange As Range, Condition As Variant, ConcatenateRange As Range, Optional Separator As String = ",") As Variant 'Updateby Extendoffice Dim xResult As String On Error Resume Next If CriteriaRange.Count <> ConcatenateRange.Count Then ConcatenateIf = CVErr(xlErrRef) Exit Function End If For i = 1 To CriteriaRange.Count If CriteriaRange.Cells(i).Value = Condition Then xResult = xResult & Separator & ConcatenateRange.Cells(i).Value End If Next i If xResult <> "" Then xResult = VBA.Mid(xResult, VBA.Len(Separator) + 1) End If ConcatenateIf = xResult Exit Function End Function
5. Then save and close this code, go back to your worksheet, and enter this formula into cell E2, =CONCATENATEIF($A$2:$A$15, D2, $B$2:$B$15, ",") , see screenshot:
6. Then drag the fill handle down to the cells that you want to apply this formula, and all the corresponding names have been combined based on the ID numbers, see screenshot:
Tips:
1. In the above formula, A2:A15 is the original data which you want to combine based on, D2 is the unique value you have extracted, and B2:B15 is the name column that you want to combine together.
2. As you can see, I combined the values which are separated by comma, you can use any other characters by changing the comma “,” of the formula as you need.
Concatenate text based on criteria with Kutools for Excel
If you have Kutools for Excel, with its Advanced Combine Rows utility, you can quickly and conveniently concatenate the text base on criteria.
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, do as following steps:
1. Select the data range that you want to combine based on one column.
2. Click Kutools > Merge & Split > Advanced Combine Rows, see screenshot:
3. In the Combine Rows Based on Column dialog box, click the ID column and then click Primary Key to make this column as the key column that your combined data based on, see screenshot:
4. And then click Name column which you want to combined the values, then click Combine option, and choose one separator for the combined data, see screenshot:
5. After finishing these settings, click OK to exit the dialog, and the data in column B has been combined together based on the key column A. See screenshot:
With this feature, the following problem will be solved as soon as possible:
How to combine multiple rows into one and sum duplicates in Excel?
Download and free trial Kutools for Excel Now !
Demo: Concatenate text based on same cell in another column 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!
