Summarise data by VBA

Panoos64

Well-known Member
Joined
Mar 1, 2014
Messages
882
Hi to all of you! I would like to alter my below data by VBA code so that, where the columns “B” and “D” have common data (code & description), should “SUM” the columns E” and “F” by the recent date (the newest). Note that the original data contains 20000+ rows and I present below an extract of my data by which at 1st are original data and 2nd is the expected result. Thank you all in advance

1st Original data
1620301119053.png


2nd Expected result

1620301154927.png


 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi bobsan, i work with Office 2016. Now i recognized that i have to use PowerQuery. I was thinking that do alter my data using PivotTable. Ok thank you for this clarification. it was so important for me!!
 
Upvote 0
How about this?

Book1
ABCDEFGHIJKLMN
1DateCodeInvoiceDescriptionAmountPaym/AdjustBalanceDateCodeInvoiceDescriptionAmountPaym/Adjust
22/17/2021121405356185Cans Beer 33cl455.183/18/2021121405556899Cans Beer 33cl600.9667.14
32/20/20211215181245897Refeshments Cans 33 cl351.363/15/20211215181260018Refeshments Cans 33 cl475.9115.45
42/22/20211216557824Lemonade Squash 75 cl85.362/22/20211216557824Lemonade Squash 75 cl85.360
53/1/2021121405458124Cans Beer 33cl145.78
63/10/20211215181254821Refeshments Cans 33 cl124.55
73/15/20211215181260018Refeshments Cans 33 cl15.45
83/17/2021121405556145Cans Beer 33cl41.36
93/18/2021121405556899Cans Beer 33cl25.78
Sheet2


VBA Code:
Sub NPQ()
Dim SD As Object:       Set SD = CreateObject("Scripting.Dictionary")
Dim AR() As Variant:    AR = Range("A2:G" & Range("A" & Rows.Count).End(xlUp).Row).Value2
Dim AL As Object:       Set AL = CreateObject("System.Collections.ArrayList")
Dim DT(1 To 5) As Variant

For i = 1 To UBound(AR)
    If Not SD.exists(AR(i, 2)) Then SD.Add AR(i, 2), DT
    TMP = SD(AR(i, 2))
    TMP(1) = TMP(1) + AR(i, 5)
    TMP(2) = TMP(2) + AR(i, 6)
    If AR(i, 1) > TMP(3) Then TMP(3) = AR(i, 1)
    If AR(i, 3) > TMP(4) Then TMP(4) = AR(i, 3)
    TMP(5) = AR(i, 4)
    SD(AR(i, 2)) = TMP
Next i

For Each k In SD.keys
    TMP = SD(k)
    AL.Add Join(Array(TMP(3), k, TMP(4), TMP(5), TMP(1), TMP(2)), ";")
Next k

With Range("I2").Resize(AL.Count)
    .Value = Application.Transpose(AL.toArray)
    .TextToColumns DataType:=xlDelimited, Semicolon:=True
End With

End Sub
 
Upvote 0
Solution
Thank you Irobbo! it works perfect and based on my data. Is exactly what i was looking and till now i couldn't arrange my data. Using now your code i work easier and accurate. Thank you also for your time spent for my project. Hv a great, lovely day!!
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,580
Members
449,039
Latest member
Arbind kumar

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