Skip to main content

在 Excel 中合併重複行並加總其值(簡單技巧)

Author: Xiaoyang Last Modified: 2025-05-12

在 Excel 中,遇到包含重複條目的數據集是一種常見的情況。通常,您可能會面臨一組數據,其中的主要挑戰是如何有效地合併這些重複的行,同時將相應列中的值進行加總,如下方截圖所示。在此背景下,我們將深入探討幾種實用的方法,幫助您合併重複數據並彙總其相關值,從而提升 Excel 工作簿的清晰度和實用性。


使用 Consolidate 功能合併重複行並加總值

Consolidate 是一個非常有用的工具,可以幫助我們在 Excel 中合併多個工作表或行。利用此功能,我們可以快速輕鬆地合併重複的行並將其對應的值進行加總。請按照以下步驟操作:

步驟 1:選擇目標單元格

選擇您希望合併後的數據出現的位置。

步驟 2:訪問 Consolidate 功能並設置合併

  1. 點擊「數據」>「Consolidate」,如下方截圖所示:
    A screenshot of the Consolidate option in Excel ribbon
  2. 在「Consolidate」對話框中:
    • (1.) 從「Function」下拉列表中選擇「Sum」;
    • (2.) 點擊選擇您想合併的範圍,在「Reference」框中選取;
    • (3.) 在「Use labels in」選項中勾選「首行」和「最左列」;
    • (4.) 最後,點擊「確定」按鈕。
    • A screenshot of the options in the Consolidate dialog box, including Function, Reference, and Use labels in settings

結果:

Excel 將合併第一列中找到的所有重複項,並在相鄰列中加總其對應的值,如下方截圖所示:

A screenshot of Excel after consolidation, showing combined duplicate rows and summed values

注意:
  • 如果範圍不包含標題行,請確保取消勾選「Use labels in」選項中的「首行」。
  • 使用此功能時,計算只能基於數據的第一列(最左邊的一列)。

使用 Kutools 合併重複行並加總值

如果您已經安裝了「Kutools for Excel」,它的「高級合併行」功能可以讓您輕鬆合併重複行,並提供加總、計數、平均或其他計算選項。此外,這個功能不僅限於單一關鍵列,還能處理多個關鍵列,讓複雜的數據合併任務變得更加容易。

Kutools for Excel 提供超過 300 種進階功能,簡化複雜任務,提升創造力與效率。 結合 AI 能力,Kutools 能夠精準自動化任務,讓數據管理變得輕而易舉。Kutools for Excel 的詳細資訊...免費試用...

安裝「Kutools for Excel」後,選擇數據範圍,然後點擊「Kutools」>「合併與分割」>「高級合併行」。

在「高級合併行」對話框中,請設置以下操作:

  1. 點擊您希望根據其合併重複項的列名稱,這裡我會點擊「Product」,然後在「Operation」列的下拉列表中選擇「主鍵列」;
  2. 然後,選擇您希望加總值的列名稱,並在「Operation」列的下拉列表中選擇「求和」;
  3. 至於其他列,您可以選擇所需的運算方式,例如使用特定分隔符合併值或執行某種計算;(如果只有兩列,則可以忽略此步驟)
  4. 最後,您可以預覽合併結果,然後點擊「確定」按鈕。
  5. A screenshot of Kutools' Advanced Combine Rows dialog box, with options to combine duplicates and sum values

結果:

現在,關鍵列中的重複值已合併,其他相應的值也已加總,如下方截圖所示:

A screenshot showing the result of combining duplicate rows and summing values in Excel using Kutools

提示:
  • 使用這個實用的功能,您還可以根據重複單元格值來合併行,如下方示例所示:
    An animated demonstration of combining rows based on duplicate values in Excel using Kutools
  • 此功能「支持撤銷」,如果您想恢復原始數據,只需按下「Ctrl + Z」即可。
  • 要應用此功能,請下載並安裝 Kutools for Excel

使用 Pivot Table 合併重複行並加總值

Excel 中的資料透視表提供了一種動態的方式來重新排列、分組和彙總數據。當您面對充滿重複條目的數據集並且需要加總相應值時,這一功能變得非常有用。

步驟 1:創建資料透視表

  1. 選擇數據範圍,然後轉到「插入」選項卡,點擊「資料透視表」,如下方截圖所示:
    A screenshot of selecting the Pivot Table option from the Insert tab in Excel
  2. 在彈出的對話框中,選擇您希望放置資料透視表報告的位置,您可以將其放在新工作表或現有工作表中。然後,點擊「確定」。請參閱截圖:
    A screenshot of the Pivot Table dialog box, where you can choose to insert the table in a new or existing sheet
  3. 現在,資料透視表已插入到所選的目標單元格中。請參閱截圖:
    A screenshot showing a blank Pivot Table inserted in the Excel worksheet

步驟 2:配置資料透視表: 

  1. 在「資料透視表字段」窗格中,將包含重複項的字段拖到「行」區域。這將對您的重複項進行分組。
  2. 接下來,將包含您希望加總值的字段拖到「值」區域。默認情況下,Excel 會對這些值進行求和。請參閱下方示例:
  3. An animated demonstration of configuring the Pivot Table fields to group duplicates and sum values in Excel

結果:

現在,資料透視表顯示了合併後的數據及其加總的值,為分析提供了清晰簡潔的視圖。請參閱截圖:

A screenshot showing the result of combining duplicate rows and summing values using a Pivot Table in Excel


使用 VBA 程式碼合併重複行並加總值

如果您對 VBA 程式碼感興趣,在本節中,我們將提供一段 VBA 程式碼來合併重複行並加總其他列中的相應值。請按照以下步驟操作:

步驟 1:打開 VBA 表模塊編輯器並複製程式碼

  1. 在 Excel 中按住「ALT + F11」鍵以打開「Microsoft Visual Basic for Applications」窗口。
  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」鍵運行程式碼。在提示框中,選擇您希望合併並加總的數據範圍,然後點擊「確定」。
    A screenshot of selecting the data range in the VBA input box to combine and sum duplicate rows
  2. 在下一個提示框中,選擇一個單元格作為輸出結果的位置,然後點擊「確定」。
    A screenshot of selecting the output cell in the VBA input box for combined and summed results

結果:

現在,重複行已合併,其相應的值也已加總。請參閱截圖:

A screenshot of the results after running the VBA code to combine duplicate rows and sum their values


在 Excel 中合併並加總重複行可以簡單高效。您可以從易用的 Consolidate 功能、進階的 Kutools、分析型的資料透視表或靈活的 VBA 編程中選擇適合您技能和需求的解決方案。如果您對探索更多 Excel 技巧感興趣,我們的網站提供了數千個教程,請 點擊這裡訪問它們。感謝您的閱讀,我們期待未來為您提供更多有用的信息!


相關文章:

  • 根據重複項合併多行為一行
  • 也許,您有一組數據,在產品名稱列 A 中有一些重複項目,現在您需要刪除列 A 中的重複條目,但合併列 B 中的相應值。如何在 Excel 中完成此任務?
  • Vlookup 並返回無重複的多個值
  • 有時候,您可能希望 vlookup 並一次性返回多個匹配值到單一單元格中。但如果返回的單元格中存在一些重複值,如何忽略重複值並僅保留唯一值,如下方截圖所示在 Excel 中返回所有匹配值?
  • 合併具有相同 ID/名稱的行
  • 例如,您有一個如下方截圖所示的表格,您需要合併具有訂單 ID 的行,有什麼想法嗎?本文將為您介紹兩種解決方案。