Skip to main content

如何根據某一列的值快速合併行,然後在 Excel 中進行一些計算?

Author: Sun Last Modified: 2025-05-12

例如,您有一個數據範圍,其中一列包含重複值,現在您希望根據 A 列(有重複值)合併行,然後根據合併後的行對另一列進行一些計算,如下截圖所示:

在 Excel 中,沒有快速的方法可以根據某一列的值合併行,但這裡我將介紹一些技巧,幫助您合併重複行,然後對另一列進行求和或其他計算。


arrow blue right bubble 使用 VBA 根據某一列合併行然後求和

有一段 VBA 代碼可以幫助您合併重複行,然後對另一列的值進行求和。

1. 按下 F11 + Alt 鍵啟用 Microsoft Visual Basic for Applications 窗口,然後點擊 插入 > 模塊,並將以下 VBA 代碼複製粘貼到新的模塊窗口中。

VBA:根據某一列的值合併行

Sub MG30Nov12()
'Updateby20150519
Dim Rng As Range
Dim InputRng As Range
Dim nRng As Range
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Range :", xTitleId, InputRng.Address, Type:=8)
Set InputRng = InputRng.Parent.Range(InputRng.Columns(1).Address)
With CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
For Each Rng In InputRng
    If Not .Exists(Rng.Value) Then
        .Add Rng.Value, Rng.Offset(, 1)
    Else
        .Item(Rng.Value).Value = .Item(Rng.Value).Value + Rng.Offset(, 1)
            If nRng Is Nothing Then
                Set nRng = Rng
            Else
                Set nRng = Union(nRng, Rng)
            End If
    End If
Next
If Not nRng Is Nothing Then
    nRng.EntireRow.Delete
End If
End With
End Sub

2. 按 F5 或點擊 運行 按鈕來運行 VBA,會彈出一個對話框讓您選擇要處理的數據範圍。請參見截圖:

A screenshot showing the data selection prompt for merging rows based on one column in Excel using VBA

3. 選擇工作範圍後,點擊 確定。現在數據已經根據第一列合併,並且第二列的值已進行了求和。

A screenshot showing merged rows and summed values using VBA in Excel

注意:此 VBA 代碼僅在根據第一列合併並對第二列求和時才能正確運行。


arrow blue right bubble 使用高級合併行根據某一列合併行然後求和

如果您不熟悉 VBA 代碼,您可以使用第三方插件工具 Kutools for Excel高級合併行功能,輕鬆快速地合併重複行,然後對另一列的值進行求和。

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

1. 選擇您要使用的數據,然後點擊 Kutools > 合併與分割 > 高級合併行。請參見截圖:

A screenshot showing the Kutools Advanced Combine Rows option in Excel

2. 在彈出的對話框中,選擇您將基於其合併其他列的列,然後點擊 鍵,再點擊另一列並點擊 計算,然後選擇 求和

提示:如果您的選定範圍包含標題,請勾選 數據包含標題,勾選 使用格式化後的值 將保留合併後的格式。

A screenshot showing the options in the Kutools Combine Rows dialog for merging and summing

3. 點擊 確定。現在數據已經根據主列合併,另一列則進行了求和。請參見截圖:

A screenshot showing the merged rows and summed values after using Kutools


arrow blue right bubble 使用高級合併行根據某一列合併行,然後對其他列進行不同的操作。

高級合併行功能非常強大,因為它不僅可以根據某一列合併重複行然後對另一列求和,還可以根據某一列合併重複行然後對其他列進行不同的操作。

假設我有一個如下截圖所示的數據範圍,我希望根據 A 列合併 B 列中的值並對 C 列中的值進行求和。
A screenshot showing a dataset with duplicate values where merging and different operations are required

Kutools for Excel - 包含超過 300 種 Excel 必備工具。永久免費享受 AI 功能!立即下載!

1. 選擇數據範圍,然後點擊 Kutools > 合併與分割 > 高級合併行。請參見截圖:

A screenshot showing the Kutools Advanced Combine Rows option in Excel

2. 在 基於列合併行 對話框中,執行以下操作:

(1) 點擊您想基於其進行合併的列,然後點擊 主鍵

A screenshot showing the primary key selection in Kutools for merging rows based on a column in Excel

(2) 點擊您想合併數據的列,然後點擊 合併,然後從列表中選擇所需的分隔符;

A screenshot showing the combination options in Kutools for merging rows based on one column

(3) 點擊您想求和的列,然後點擊 計算,並選擇 求和

A screenshot showing the summing options in Kutools for merging rows based on one column

3. 點擊 確定。現在您可以看到如下結果:

A screenshot showing the result of merging and performing multiple operations in Excel using Kutools

使用高級合併行功能,您還可以根據某一列合併行,然後對其他列進行計數、平均值、保留第一或最後數據等操作。

最佳辦公效率工具

🤖 Kutools AI 助手:基於智能執行方式革新數據分析:智能執行   |  生成代碼  |  創建自訂公式  |  分析數據並生成圖表  |  調用 Kutools 函數
熱門功能查找、標記重複值或識別重複項   |  刪除空行   |  合併列或單元格而不丟失數據   |   四捨五入無需公式 ...
高級 LOOKUP多條件 VLookup    多值 VLookup  |   多表查找   |   模糊查找 ....
高級下拉列表快速創建下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
列管理器添加特定數量的列  |  移動列  |  切換隱藏列的可見狀態  |  比較區域和列 ...
特色功能網格聚焦   |  設計檢視   |   增強編輯欄    工作簿與工作表管理器   |  資源庫(自動文本)   |  日期提取器   |  合併資料   |  加密/解密儲存格    按列表發送電子郵件   |  超級篩選   |   特殊篩選(篩選粗體/斜體/刪除線...) ...
頂級 15 種工具集12 個文本工具添加文本刪除特定字符、...)   |   50+ 圖表 類型甘特圖、...)   |   40+ 實用 公式基於生日計算年齡、...)   |   19 個插入工具插入QR碼根據路徑插入圖片、...)   |   12 個轉換工具金額轉大寫匯率轉換、...)   |   7 個合併與分割工具高級合併行分割儲存格、...)   |   ... 還有更多

使用 Kutools for Excel 提升您的 Excel 技巧,體驗前所未有的高效。 Kutools for Excel 提供超過 300 種高級功能來提高生產力並節省時間。  點擊這裡獲取您最需要的功能...


Office Tab 將標籤式界面帶到 Office,讓您的工作更加輕鬆

  • 在 Word、Excel、PowerPoint、Publisher、Access、Visio 和 Project 中啟用標籤式編輯和閱讀。
  • 在同一窗口的新標籤中打開和創建多個文檔,而不是在新窗口中。
  • 將您的生產力提高 50%,每天為您減少數百次鼠標點擊!