跳到主要内容

如何基于Excel中的重复项将多行合并为一行?

也许您有以下屏幕截图数据,在“产品名称”列A中,有一些重复项,现在您需要删除列A中的重复项,但要合并列B中的相应值,请参见屏幕截图:

文档高级组合1 -2 文档高级组合2

您有解决此任务的有效方法吗?

使用VBA代码将多个重复的行合并为一个

将多个重复行合并到一个 Kutools for Excel 中

使用 Kutools for Excel 合并多个重复行并对值求和/平均


箭头蓝色右气泡使用VBA代码将多个重复的行合并为一个

以下VBA代码可以帮助您根据列A的值合并重复的行,并且以下代码仅适用于两列。 因此,如果您有两列数据,那么此代码将为您提供帮助。

1。 按住 ALT + F11 键,然后打开 Microsoft Visual Basic for Applications窗口.

2。 点击 插页 > 模块,然后将以下代码粘贴到 模块窗口.

VBA代码:将多行合并为一

Sub CombineRows()
'Update 20131202
Dim WorkRng As Range
Dim Dic As Variant
Dim arr As Variant
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
Set Dic = CreateObject("Scripting.Dictionary")
arr = WorkRng.Value
For i = 1 To UBound(arr, 1)
    xvalue = arr(i, 1)
    If Dic.Exists(xvalue) Then
        Dic(arr(i, 1)) = Dic(arr(i, 1)) & " " & arr(i, 2)
    Else
        Dic(arr(i, 1)) = arr(i, 2)
    End If
Next
Application.ScreenUpdating = False
WorkRng.ClearContents
WorkRng.Range("A1").Resize(Dic.Count, 1) = Application.WorksheetFunction.Transpose(Dic.keys)
WorkRng.Range("B1").Resize(Dic.Count, 1) = Application.WorksheetFunction.Transpose(Dic.items)
Application.ScreenUpdating = True
End Sub

3。 然后按 F5 键,并弹出一个提示框,提醒您选择需要组合的范围。 看截图:

文档高级组合3

4。 然后点击 OK,则B列中的值已合并为一个记录,并用空格分隔。

文档高级组合4

笔记:

1.此代码可以销毁原始数据,使用时最好先备份数据。

2.此代码仅适用于两列,如果您有两列以上,则此代码将仅合并前两列并删除其他列数据。 使用此代码时,请务必谨慎。


箭头蓝色右气泡将多个重复行合并到一个 Kutools for Excel 中

既然上面的代码有一定的局限性和风险,是否有方便而安全的方法来完成这项工作? 用 Kutools for Excel高级组合行 实用程序,您可以快速删除您所基于的列中的多个重复行,然后合并或对其他记录进行一些计算到适当的单元格中。

Kutools for Excel 包括300多个便捷的Excel工具。 30天免费试用,不受限制。 立即获取.

安装 Kutools for Excel 后,请执行以下操作:

1。 选择要合并重复项的范围数据。

文档高级组合5

2。 然后点击 库工具 > 内容转换器 > 高级组合行。 看截图:

文档高级组合6

3。 在 高级组合行 对话框中,选择列名称作为要基于其组合值的键列,然后单击“确定”。 关键列。 看截图:

文档高级组合7

4。 然后继续单击您需要将其值合并到一条记录中的列,然后单击 合并作业 并选择 仅与分隔符结合,然后选择一个分隔符,您需要从右边的列表中分隔出组合的值。 看截图:

文档高级组合8

5。 完成设置后,单击 OK,您的数据将根据键列进行合并,并以分隔符分隔。

文档高级组合9

请注意: 如果您不需要为其余列设置操作,则只需保留默认状态即可。


箭头蓝色右气泡使用 Kutools for Excel 合并多个重复行并对值求和/平均

有了这个多功能 高级组合行 实用程序,您可以合并重复的行,并对值进行一些计算。

1。 选择要合并重复项的范围数据。

2。 去的 高级组合行 对话框;

  • (1.)选择一个列名作为要基于其计算其他列值的键列,然后单击 关键列;
  • (2.)选择您需要对值进行一些计算的列,然后单击 合并作业 > 合并计算,然后选择一种所需的计算类型,在这种情况下,我选择 总和。 看截图:

文档高级组合10

3。 如果您不需要为其余列设置操作,只需保持默认状态,然后单击 OK,则A列中的重复值将被合并,并且B列中其对应的值将相加。 查看屏幕截图:

文档高级组合11 -2 文档高级组合12

笔记:

1.如果您检查 我的数据有标题 选项,应用此实用程序时将忽略范围的标题(第一行)。

2. 使用格式化的值:合并重复的单元格后,将保留值的格式。

3.此实用程序不可用于过滤数据。

单击以了解有关此高级合并行功能的更多信息。


相关文章:

合并重复的行并求和

最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (8)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
I have used the code above successfully in the past however today, it is deleting column B. Looks like it's just removing duplicates...not sure what the issue is.

Sample Data
Reece, Ashley G01-Registered-UWG
Reece, Chris G02-Registered-KSU
Reece, Chris G03-Dropped-KSU
Reece, Jayden G02-Registered-UNG
Reece, Jayden G02-Withdrawn-UNG
This comment was minimized by the moderator on the site
I have in column A

1 Cast
2 Crew
3 Hiring
4 Cast
5 Hiring
6 Crew

I need these unique CAST CREW AND HIRING in another column without delete in column A entries.

Ex: I need (after formula)

1 Cast
2 Crew
3 Hiring

Is it possible by formula?
This comment was minimized by the moderator on the site
Above Macro only Combine the limited cell(e.g 20 cells) in b column. How to Combine more cells
This comment was minimized by the moderator on the site
Same - 2021 and this still only works if you have like 20 rows...
This comment was minimized by the moderator on the site
I've found that the "Combine multiple duplicate rows and sum/average the values" feature within KUTools does not work if the numbers in the row have a comma or period. This makes the tool unusable for combining and summing currency fields (which is why I tried the product in first place). I'm currently using the trial version which states that it is full-functioned; is it possible that this works in the paid version?
This comment was minimized by the moderator on the site
Same here. I was about to purchase because the tools are very nice, but figured I would try it first. Combine never works. I tried it for about 3 weeks now and it always locks up. No matter the power of my computer (ran on my at home powerhouse computer, same issue)
I kept cutting down to less and less data to see if it would work but it still would not.
The last one I tried was 4 columns and 100k rows and it still locked up. Not very useful unless you have like < 100 rows or something.
This comment was minimized by the moderator on the site
Same here, I filtered my data all the way down to 4 columns and 100k rows and it still work not work. I have tried this for 3 weeks now and unable to successfully complete my task. Kutools will only work if you have very few rows.
This comment was minimized by the moderator on the site
[quote]I've found that the "Combine multiple duplicate rows and sum/average the values" feature within KUTools does not work if the numbers in the row have a comma or period. This makes the tool unusable for combining and summing currency fields (which is why I tried the product in first place). I'm currently using the trial version which states that it is full-functioned; is it possible that this works in the paid version?By David Greene[/quote] Sorry, we will fix it in next version.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations