Làm cách nào để nối các hàng thành một ô dựa trên nhóm trong Excel?
Đây là một phạm vi gồm hai cột, một là danh sách lớp, và cột kia là danh sách tên học sinh. Như bạn thấy, một số học sinh học cùng lớp, một số không. Bây giờ tôi muốn nối các sinh viên trong cùng một lớp thành một ô như hình minh họa bên dưới, làm cách nào để xử lý nhanh trong Excel?
Nhóm và nối với các công thức và chức năng Bộ lọc
Nhóm và nối với các hàng kết hợp nâng cao
Nhóm và nối với các công thức và chức năng Bộ lọc
Trong Excel, bạn có thể áp dụng công thức cho các hàng được nối dựa trên một cột, sau đó sử dụng chức năng Bộ lọc để chỉ hiển thị kết quả.
Chú thích: Bạn cần sắp xếp dữ liệu của mình theo lớp trước khi làm theo các bước.
1. Trong ô trống bên cạnh phạm vi dữ liệu, ví dụ: C13, hãy nhập công thức này =IF(A13=A12,C12&", "&B13,B13), nhấn Enter và điền công thức vào các ô bằng thao tác kéo điền.
Trong công thức, A13 là dữ liệu đầu tiên trong cột “Lớp”, B13 là dữ liệu đầu tiên trong cột “Tên”, “,” là dấu phân tách để phân tách các nội dung nối.
2. Sau đó, trong cột tiếp theo, D13, nhập công thức này =IF(A13<>A14,"Last","") và kéo chốt điền xuống để áp dụng công thức cho các ô bạn cần.
3. Bây giờ hãy chọn tất cả phạm vi dữ liệu bao gồm các công thức và nhấp vào Ngày > Filter thêm Filter icons vào dữ liệu.
4. Nhấp vào Filter icon trong tiêu đề công thức cuối cùng, hãy kiểm tra Last chỉ hộp kiểm từ danh sách thả xuống và nhấp vào OK.
Bây giờ kết quả được hiển thị như bên dưới, bạn có thể loại bỏ cột trợ giúp cuối cùng nếu bạn không cần đến nó.
Nhóm và nối với mã VBA
Đây là mã VBA cũng có thể xử lý công việc này.
1. nhấn Alt + F11 các phím để kích hoạt Microsoft Visual Basic for Applications cửa sổ.
2. Sau đó trong cửa sổ, nhấp vào Tools > References để cho phép References hộp thoại và kiểm tra Microsoft Scripting Runtime. Xem ảnh chụp màn hình:
3. nhấp chuột OK, Và nhấp vào Insert > Module trong cửa sổ VBA và sao chép và dán mã VBA bên dưới vào Module kịch bản. Xem ảnh chụp màn hình:
VBA: Nối các hàng thành một ô dựa trên nhóm
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
xDic.Add xRgKey(I).Text, xStr
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.
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.

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