如何基于Excel中的组将行连接到一个单元格中?
这是两列的范围,一列是班级列表,另一列是学生姓名列表。 如您所见,有些学生在同一堂课,有些则不在。 现在,我想将同一班级的学生连接到一个单元格中,如下面的屏幕截图所示,如何在Excel中快速处理它?
用公式和过滤器功能分组和连接
分组并连接VBA代码
分组并连接“高级合并行”
用公式和过滤器功能分组和连接
在Excel中,您可以将公式应用于基于一列的串联行,然后使用“筛选器”功能仅显示结果。
备注:在执行以下步骤之前,您需要按类对数据进行排序。
1.在数据范围旁边的空白单元格中,例如C13,键入此公式 =IF(A13=A12,C12&", "&B13,B13), 按 Enter
键,然后拖动填充手柄将公式填充到单元格中。
在公式中,A13是“类别”列中的第一个数据,B13是“名称”列中的第一个数据,“,”是用于分隔连接内容的分隔符。
2.然后在下一列D13中键入此公式 =IF(A13<>A14,"Last","")
,然后向下拖动填充手柄以将公式应用于所需的单元格。
3.现在选择所有数据范围,包括公式,然后单击 Data > Filter 添加 Filter icons
数据。
4。 点击 Filter icon 在最后一个公式标题中,检查 Last 仅从下拉列表中选中复选框,然后单击 OK
.
现在结果显示如下,如果不需要,您可以删除最后一个帮助器列。
分组并连接VBA代码
这是一个VBA代码,也可以处理此工作。
1。 按 Alt + F11 启用 Microsoft Visual Basic for Applications 窗口。
2.然后在窗口中,单击 Tools > References 启用 References 对话框,然后检查 Microsoft Scripting Runtime
。 看截图:
3。 点击 OK和点击 Insert > Module 在VBA窗口中,然后将以下VBA代码复制并粘贴到 Module 脚本。 看截图:
VBA:根据组将行连接到一个单元格中
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.
Free Downloadfree full-featured in 30 days
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
Best Office Productivity Tools
Transform Hours into Minutes with Kutools for Excel!
Ready to supercharge your Excel tasks? Harness the power of Kutools for Excel - your ultimate time-saving tool. Streamline intricate tasks and glide through your data like a pro. Experience Excel at lightning speed!
Why You Need Kutools for Excel
🛠️ Over 300 Powerful Features: Kutools is packed with more than 300 advanced features, simplifying your work in over 1500 scenarios.
📈 Superior Data Processing: Merge cells, remove duplicates, and perform advanced data conversions – all without breaking a sweat!
⏱️ Efficient Batch Operations: Why put in extra effort when you can work smart? Import, export, combine, and tweak data in bulk with ease.
📊 Customizable Charts and Reports: Access a broad variety of additional charts and generate insightful reports that tell a story.
🗄️ Powerful Navigation Pane: Gain an advantage with the robust Column Manager, Worksheet Manager, and Custom Favorites.
📝 Seven Types of Drop-down Lists: Make data entry a breeze with drop-down lists of various features and types.
🎓 User-Friendly: A breeze for beginners and a powerful tool for experts.
Download Now and Soar Through Time with Excel!
Read More... Free Download... Purchase...
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!
Read More... Free Download... Purchase...