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.

You can use the Consolidate feature to combine duplicate rows and then sum the values in excel, let’s see the below steps:

1# select a cell that you want to display the result combined

combine duplicate rows1

2# on the DATA tab, click Consolidate command under Data Tools group.

combine duplicate rows2

3# the Consolidate window will appear.

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

5# you will see that all duplicate rows are combined and the corresponding values summed.

combine duplicate rows4

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:

1# click on “Visual Basic” command under DEVELOPER Tab.

Get the position of the nth using excel vba1

2# then the “Visual Basic Editor” window will appear.

3# click “Insert” ->”Module” to create a new module

convert column number to letter3

4# paste the below VBA code into the code window. Then clicking “Save” button.

combine duplicate rows5

Sub CombineDuplicateRowsAndSum()
    Dim R As Range
    Dim Dic As Variant
    Dim arr As Variant
    On Error Resume Next
    xTitleId = "Combining Duplicate rows and sum values"
    Set R = Application.Selection
    Set R = Application.InputBox("Range", xTitleId, R.Address, Type:=8)
    Set Dic = CreateObject("Scripting.Dictionary")
    arr = R.Value
    For i = 1 To UBound(arr, 1)
        Dic(arr(i, 1)) = Dic(arr(i, 1)) + arr(i, 2)
    Next
    Application.ScreenUpdating = False
    R.ClearContents
    R.Range("A1").Resize(Dic.Count, 1) = Application.WorksheetFunction.Transpose(Dic.keys)
    R.Range("B1").Resize(Dic.Count, 1) = Application.WorksheetFunction.Transpose(Dic.items)
    Application.ScreenUpdating = True
End Sub

5# back to the current worksheet, then run the above excel macro.

combine duplicate rows6

combine duplicate rows7

Comments

So empty here ... leave a comment!

Leave a Reply

Your email address will not be published. Required fields are marked *

Sidebar