跳到主要内容

在 Excel 中,经常会遇到包含重复条目的数据集。 通常,您可能会发现自己拥有一系列数据,其中的关键挑战是有效组合这些重复行,同时对相应列中的值进行求和,如下图所示。 在此背景下,我们将深入研究几种实用方法,这些方法可以帮助您合并重复数据并聚合其关联值,从而增强 Excel 工作簿的清晰度和实用性。


合并重复行并使用 Consolidate 函数对值求和

合并是我们在 Excel 中合并多个工作表或行的有用工具,通过此功能,我们可以快速轻松地合并重复的行并汇总其相应的值。 请按照以下步骤进行:

第 1 步:选择目标单元格

选择您希望合并数据出现的位置。

步骤 2:访问合并功能并设置合并

  1. 点击 时间 > 整合,请参见屏幕截图:
  2. 整合 对话框:
    • (1.)选择 总和功能 下拉列表;
    • (2.) 单击以选择要合并到的范围 参考文献 框;
    • (3.)检查 第一排左栏在中使用标签 选项;
    • (4.) 最后,单击 OK 按钮。

结果:

Excel 将合并第一列中找到的所有重复项,并将相邻列中的相应值相加,如下图所示:

笔记:
  • 如果范围不包含标题行,请确保 取消选中顶行 来自 在中使用标签 选项。
  • 使用此功能,只能根据数据的第一列(最左边)来合并计算。

合并重复的行并使用强大的功能对值求和 – Kutools

如果你已经安装 Kutools for Excel,它的 高级组合行 该功能允许您轻松组合重复的行,提供对数据进行求和、计数、平均或执行其他计算的选项。 此外,这一功能不仅限于一个键列,它还可以处理多个键列,使复杂的数据整合任务变得更加容易。

备注: 如果你想用这个 高级组合行 功能,请 下载并安装 Kutools for Excel 第一。

安装后 Kutools for Excel,选择数据范围,然后单击 库工具 > 合并与拆分 > 高级组合行.

高级组合行 对话框中,请设置以下操作:

  1. 单击要根据其合并重复项的列名称,在这里,我将单击“产品”,然后选择 首要的关键 从下拉列表中 操作 柱;
  2. 然后,选择要对值求和的列名称,然后选择 总和 从下拉列表中 操作 柱;
  3. 对于其他列,您可以选择您需要的操作,例如将值与特定分隔符组合或执行某种计算; (如果只有两列,则可以忽略此步骤)
  4. 最后,您可以预览合并结果,然后单击 OK 按钮。

结果:

现在,键列中的重复值被组合起来,其他相应的值被总结起来,如下图所示:

提示:
  • 通过这个有用的功能,您还可以根据重复的单元格值合并行,如下所示:
  • 此功能 支持撤消,如果您想恢复原始数据,只需按 按Ctrl + Z.
  • 要应用此功能,请 下载并安装 Kutools for Excel 第一。

合并重复行并将值与数据透视表相加

Excel 中的数据透视表提供了一种重新排列、分组和汇总数据的动态方式。 当您面对充满重复条目的数据集并且需要对相应值求和时,此功能变得非常有用。

第 1 步:创建数据透视表

  1. 选择数据范围。 然后,转到 插页 选项卡,然后单击 数据透视表,请参见屏幕截图:
  2. 在弹出的对话框中,选择数据透视表报表的放置位置,您可以根据需要将其放置到新工作表或现有工作表中。 然后,单击 OK。 看截图:
  3. 现在,数据透视表已插入到选定的目标单元格中​​。 看截图:

步骤 2:配置数据透视表:

  1. 数据透视表字段 窗格中,将包含重复项的字段拖到 区域。 这将对您的重复项进行分组。
  2. 接下来,将包含要求和的值的字段拖到 价值观 区域。 默认情况下,Excel 对值进行求和。 请参阅下面的演示:

结果:

数据透视表现在显示合并了重复项的数据及其汇总值,为分析提供清晰简洁的视图。 看截图:


合并重复的行并使用VBA代码对值求和

如果您对 VBA 代码感兴趣,在本节中,我们将提供一个 VBA 代码来合并重复行并对其他列中的相应值求和。 请按照以下步骤进行:

步骤1:打开VBA工作表模块编辑器并复制代码

  1. 按住 ALT + F11 在Excel中打开键 Microsoft Visual Basic应用程序 窗口。
  2. 点击 插页 > 模块,然后将以下代码粘贴到 模块 窗口。
    VBA代码:合并重复的行并求和
    Sub CombineDuplicateRowsAndSumForMultipleColumns()
    'Update by Extendoffice
        Dim SourceRange As Range, OutputRange As Range
        Dim Dict As Object
        Dim DataArray As Variant
        Dim i As Long, j As Long
        Dim Key As Variant
        Dim ColCount As Long
        Dim SumArray() As Variant
        Dim xArr As Variant
        Set SourceRange = Application.InputBox("Select the original range:", "Kutools for Excel", Type:=8)
        If SourceRange Is Nothing Then Exit Sub
        ColCount = SourceRange.Columns.Count
        Set OutputRange = Application.InputBox("Select a cell for output:", "Kutools for Excel", Type:=8)
        If OutputRange Is Nothing Then Exit Sub
        Set Dict = CreateObject("Scripting.Dictionary")
        DataArray = SourceRange.Value
        For i = 1 To UBound(DataArray, 1)
            Key = DataArray(i, 1)
            If Not Dict.Exists(Key) Then
                ReDim SumArray(1 To ColCount - 1)
                For j = 2 To ColCount
                    SumArray(j - 1) = DataArray(i, j)
                Next j
                Dict.Add Key, SumArray
            Else
                xArr = Dict(Key)
                For j = 2 To ColCount
                    xArr(j - 1) = xArr(j - 1) + DataArray(i, j)
                Next j
                Dict(Key) = xArr
            End If
        Next i
        OutputRange.Resize(Dict.Count, ColCount).ClearContents
        i = 1
        For Each Key In Dict.Keys
            OutputRange.Cells(i, 1).Value = Key
            For j = 1 To ColCount - 1
                OutputRange.Cells(i, j + 1).Value = Dict(Key)(j)
            Next j
            i = i + 1
        Next Key
        Set Dict = Nothing
        Set SourceRange = Nothing
        Set OutputRange = Nothing
    End Sub
    

