VBA Dictionary / Collection help for Sumifs Formula...

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
983
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

I have below Data and its expected output, Currently I am getting the result via Sumifs formula.
But I want answer using vba dictionary or Collections help to achieve same result.

Input Data is column (A:F)
Expected Output is in Column Range("H2:H5) and
Criteria is in Range("H13:J15)"

Step 1 Extract Unique value from Column "A" paste horizontally in Range("I2") onward(transpose it)
Step 2, Apply sumifs formula, Take Sum those columns which is mentioned infront of Cost Center.

Uploaded Screenshot as well.
Range("I3") FORMULA =SUMIFS($B$2:$B$20,$A$2:$A$20,I$2)+SUMIFS($C$2:$C$20,$A$2:$A$20,I$2)
Range("I4") FORMULA =SUMIFS($B$2:$B$20,$A$2:$A$20,I$2)+SUMIFS($C$2:$C$20,$A$2:$A$20,I$2)
Range("I5") FORMULA =SUMIFS($F$2:$F$20,$A$2:$A$20,I$2)


I understand vba code, I will modify as per my requirement later.

Cost Center DescriptionBasicArr BasicCity Compensatory AllowanArr CCAH.R.A.Cost Center Unique values horizontally Pasted
20009
111392.00​
657.00​
12353.00
201.00​
44557.00​
Cost Center Description20009200002000320006
20000
73190.00​
584.00​
19265.00
160.00​
29276.00​
Basic
126620.00​
120639.00​
128036.00​
67594.00​
20003
91846.00​
595.00​
17111.00
645.00​
36739.00​
City Compensatory Allowan
24940.00​
54786.00​
29697.00​
31091.00​
20003
35146.00​
449.00​
11662.00
279.00​
14059.00​
H.R.A.
50157.00​
47720.00​
50798.00​
26871.00​
20006
16369.00​
197.00​
18970.00
68.00​
6548.00​
20005
29910.00​
174.00​
12230.00
126.00​
11964.00​
=SUMIFS($B$2:$B$20,$A$2:$A$20,I$2)+SUMIFS($C$2:$C$20,$A$2:$A$20,I$2)
20000
19095.00​
244.00​
18782.00
839.00​
7638.00​
=SUMIFS($D$2:$D$20,$A$2:$A$20,I$2)+SUMIFS($E$2:$E$20,$A$2:$A$20,I$2)
20007
39674.00​
528.00​
11525.00
578.00​
15870.00​
=SUMIFS($F$2:$F$20,$A$2:$A$20,I$2)
20001
13067.00​
605.00​
18795.00
103.00​
4303.00​
20002
35564.00​
395.00​
15402.00
789.00​
14226.00​
Criteria to Sum
20006
50807.00​
221.00​
11560.00
493.00​
20323.00​
Cost Center DescriptionTake sum of headers
20008
42818.00​
634.00​
11077.00
207.00​
17127.00​
BasicBasicArr Basic
20005
45113.00​
412.00​
18777.00
316.00​
18046.00​
City Compensatory AllowanCity Compensatory AllowanArr CCA
20004
86064.00​
621.00​
12822.00
404.00​
34426.00​
H.R.A.H.R.A.
20008
16388.00​
300.00​
13432.00
444.00​
6555.00​
20001
14000.00​
495.00​
15577.00
673.00​
5600.00​
20009
14000.00​
571.00​
11678.00
708.00​
5600.00​
20001
33419.00​
213.00​
12360.00
198.00​
13368.00​
20000
27014.00​
512.00​
15505.00
235.00​
10806.00​

I
Thanks in advance for your help!

Regards,
mg
 

Attachments

  • Input Data Screenshot -- Using Sumifs.PNG
    Input Data Screenshot -- Using Sumifs.PNG
    51.4 KB · Views: 34

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi Mallesh23,

Try the below code

VBA Code:
Sub test()

Dim a, b, aa$, bb$, cc$
a = [A1].CurrentRegion

With CreateObject("scripting.dictionary")
    For x = 2 To UBound(a)
        If Not .exists(a(x, 1)) Then
            .Add a(x, 1), Join(Array(a(x, 2) + a(x, 3), a(x, 4) + a(x, 5), a(x, 6)), "|")
        Else
            aa = a(x, 2) + a(x, 3) + Split(.Item(a(x, 1)), "|")(0)
            bb = a(x, 4) + a(x, 5) + Split(.Item(a(x, 1)), "|")(1)
            cc = a(x, 6) + Split(.Item(a(x, 1)), "|")(2)
            .Item(a(x, 1)) = Join(Array(aa, bb, cc), "|")
        End If
    Next
    ReDim b(1 To .Count, 1 To 4)
    For Each k In .keys
        i = i + 1
        b(i, 1) = k: b(i, 2) = Split(.Item(k), "|")(0)
        b(i, 3) = Split(.Item(k), "|")(1): b(i, 4) = Split(.Item(k), "|")(2)
    Next
End With

[H2].Resize(4) = [{"Cost Center Description";"Basic";"City Compensatory Allowan";"H.R.A."}]
[I2].Resize(UBound(b, 2), UBound(b)) = Application.Transpose(b)

End Sub
 
Upvote 0
So assuming your data in A2 onwards. The results in I2 onwards:

Dante Amor
ABCDEFIJKLMNOPQR
1CostBasicArr BasicCityArr CCAH.R.A.Cost
220009111392.00657.0012353.00201.0044557.0020009200002000320006200052000720001200022000820004
32000073190.00584.0019265.00160.0029276.00126620.00120639.00128036.0067594.0075609.0040202.0061799.0035959.0060140.0086685.00
42000391846.00595.0017111.00645.0036739.0024940.0054786.0029697.0031091.0031449.0012103.0047706.0016191.0025160.0013226.00
52000335146.00449.0011662.00279.0014059.0050157.0047720.0050798.0026871.0030010.0015870.0023271.0014226.0023682.0034426.00
62000616369.00197.0018970.0068.006548.00
72000529910.00174.0012230.00126.0011964.00
82000019095.00244.0018782.00839.007638.00
92000739674.00528.0011525.00578.0015870.00
102000113067.00605.0018795.00103.004303.00
112000235564.00395.0015402.00789.0014226.00
122000650807.00221.0011560.00493.0020323.00
132000842818.00634.0011077.00207.0017127.00
142000545113.00412.0018777.00316.0018046.00
152000486064.00621.0012822.00404.0034426.00
162000816388.00300.0013432.00444.006555.00
172000114000.00495.0015577.00673.005600.00
182000914000.00571.0011678.00708.005600.00
192000133419.00213.0012360.00198.0013368.00
202000027014.00512.0015505.00235.0010806.00
Hoja6



Try:
VBA Code:
Sub VBA_Sumifs()
  Dim a As Variant, b As Variant, dic As Object
  Dim i As Long, lr As Long, m As Long, n As Long, col As Long
  '
  lr = Range("A" & Rows.Count).End(3).Row
  n = Evaluate("=SUM(IF(FREQUENCY(MATCH(A2:A" & lr & ",A2:A" & lr & ",0)," & _
                                 "MATCH(A2:A" & lr & ",A2:A" & lr & ",0))>0,1))")
  Set dic = CreateObject("Scripting.Dictionary")
  a = Range("A2:F" & lr).Value2
  ReDim b(1 To 3, 1 To n)
  '
  For i = 1 To UBound(a)
    If Not dic.exists(a(i, 1)) Then
      m = m + 1
      dic(a(i, 1)) = m
      col = m
    Else
      col = dic(a(i, 1))
    End If
    b(1, col) = b(1, col) + a(i, 2) + a(i, 3)
    b(2, col) = b(2, col) + a(i, 4) + a(i, 5)
    b(3, col) = b(3, col) + a(i, 6)
  Next
  '
  Range("I2").Resize(1, dic.Count).Value = dic.keys
  Range("I3").Resize(3, n).Value = b
End Sub
 
Upvote 0
Hi mse300 and Danteamor,

Thank you both of you for your help on this.
Danteamor your code is working fine giving expected result.
To sum Basic, Macro to find Which columns to sum in Criteria List. and find those columns and sum only those columns.


b(1, col) = b(1, col) + a(i, 2) + a(i, 3)
= b(1, col) = b(1, col) + a(i, Col_Basics) + a(i, Col_Arr Basics) something like this.


I am ok with normal loop also and if conditions. Thanks.



Criteria:-
Cost DescriptionColumnsColumns
BasicBasicArr Basic
City AllowanceCityArr CCA
HRAH.R.A.


Thanks in advance
Regards,
mg
 
Last edited:
Upvote 0
Hi Mallesh23,

Thanks for the feedback ... I think you could achieve the same in a simpler way using your own formulas which would also be easier for you to miniplate at a later stage if required. Just one more way if interested, let us know if you have any question

VBA Code:
Sub RetainFormulas()

Dim a: a = [A1].CurrentRegion

With CreateObject("scripting.dictionary")
    For x = 2 To UBound(a)
        If Not .exists(a(x, 1)) Then .Add a(x, 1), Nothing
    Next
    [I2].Resize(, .Count) = .keys
    [I3].Resize(, .Count) = Replace("=SUMIFS($B$2:$B$@,$A$2:$A$@,I$2)+SUMIFS($C$2:$C$@,$A$2:$A$@,I$2)", "@", UBound(a))
    [I4].Resize(, .Count) = Replace("=SUMIFS($D$2:$D$@,$A$2:$A$@,I$2)+SUMIFS($E$2:$E$@,$A$2:$A$@,I$2)", "@", UBound(a))
    [I5].Resize(, .Count) = Replace("=SUMIFS($F$2:$F$@,$A$2:$A$@,I$2)", "@", UBound(a))
    [I3].Resize(3, .Count) = [I3].Resize(3, .Count).Value '<--- Comment this line if you wish to retain your formulas
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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