Loop cell value in column then insert value to table with accumulate amount (Scripting Dictionary)

SamKhem

Board Regular
Joined
Mar 18, 2024
Messages
50
Office Version
  1. 2016
Platform
  1. Windows
Dear Senior member

I would like request you to provide code run vba excel with around 100,000 rows as example: insert (List) to original table then accumulate amount as below.

Book2
ABCDEFGHIJKLMN
1ORIGINAL TABLEListRESULT
2CODETYPEAMOUNTCODEAMOUNTCODETYPEAMOUNTACCUMULATE
3100100584296001EUR-10010010058429254,100.0010010058429254,100.00
4100100594226001USD-103325100100594222,800.00100100584296001EUR-100254,000.00
5100100616246001KHR100000100100616242,276,200.00100100594222,800.00
6100100616246001KHR-14500010010062117290,608.00100100594226001USD-103325(100,525.00)
7100100616246001EUR-50000010010058428359,100.00100100616242,276,200.00
8100100621176001EUR-600010010058426151,044.00100100616246001KHR1000002,376,200.00
9100100621176001EUR2063001001005842326,800.00100100616246001KHR-1450002,231,200.00
10100100621176001EUR-200000100100616246001EUR-5000001,731,200.00
11100100621176001EUR-19390010010062117290,608.00
12100100621176001EUR-290900100100621176001EUR-6000284,608.00
13100100584286001EUR-387900100100621176001EUR206300490,908.00
14100100584286001EUR-484900100100621176001EUR-200000290,908.00
15100100584266001CYN-581900100100621176001EUR-19390097,008.00
16100100584266001CYN-678900100100621176001EUR-290900(193,892.00)
17100100584266001CYN-77590010010058428359,100.00
18100100584236001CYN-872900100100584286001EUR-387900(28,800.00)
19100100584236001CYN-969900100100584286001EUR-484900(513,700.00)
20100100584236001CYN-106690010010058426151,044.00
21100100584236001CYN-1163900100100584266001CYN-581900(430,856.00)
22100100584266001CYN-678900(1,109,756.00)
23100100584266001CYN-775900(1,885,656.00)
241001005842326,800.00
25100100584236001CYN-872900(846,100.00)
26100100584236001CYN-969900(1,816,000.00)
27100100584236001CYN-1066900(2,882,900.00)
28100100584236001CYN-1163900(4,046,800.00)
Sheet1
Cell Formulas
RangeFormula
N4,N25:N28,N21:N23,N18:N19,N12:N16,N8:N10,N6N4=N3+L4


Thank in advance for your assist.
Kindly regards,
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
1. i saw that some code has different type, can you calculate accumulate with that?
2. data in list is manual insert?
3. do you really need to show all amount fluctuations for each code in result table? why not accumulate all and show in one result for each code?
 
Upvote 0
1. i saw that some code has different type, can you calculate accumulate with that?
2. data in list is manual insert?
3. do you really need to show all amount fluctuations for each code in result table? why not accumulate all and show in one result for each code?
1. Yes, so please ignore type. focus on code as base.
2. Yes I try to quick manual forgot check.
3. I need to accumulate amount by each code as first amount came from list then it accumulate original table.
 
Upvote 0
One way (Not using dictionary)
VBA Code:
Option Explicit
Sub test()
Dim lr&, i&, j&, k&, rng, rng2, res(1 To 10000, 1 To 6)
Dim cell As Range
rng2 = Range("F3:G" & Cells(Rows.Count, "F").End(xlUp).Row).Value
rng = Range("A3:D" & Cells(Rows.Count, "A").End(xlUp).Row).Value
For i = 1 To UBound(rng2)
    k = k + 1: res(k, 5) = rng2(i, 1): res(k, 6) = rng2(i, 2)
    For j = 1 To UBound(rng)
        If rng(j, 1) = rng2(i, 1) Then
            k = k + 1: res(k, 1) = rng(j, 1): res(k, 2) = rng(j, 2)
            res(k, 3) = rng(j, 3): res(k, 4) = rng(j, 4): res(k, 6) = res(k - 1, 6) + res(k, 4)
        End If
    Next
Next
If k = 0 Then Exit Sub
Range("I3:N10000").ClearContents
Range("I3").Resize(k, 6).Value = res
End Sub
 
Upvote 0
Solution
One way (Not using dictionary)
VBA Code:
Option Explicit
Sub test()
Dim lr&, i&, j&, k&, rng, rng2, res(1 To 10000, 1 To 6)
Dim cell As Range
rng2 = Range("F3:G" & Cells(Rows.Count, "F").End(xlUp).Row).Value
rng = Range("A3:D" & Cells(Rows.Count, "A").End(xlUp).Row).Value
For i = 1 To UBound(rng2)
    k = k + 1: res(k, 5) = rng2(i, 1): res(k, 6) = rng2(i, 2)
    For j = 1 To UBound(rng)
        If rng(j, 1) = rng2(i, 1) Then
            k = k + 1: res(k, 1) = rng(j, 1): res(k, 2) = rng(j, 2)
            res(k, 3) = rng(j, 3): res(k, 4) = rng(j, 4): res(k, 6) = res(k - 1, 6) + res(k, 4)
        End If
    Next
Next
If k = 0 Then Exit Sub
Range("I3:N10000").ClearContents
Range("I3").Resize(k, 6).Value = res
End Sub
Perfect worked.
Thank for support. Have a nice day.
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,107
Members
452,302
Latest member
TaMere

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