Excel VBA Merge Duplicate rows and sum values in certain column

skyking022

New Member
Joined
May 9, 2019
Messages
1
Hi, my data is as below:
Sample Data:
A B C Result:
DG M 1 DG M 5
DG M 2 KH M 9
DG M 2 SG C 7
KH M 4 KH M 5
KH M 5 DG M 5
SG C 6
SG C 1
KH M 3
KH M 2
DG M 5

I got 3 column here, and I wish to sum up the value if rows in column A and B is the same with previous row.

Below is the code I refer from MickG. But the code seem to have only one criteria, I would like to seek a way to add another criteria.Thank you.

Sub MG()

Dim Rng As Range, Dn As Range, n As Double, nRng As Range
Set Rng = Worksheets("sheet1").Range(Range("B2"), Range("B" & Rows.Count).End(xlUp))
With CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
For Each Dn In Rng
If Not .Exists(Dn.Value) Then
.Add Dn.Value, Dn
Else
If nRng Is Nothing Then Set nRng = Dn Else Set nRng = Union(nRng, Dn)
.Item(Dn.Value).Offset(, 3) = .Item(Dn.Value).Offset(, 3) + Dn.Offset(, 3)
End If
Next
If Not nRng Is Nothing Then nRng.EntireRow.Delete
End With
End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try this. The results will be in columns E, F and G

Code:
Sub sum_values()
    Dim ant As String, i As Long, dat As Variant, acum As Long
    ant = Cells(2, "A") & "|" & Cells(2, "B")
    For i = 2 To Range("A" & Rows.Count).End(xlUp).Row + 1
        If ant <> Cells(i, "A") & "|" & Cells(i, "B") Then
            dat = Split(ant, "|")
            Range("E" & Rows.Count).End(xlUp)(2).Resize(1, 3).Value = Array(dat(0), dat(1), acum)
            acum = 0
        End If
        acum = acum + Cells(i, "C").Value
        ant = Cells(i, "A") & "|" & Cells(i, "B")
    Next
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,326
Messages
6,124,270
Members
449,149
Latest member
mwdbActuary

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top