Tip: Other languages are Google-Translated. You can visit the English version of this link.
or

Register

or
0
0
0
s2smodern

## 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?

Group and concatenate with formulas and Filter function

Group and concatenate with VBA code

#### Easily Combine columns or rows without losing data in Excel

Supposing you are working with a worksheet which contains multiple duplicate records, and now you need to combine / merge the rows based on the same value and do some calculations, such as sum, average, count of the duplicate rows. With this Advanced Combine Rows of Kutools for Excel, you can quickly combine same values / same data or duplicate rows into appropriate cells.  Click for 60 days free trial!

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

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.

3. Now select all the data range including formulas and click Data > Filter to add Filter icons to the data.

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

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

#### 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:

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()
'UpdatebyExtendoffice20180201
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)
Next
If xDic.Exists(xRgKey(I).Text) Then
xDic(xRgKey(I).Text) = xDic(xRgKey(I).Text) & xStr
xRgKey(I).EntireRow.Delete
I = I - 1
Else
End If
Next
For I = 1 To xRgVal.Count
xRgVal(I).Value = xDic(xRgKey(I).Text)
Next
End Sub```

4. Press F5 key, and select the data range you use in the popping dialog.

5. Click OK to select the key column you want to group based on.

6. Click OK, now the result is shown as below:

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

After installing Kutools for Excel, please do as below:

1. Select the data range you use and click Kutools > Content > Advanced Combine Rows.

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.

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

4. Click Ok. The result is shown as this:

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

### Recommended Productivity Tools

#### Office Tab

Bring handy tabs to Excel and other Office software, just like Chrome, Firefox and new Internet Explorer.

#### Kutools for Excel

Amazing! Increase your productivity in 5 minutes. Don't need any special skills, save two hours every day!

200 New Features for Excel, Make Excel Much Easy and Powerful:

• Merge Cell/Rows/Columns without Losing Data.
• Combine and Consolidate Multiple Sheets and Workbooks.
• Compare Ranges, Copy Multiple Ranges, Convert Text to Date, Unit and Currency Conversion.
• Count by Colors, Paging Subtotals, Advanced Sort and Super Filter,