第2步:执行代码

  1. 粘贴此代码后,请按 F5 键来运行此代码。 在提示框中选择需要合并求和的数据范围。 然后,单击 OK.
  2. 在接下来的提示框中,选择要输出结果的单元格,然后单击 OK.

结果:

现在,重复的行被合并,并且它们对应的值已被求和。 看截图:


在 Excel 中对重复行进行组合和求和既简单又高效。 从简单的合并功能、高级的 Kutools、分析数据透视表或灵活的 VBA 编码中进行选择,以找到适合您的技能和需求的解决方案。 如果您有兴趣探索更多 Excel 提示和技巧,我们的网站提供了数千个教程,请 单击此处访问它们。 感谢您的阅读,我们期待在未来为您提供更多有用的信息!


相关文章:

  • 根据重复项将多行合并为一行
  • 也许,您有一系列数据,在产品名称 A 列中,有一些重复的项目,现在您需要删除 A 列中的重复条目,但合并 B 列中的相应值。如何在 Excel 中完成此任务?
  • Vlookup并返回多个没有重复的值
  • 有时,您可能需要vlookup并将多个匹配的值一次返回到单个单元格中。 但是,如果在返回的单元格中填充了一些重复的值,那么在返回所有匹配值时如何忽略重复项,而仅保留唯一值,如下面的Excel截图所示?
  • 合并具有相同 ID/名称的行
  • 例如,您有一个如下图所示的表格,您需要将行与订单ID合并,有什么想法吗? 在这里,本文将为您介绍两种解决方案。
Comments (30)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Em planilha sem cálculo número, onde eu tenho uma lista de prestadores de serviço para determinadas empresas, como faço para deixar classificado por empresas, por ordem de empresas? Se é possível.
Na planilha eu tenho, o nome da pessoa, razão social e empresa. Neste caso algumas empresas repetem, gostaria de classificar automático, sem precisar refazer um por um.
This comment was minimized by the moderator on the site
is there a way to save the specific merging and combining settings so that i can reuse them for future workbooks?
This comment was minimized by the moderator on the site
Thanks for your help.

This comment was minimized by the moderator on the site
SN SAD No Unit Item No Description Qty CIF_Value ID_EXD AID CSF ARF ECS RCF RDF IFT IDP AIT VAT
1 M200 UNT 1 Pen 194 500 50 0 0 0 0 0 0 0 0 0 65
2 M200 UNT 2 Pencil 241 250 25 0 0 0 0 0 0 0 0 0 32.5
3 M200 UNT 3 Cutter 204 400 40 0 0 0 0 0 0 0 0 0 52
4 M200 UNT 4 Copy 171 600 60 0 0 0 0 0 0 0 0 0 78
5 M300 KGM 1 Cup 220 250 25 0 500 0 0 0 0 0 0 0 32.5
6 M300 KGM 2 Plate 40 350 35 155 0 0 0 0 0 0 0 0 45.5
7 M300 UNT 3 Bottle 2 150 15 131 0 0 0 0 0 0 0 0 19.5
8 M300 UNT 4 Glass 2 90 9 34 0 0 0 0 0 0 0 0 11.7
9 M400 null 1 Shirt 20 800 80 0 0 0 0 0 0 0 0 0 104
10 M400 KGM 2 Pant 5 5000 500 0 0 0 0 0 0 0 0 0 650
11 M400 null 3 Shoe 12 7200 720 0 0 0 0 0 0 0 0 0 936
12 M400 MTR 4 Sandle 40 1600 160 0 0 0 0 0 0 0 0 0 208
13 M400 UNT 5 Belt 100 2000 200 0 0 0 0 0 0 0 0 0 260
how to sum cif value and remove duplicate No. (SAD No.) Please help me.
This comment was minimized by the moderator on the site
I am sooooo happy & glad with your tips. Allah bless you.
This comment was minimized by the moderator on the site
Sub MergeSameCells()
Application.DisplayAlerts = False

Dim rng As Range

MergeCells:

For Each rng In Selection
If rng.Value = rng.Offset(1, 0).Value And rng.Value <> "" Then
Range(rng, rng.Offset(1, 0)).Merge
GoTo MergeCells
End If
Next

End Sub
This comment was minimized by the moderator on the site
An absolute mess of an explantation. Thanks for the effort but it did nothing to help.
This comment was minimized by the moderator on the site
LOVE IT!!! YOUR SAVE MY LIFE!!
This comment was minimized by the moderator on the site
Ifsum=(columns include,start point row,sum column)
Example ifsum=(A:D,B:2,D:D)
WAY EASIER!
This comment was minimized by the moderator on the site
Hi Am chinnaraju

can u please assist for this. Any one?

=VLOOKUP(M5,E:F,2,)


Thanks in advance.
This comment was minimized by the moderator on the site
Needs to be:
=VLOOKUP(M5,E:F,2,FALSE)
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations