Locate duplicates and sum the two columns with different values

ajcarlos18

New Member
Joined
Sep 4, 2017
Messages
15
Hi,

Need help to add / alter the code. Need to sum the values on column 4 & 5 by locating duplication on column 1. Say for an example:

[TABLE="width: 500"]
<tbody>[TR]
[TD]Consumables[/TD]
[TD]31222[/TD]
[TD]NY[/TD]
[TD][TABLE="width: 167"]
<tbody>[TR]
[TD]698.2[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD]77.6[/TD]
[/TR]
[TR]
[TD]Consumables[/TD]
[TD]31222[/TD]
[TD]NY[/TD]
[TD]123.4[/TD]
[TD]445.9[/TD]
[/TR]
[TR]
[TD]Consumables[/TD]
[TD]31222[/TD]
[TD]NY[/TD]
[TD]554.21[/TD]
[TD]23.5[/TD]
[/TR]
[TR]
[TD]Spares[/TD]
[TD]31119[/TD]
[TD]NY[/TD]
[TD]33.4[/TD]
[TD]87.0[/TD]
[/TR]
[TR]
[TD]Spares[/TD]
[TD]31119[/TD]
[TD]NY[/TD]
[TD]55.7[/TD]
[TD]23.9[/TD]
[/TR]
</tbody>[/TABLE]

Output:

[TABLE="width: 500"]
<tbody>[TR]
[TD]Consumables[/TD]
[TD]31222[/TD]
[TD]NY[/TD]
[TD]1375.81[/TD]
[TD]547[/TD]
[/TR]
[TR]
[TD]Spares[/TD]
[TD]31119[/TD]
[TD]NY[/TD]
[TD]89.1[/TD]
[TD]110.9[/TD]
[/TR]
</tbody>[/TABLE]



Code:

Sub Finaladd()


Dim Rng As Range, Dn As Range, n As Long, nRng As Range


Sheet17.Select
Sheet17.Range("A1").Select


Set Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
With CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare


Sheet17.Select
Sheet17.Range("A1").Select


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

Thank you very much for your help
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
I am not sure I see the problem as this works
Code:
Sub Finaladd()
Dim Rng As Range, Dn As Range, n As Long, nRng As Range
Dim NewID As Boolean


Set Rng = Range(Range("A2"), Range("A" & 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
 
Upvote 0
Thanks for the reply. The code sums up column D only. What I need is for both column D & E to be summed up. Maybe I lack the code to include column E in the summation.
 
Upvote 0
You could try

Code:
Sub FinalAdd2()
Dim Ast As Long, AEnd As Long, Bst As Long, BEnd As Long
Dim ACnt As Long, BCnt As ILong


Ast = 2
AEnd = Range("A1048576").End(xlUp).Row
ACnt = Ast
BCnt = Ast + 1




Do While ACnt <= AEnd
    Do While BCnt <= AEnd
        If Range("A" & ACnt).Value = Range("A" & BCnt).Value Then
            Range("D" & ACnt).Value = Range("D" & ACnt).Value + Range("D" & BCnt).Value
            Range("E" & ACnt).Value = Range("E" & ACnt).Value + Range("E" & BCnt).Value
            Rows(BCnt & ":" & BCnt).Delete xlShiftUp
            AEnd = AEnd - 1
        Else
            BCnt = BCnt + 1
        End If
    Loop
    ACnt = ACnt + 1
    BCnt = ACnt + 1
Loop
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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