This post will teach you how to combine duplicate rows and sum the corresponding values or calculate numbers in specific column in excel. And how to merge duplicate rows and then sum the values with VBA macro in Excel. How to combine rows with same name and sum the value.
Table of Contents
1. Combine Duplicate Rows and Sum the Values with Consolidate Feature
You can use the Consolidate feature to combine duplicate rows and then sum the values in excel, let’s see the below steps:
Step1: select a cell that you want to display the result combined
![combine duplicate rows1](https://www.excelhow.net/wp-content/uploads/2018/03/combine-duplicate-rows1.jpg)
Step2: on the DATA tab, click Consolidate command under Data Tools group.
![combine duplicate rows2](https://www.excelhow.net/wp-content/uploads/2018/03/combine-duplicate-rows2.jpg)
Step3: the Consolidate window will appear.
Step4: choose Sum from Function: drop-down list, select the range that you want to combine, then click Add button to add it in the All references box. Select Top row and Left column checkbox. Then click OK button.
![combine duplicate rows3](https://www.excelhow.net/wp-content/uploads/2018/03/combine-duplicate-rows3.jpg)
Step5: you will see that all duplicate rows are combined and the corresponding values summed.
![combine duplicate rows4](https://www.excelhow.net/wp-content/uploads/2018/03/combine-duplicate-rows4.jpg)
2. Combine Duplicate Rows and Sum the Values with VBA code
You can also combine duplicate rows and sum the values with VBA code in Excel. Just do the following:
Step1: click on “Visual Basic” command under DEVELOPER Tab.
![Get the position of the nth using excel vba1](https://www.excelhow.net/wp-content/uploads/2017/11/Get-the-position-of-the-nth-using-excel-vba1.jpg)
Step2: then the “Visual Basic Editor” window will appear.
Step3: In the VBE, click on Insert from the top menu and select Module to create a new module.
![convert column number to letter3](https://www.excelhow.net/wp-content/uploads/2017/12/convert-column-number-to-letter3.jpg)
Step4: paste the below VBA code into the code window. Then clicking “Save” button.
![Combine Duplicate Rows and Sum the Values vba 1.png](https://www.excelhow.net/wp-content/uploads/2023/04/Combine-Duplicate-Rows-and-Sum-the-Values-vba-1.png)
Sub CombineDuplicateRowsAndSum_ExcelHow()
Dim R As Range, OutputRange As Range
Set R = Application.Selection
Set R = Application.InputBox("Select one range:", "CombineDuplicateRowsAndSum", R.Address, Type:=8)
' Add prompt to select range for output
Set OutputRange = Application.InputBox("Select range for output:", "CombineDuplicateRowsAndSum", Type:=8)
Dim Dic As Object
Set Dic = CreateObject("Scripting.Dictionary")
Dim arr As Variant
arr = R.Value
Dim i As Long
For i = 1 To UBound(arr, 1)
Dic(arr(i, 1)) = Dic(arr(i, 1)) + arr(i, 2)
Next
OutputRange.Range("A1").Resize(Dic.Count, 1) = Application.WorksheetFunction.Transpose(Dic.keys)
OutputRange.Range("B1").Resize(Dic.Count, 1) = Application.WorksheetFunction.Transpose(Dic.items)
Application.ScreenUpdating = True
End Sub
Step5: back to the current worksheet, then select the CombineDuplicateRowsAndSum_ExcelHow macro and run the above excel macro.
![combine duplicate rows6](https://www.excelhow.net/wp-content/uploads/2018/03/combine-duplicate-rows6.jpg)
Step6: Follow the prompts to select the source range and output range for the processed data. Click OK to start processing the data.
![Combine Duplicate Rows and Sum the Values vba 3.png](https://www.excelhow.net/wp-content/uploads/2023/04/Combine-Duplicate-Rows-and-Sum-the-Values-vba-3.png)
![Combine Duplicate Rows and Sum the Values vba 4.png](https://www.excelhow.net/wp-content/uploads/2023/04/Combine-Duplicate-Rows-and-Sum-the-Values-vba-4.png)
Step7: The processed data will be displayed in the output range you specified.
![combine duplicate rows7](https://www.excelhow.net/wp-content/uploads/2018/03/combine-duplicate-rows7.jpg)
3. Video: Combine Duplicate Rows and Sum the Values
This video will demonstrate how to combine duplicate rows and sum the values in Excel using both the Consolidate feature and VBA code.
Leave a Reply
You must be logged in to post a comment.