collection duplicate items based on column and summing values

KalilMe

Active Member
Joined
Mar 5, 2021
Messages
382
Office Version
  1. 2016
Platform
  1. Windows
hi
I need macro merge duplicate items for about 1000 rows . should merge based on column A and summing values for columns E,G
the data
columns.xlsm
ABCDEFG
1CODEBRTYORQTYPRICETOTAL
2fr001FRBANANATT200.0010.002,000.00
3fr002FRAPPLELL100.0054.005,400.00
4fr003VEGTOMATOSS12.0022.00264.00
5fr004VEGTOMATOAA12.0023.00276.00
6fr001FRBANANATT10.0032.00320.00
7fr002FRAPPLELL20.0010.00200.00
8fr007TUNA180GIND20.0023.00460.00
9fr008TUNA180GTHI10.0012.00120.00
OPER
Cell Formulas
RangeFormula
G2:G9G2=E2*F2



result
columns.xlsm
IJKLMN
1CODEBRTYORQTYTOTAL
2fr001FRBANANATT210.002,320.00
3fr002FRAPPLELL120.005,600.00
4fr003FRPEARNN12.00264.00
5fr004FRBANANAQQ12.00276.00
6fr007VEGTOMATOSS10.00460.00
7fr008VEGTOMATOAA20.00120.00
OPER
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
It would have been nice if your Result data was the same as your source data. For example "fr008" in the source is TUNA, 180G, THI, but in the result you have it listed as VEG, TOMATO, AA in those respective columns. That said, try this:

VBA Code:
Sub SumData()

    Dim dic As Object, dic2 As Object
    Dim Data, dki, dki2
    Dim r As Long, x As Long
    
    Data = Sheets("OPER").Range("A2", Sheets("OPER").Cells(Rows.Count, "G").End(xlUp))
    Set dic = CreateObject("Scripting.Dictionary")
    For r = 1 To UBound(Data)
        dic(Data(r, 1)) = dic(Data(r, 1)) + Data(r, 5) * Data(r, 6)
    Next
    Set dic2 = CreateObject("Scripting.Dictionary")
    For r = 1 To UBound(Data)
        dic2(Data(r, 1)) = dic2(Data(r, 1)) + Data(r, 5)
    Next
    dki = Application.Transpose(Array(dic.Keys, dic.Items))
    dki2 = Application.Transpose(Array(dic2.Keys, dic2.Items))
    ReDim Preserve dki(1 To UBound(dki), 1 To 6)

    For r = 1 To UBound(dki)
        For x = 1 To UBound(Data)
            If dki(r, 1) = Data(x, 1) Then
                dki(r, 6) = dki(r, 2)
                dki(r, 2) = Data(x, 2)
                dki(r, 3) = Data(x, 3)
                dki(r, 4) = Data(x, 4)
                dki(r, 5) = dki2(r, 2)
                Exit For
            End If
        Next
    Next
            
    Sheets("OPER").Range("I1:M1") = Array("CODE", "BR", "TY", "OR", "QTY", "TOTAL")
    Sheets("OPER").Range("I2").Resize(UBound(dki, 1), UBound(dki, 2)) = dki
End Sub
 
Upvote 0
magnificiant ! this is exactly what I want.(y)
my apologies about mistake fr008:eek:
just last thing how can I implement your code for multiple sheets . I don't come back here to issue new thread or ask somebody else mod your code
I would to be the code more dynamically .;)
note: your code doesn't also show the formatting and borders as is in column A: G
 
Last edited:
Upvote 0
I am glad it worked for you. Thanks for the feedback!

Not 100% sure what you are asking, If you want to use the code on a different sheet, change the sheet name "OPER" to what ever sheet you want to act on. You can do that in a variety of ways. Hard to say what would work best for your situation.
 
Upvote 0
Not 100% sure what you are asking
I'm asking about others sheets for instance sheets (rep,port,mort) like the same sheet OPER . it should be loop throught for multiple sheets not only one sheet .
 
Upvote 0
Depending on what the data looks like on the other sheets... If the other sheets were all in the same format for columns and totals as OPER, then you could create a loop to put all the sheets through the code, one at a time. That should not be difficult.

If they are all in the same format, and you supply the names of the other sheets, I can see if I can manage that.

Additionally, if you want Formatting and Borders, perhaps you could Google that and see what you come up with.?
 
Upvote 0
If the other sheets were all in the same format for columns and totals as OPER, then you could create a loop to put all the sheets through the code, one at a time
can you show me how ? like put the sheets in array ("rep","port","mort").
 
Upvote 0
See if this does what you want. You are going back and forth between all caps and small case for the sheet names so I put the UCase function in to compensate for that. Also, if you want to add more sheet names, I indicated where in the code...

VBA Code:
Sub SumData2()

    Dim dic As Object, dic2 As Object
    Dim Data, dki, dki2, Snam
    Dim r As Long, x As Long, n As Long
    
    Application.ScreenUpdating = False
    Snam = Array("OPER", "rep", "port", "mort")     '*** Add more sheet names here ***
    For n = 0 To UBound(Snam)
        Data = Sheets(UCase(Snam(n))).Range("A2", Sheets(UCase(Snam(n))).Cells(Rows.Count, "G").End(xlUp))
        Set dic = CreateObject("Scripting.Dictionary")
        For r = 1 To UBound(Data)
            dic(Data(r, 1)) = dic(Data(r, 1)) + Data(r, 5) * Data(r, 6)
        Next
        Set dic2 = CreateObject("Scripting.Dictionary")
        For r = 1 To UBound(Data)
            dic2(Data(r, 1)) = dic2(Data(r, 1)) + Data(r, 5)
        Next
        dki = Application.Transpose(Array(dic.Keys, dic.Items))
        dki2 = Application.Transpose(Array(dic2.Keys, dic2.Items))
        ReDim Preserve dki(1 To UBound(dki), 1 To 6)
        For r = 1 To UBound(dki)
            For x = 1 To UBound(Data)
                If dki(r, 1) = Data(x, 1) Then
                    dki(r, 6) = dki(r, 2)
                    dki(r, 2) = Data(x, 2)
                    dki(r, 3) = Data(x, 3)
                    dki(r, 4) = Data(x, 4)
                    dki(r, 5) = dki2(r, 2)
                    Exit For
                End If
            Next
        Next
        Sheets(UCase(Snam(n))).Range("I1:M1") = Array("CODE", "BR", "TY", "OR", "QTY", "TOTAL")
        Sheets(UCase(Snam(n))).Range("I2").Resize(UBound(dki, 1), UBound(dki, 2)) = dki
        dic.RemoveAll: dic2.RemoveAll: Erase Data: Erase dki: Erase dki2
    Next
    Application.ScreenUpdating = True
    MsgBox "Operation Complete"
    
End Sub
 
Upvote 0
Solution
You are welcome, I was happy to help. Thanks again for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,269
Members
452,628
Latest member
dd2

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