Skip to main content

How to concatenate rows into one cell based on group in Excel?

Here is a range of two columns, one is the class list, and the other is the student names list. As you see, some students are in the same class, some are not. Now I want to concatenate the students in the same class into one cell as below screenshot shown, how can I quickly handle it in Excel?
doc concatenate by group 1

Group and concatenate with formulas and Filter function

Group and concatenate with VBA code

Group and concatenate with Advanced Combine Rowsgood idea3

Group and concatenate with formulas and Filter function

In Excel, you can apply formulas to concatenated rows based on one column, then use the Filter function to display the result only.

Note: You need to sort your data by the class before following steps.

1. In a blank cell next to the data range, for instance, C13, type this formula =IF(A13=A12,C12&", "&B13,B13), press Enter key and fill the formula to cells with dragging fill handle.
doc concatenate by group 2

In the formula, A13 is the first data in the “Class” column, B13 is the first data in the “Name” column, “,” is the separator to delimit concatenate contents.

2. Then in the next column, D13, type this formula =IF(A13<>A14,"Last","") , and drag fill handle down to apply the formula to cells you need.
doc concatenate by group 3

3. Now select all the data range including formulas and click Data > Filter to add Filter icons to the data.
doc concatenate by group 4

4. Click the Filter icon in last formula header, check Last checkbox only from the drop-down list, and click OK.
doc concatenate by group 5

Now the result is shown as below, you can remove last helper column if you do not need it ever.
doc concatenate by group 6

Group and concatenate with VBA code

Here is a VBA code that also can handle this job.

1. Press Alt + F11 keys to enable the Microsoft Visual Basic for Applications window.

2. Then in the window, click Tools > References to enable References dialog, and check Microsoft Scripting Runtime. See screenshot:
doc concatenate by group 7
doc concatenate by group 8

3. Click OK, and click Insert > Module in VBA window, and copy and paste below VBA code to the Module script. See screenshot:

VBA: Concatenate rows into one cell based on group

  Sub ConcatenateCellsIfSameValues()
    Dim I As Long
    Dim J As Long
    Dim xRg As Range
    Dim xRgKey As Range
    Dim xRgVal As Range
    Dim xStr As String
    Dim xDic As New Dictionary
    On Error Resume Next
    Set xRg = Application.InputBox("Select data range", "KuTools for Excel", Selection.Address, , , , , 8)
    If xRg Is Nothing Then Exit Sub
    Set xRgKey = Application.InputBox("Select key column", "KuTools for Excel", xRg.Columns(1).Address, , , , , 8)
    If xRgKey Is Nothing Then
        MsgBox "Key column cannot be empty", vbInformation, "KuTools for Excel"
    End If
    Set xRgVal = xRg(1).Offset(, 1).Resize(xRg.Rows.Count, xRg.Columns.Count - 1)
    For I = 1 To xRgKey.Count
        If I > xRgKey.Count Then Exit For
        xStr = ""
        For J = 1 To xRgVal.Columns.Count
            xStr = xStr & " " & xRgVal(I, J)
        If xDic.Exists(xRgKey(I).Text) Then
            xDic(xRgKey(I).Text) = xDic(xRgKey(I).Text) & xStr
            I = I - 1
            xDic.Add xRgKey(I).Text, xStr
        End If
    For I = 1 To xRgVal.Count
        xRgVal(I).Value = xDic(xRgKey(I).Text)
End Sub

doc concatenate by group 9

4. Press F5 key, and select the data range you use in the popping dialog.
doc concatenate by group 10

5. Click OK to select the key column you want to group based on.
doc concatenate by group 11

6. Click OK, now the result is shown as below:
doc concatenate by group 12

Group and concatenate with Advanced Combine Rows

Here is a utility in Kutools for Excel, Advanced Combine Rows, which can combine rows or do calculations based on a key column in Excel.

Kutools for Excel, with more than 300 handy functions, makes your jobs more easier. 

After installing Kutools for Excel, please do as below:(Free Download Kutools for Excel Now!)

1. Select the data range you use and click Kutools > Merge & Split > Advanced Combine Rows.
doc concatenate by group 13

2. In the Advanced Combine Rows window, choose the column which you want to combine rows based on, and click Primary Key to set it as key column.
doc concatenate by group 14

3. Select the column you need to combine, click Combine, and choose one delimiter you use to separate the combined contents.

doc concatenate by group 15 doc arrow right doc concatenate by group 16

4. Click Ok. The result is shown as this:
doc concatenate by group 17

Note: Before apply the utility, you had better have a copy of the original data.

Best Office Productivity Tools

Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...

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!
Comments (2)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
la formule excel détaillée plus haut ne fonctionne pas, il y a un problème=IF(A13=A12,C12&", "&B13,B13)
This comment was minimized by the moderator on the site
Hi, could you repeat the qustion in English? This formula I have tried again, it is correct.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
Rate this post:
0   Characters
Suggested Locations