How to Combine Duplicate Rows and Sum the Values

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.

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

Step2: on the DATA tab, click Consolidate command under Data Tools group.

combine duplicate rows2

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

Step5: you will see that all duplicate rows are combined and the corresponding values summed.

combine duplicate rows4

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

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

Step4: paste the below VBA code into the code window. Then clicking “Save” button.

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

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
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

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