How to combine duplicate rows and sum the values in Excel?
In Excel，you may always meet this problem, when you have a range of data which contains some duplicate entries, and now you want to combine the duplicate data and sum the corresponding values in another column, as following screenshots shown. How could you solve this problem?
Combine duplicate rows and sum the values with Consolidate function
The Consolidate is a useful tool for us to consolidate multiple worksheets or rows in Excel, so with this function, we can also summarize multiple rows based on the duplicates. Please do with the following steps:
1. Click a cell where you want to locate the result in your current worksheet.
2. Go to click Data > Consolidate, see screenshot:
3. In the Consolidate dialog box:
(1.) Select Sum from Function drop down list;
(2.) Click button to select the range that you want to consolidate, and then click Add button to add the reference to All references list box;
(3.) Check Top row and Left column from Use labels in option. See screenshot:
4. After finishing the settings, click OK, and the duplicates are combined and summed. See screenshot:
Note: If the range do not have the header row, you need uncheck Top row from the Use labels in option.
Combine duplicate rows and sum / average corresponding values in another column
Kutools for Excel's Advanced Combibe Rows helps you to combine multiple duplicate rows into one record based on a key column, and it also can apply some calculations such as sum, average, count and so on for other columns. Click to download Kutools for Excel!
Combine duplicate rows and sum the values with VBA code
The following VBA code also can help you consolidate duplicate rows in the worksheet, but at the same time, the original data will be destroyed, you need to backup a copy of the data.
1. Hold down the ALT + F11 keys, then it opens the Microsoft Visual Basic for Applications window.
2. Click Insert > Module, and paste the following code in the Module Window.
VBA code: Combine duplicate rows and sum the values
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)
Dic(arr(i, 1)) = Dic(arr(i, 1)) + arr(i, 2)
Application.ScreenUpdating = False
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
3. Then press F5 key to run this code, and you need to select the range that you want to consolidate in the popped out prompt box. See screenshot:
4. And then click OK, the duplicate rows have been combined and the values are added up.
Note: If you want to use this code, you’d better make a copy of the file to avoid destroying the data, and this code is only applied to two columns.
Combine duplicate rows and sum the values with Kutools for Excel
Here, I introduce the handy tool -- Kutools for Excel for you, its Advanced Combine Rows also can quickly do a favor to solve this problem.
Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
More than 300 powerful features. Supports Office/Excel
2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features
30-day free trial. 60-day money back guarantee.
Your first example is pretty good but it is missing one step. Prior to clicking OK the cursor needs to be in a blank area for the consolidation to export. If you have not selected a blank cell area after adding the range you will get a "source overlaps destination" error.