VBA Sum Multiple Columns and Duplicate Rows

drawde07

New Member
Joined
Jul 12, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi! I am new here and just started learning vba codes.

I was wondering, I have searched alot but Could not find a specific code where I would be able to sum the A column of the duplicates of B to D columns and deleting the duplicates.

The original Data would be like
QTY UOM ITEMS BRAND
60 Meters 8.0mm wire - Red Duraflex
2 Boxes Grinding Disc Stanley
1 Kilos Item 3 Brand 3
5 Meters Grinding Disc Brand 3
-10 Meters 8.0mm wire - Red Duraflex

And after using the Code it would be
50 Meters 8.0mm wire - Red Duraflex
2 Boxes Grinding Disc Stanley
1 Kilos Item 3 Brand 3
5 Meters Grinding Disc Brand 3

I found different codes but all of the ones that would be summed will be at the right and only used 2/3 columns , I cannot find where it would be left and many columns.
Thank you!

1657681566338.png
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
With data from A1:D1 downwards.
Copy this code into an available module (Alt-F11, insert/modules)
VBA Code:
Option Explicit
Sub test()
Dim lr&, i&, rng, id As String, arr()
Dim dic As Object, key
Set dic = CreateObject("Scripting.dictionary")
lr = Cells(Rows.Count, "D").End(xlUp).Row
rng = Range("A2:D" & lr).Value
For i = 1 To lr - 1
    id = rng(i, 2) & "|" & rng(i, 3) & "|" & rng(i, 4)
    If Not dic.exists(id) Then
        dic.Add id, rng(i, 1)
    Else
        dic(id) = dic(id) + rng(i, 1)
    End If
Next
i = 0
ReDim arr(1 To dic.Count, 1 To 4)
For Each key In dic.keys
    i = i + 1
    arr(i, 1) = dic(key)
    arr(i, 2) = Split(key, "|")(0)
    arr(i, 3) = Split(key, "|")(1)
    arr(i, 4) = Split(key, "|")(2)
Next
Range("A2:D10000").ClearContents
Range("A2").Resize(UBound(arr), 4).Value = arr
End Sub

Capture.JPG
 
Upvote 0
Solution
Thanks alot!
This helped me by 100000x!
Really Appreciated
With data from A1:D1 downwards.
Copy this code into an available module (Alt-F11, insert/modules)
VBA Code:
Option Explicit
Sub test()
Dim lr&, i&, rng, id As String, arr()
Dim dic As Object, key
Set dic = CreateObject("Scripting.dictionary")
lr = Cells(Rows.Count, "D").End(xlUp).Row
rng = Range("A2:D" & lr).Value
For i = 1 To lr - 1
    id = rng(i, 2) & "|" & rng(i, 3) & "|" & rng(i, 4)
    If Not dic.exists(id) Then
        dic.Add id, rng(i, 1)
    Else
        dic(id) = dic(id) + rng(i, 1)
    End If
Next
i = 0
ReDim arr(1 To dic.Count, 1 To 4)
For Each key In dic.keys
    i = i + 1
    arr(i, 1) = dic(key)
    arr(i, 2) = Split(key, "|")(0)
    arr(i, 3) = Split(key, "|")(1)
    arr(i, 4) = Split(key, "|")(2)
Next
Range("A2:D10000").ClearContents
Range("A2").Resize(UBound(arr), 4).Value = arr
End Sub

View attachment 69174
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,182
Members
452,615
Latest member
bogeys2birdies

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