Dictionary in VBA

chriscorpion786

Board Regular
Joined
Apr 3, 2011
Messages
112
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm using the dictionary in VBA to summarize based on the units held in one column, but I would also like the code to summarize by amount, which is in another column, can I do this in the same sub and output both the results by name for units and amount. I have pasted the example how the output should be like below.

Dim dict As Dictionary
Set dict = New Dictionary


Dim name As String
Dim amount As Double
Dim units As Long
Dim r As Range
Dim key As Variant




Set r = Range("A1").CurrentRegion


For i = 2 To r.Rows.Count


name = r.Cells(i, 1).Value
units = r.Cells(i, 4).Value
amount = r.cells(i,5).value


dict(name) = dict(name) + units
dict(name) = dict(name) + amount


Next i

x = 2


For Each key In dict


Cells(x, 8).Value = key
Cells(x, 9).Value = dict(key)


x = x + 1


Next key
End Sub



Example.....
[TABLE="width: 292"]
<colgroup><col><col span="2"></colgroup><tbody>[TR]
[TD]Name[/TD]
[TD]Unit[/TD]
[TD]Amount[/TD]
[/TR]
[TR]
[TD] Jonathan [/TD]
[TD] 215,544[/TD]
[TD] 657,015[/TD]
[/TR]
[TR]
[TD] Mustafa [/TD]
[TD] 263,448[/TD]
[TD] 1,208,102[/TD]
[/TR]
[TR]
[TD] Mahmood [/TD]
[TD] 217,368[/TD]
[TD] 662,254[/TD]
[/TR]
[TR]
[TD] Daniel [/TD]
[TD] 198,912[/TD]
[TD] 606,677[/TD]
[/TR]
[TR]
[TD] Sam Benjamin [/TD]
[TD] 202,560[/TD]
[TD] 617,457[/TD]
[/TR]
[TR]
[TD] Mike Hallet [/TD]
[TD] 226,800[/TD]
[TD] 691,292[/TD]
[/TR]
[TR]
[TD] Harry Stanley [/TD]
[TD] 207,696[/TD]
[TD] 632,754[/TD]
[/TR]
[TR]
[TD] Christopher [/TD]
[TD] 220,152[/TD]
[TD] 671,133[/TD]
[/TR]
[TR]
[TD] Tasawar [/TD]
[TD] 195,960[/TD]
[TD] 597,515[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
There are other ways you could do this, but to use a dictionary approach you could modify your code:

Code:
'....
Dim Amounts() As Long
Const N = 3     'You only need 2!
ReDim Amounts(1 To N) As Long

Set dict = New Dictionary
Set r = Range("A1").CurrentRegion

For i = 2 To r.Rows.Count
    name = r.Cells(i, 1).Value
    For j = 1 To N
        Amounts(j) = r.Cells(i, j + 1).Value
        On Error Resume Next
        Amounts(j) = Amounts(j) + dict(name)(j)
        On Error GoTo 0
    Next j
    dict(name) = Amounts
Next i

x = 2
For Each key In dict
    Cells(x, 8).Value = key
    For j = 1 To N
        Cells(x, 8 + j).Value = dict(key)(j)
    Next j
    x = x + 1
Next key


Book1
ABCDEFGHIJK
1NameUnitAmountSomethingResults
2Jonathan215,544657,0151Jonathan418,1041,274,4726
3Mustafa263,4481,208,1022Mustafa490,2481,899,3948
4Mahmood217,368662,2543Mahmood425,0641,295,00810
5Daniel198,912606,6774Daniel394,8721,204,19213
6Jonathan202,560617,4575Christopher220,152671,1338
7Mustafa226,800691,2926
8Mahmood207,696632,7547
9Christopher220,152671,1338
10Daniel195,960597,5159
Sheet1
 
Last edited:
Upvote 0
Simplest method is to use two dictionaries I'd say:

Code:
Public Sub SummarizeData()

Dim dictUnits As Dictionary
Set dictUnits = New Dictionary
Dim dictAmount As Dictionary
Set dictAmount = New Dictionary

Dim name As String
Dim amount As Double
Dim units As Long
Dim r As Range
Dim key As Variant
Dim i As Long
Dim x As Long

Set r = Range("A1").CurrentRegion

For i = 2 To r.Rows.Count
    name = r.Cells(i, 1).Value
    units = r.Cells(i, 4).Value
    amount = r.Cells(i, 5).Value
    
    dictUnits(name) = dictUnits(name) + units
    dictAmount(name) = dictAmount(name) + amount
Next i

x = 2

For Each key In dictUnits
    Cells(x, 8).Value = key
    Cells(x, 9).Value = dictUnits(key)
    Cells(x, 10).Value = dictAmount(key)
    x = x + 1
Next key

End Sub

Please remember to use code tags when posting code.

WBD
 
Upvote 0
Simplest method is to use two dictionaries I'd say:

Code:
Public Sub SummarizeData()

Dim dictUnits As Dictionary
Set dictUnits = New Dictionary
Dim dictAmount As Dictionary
Set dictAmount = New Dictionary

Dim name As String
Dim amount As Double
Dim units As Long
Dim r As Range
Dim key As Variant
Dim i As Long
Dim x As Long

Set r = Range("A1").CurrentRegion

For i = 2 To r.Rows.Count
    name = r.Cells(i, 1).Value
    units = r.Cells(i, 4).Value
    amount = r.Cells(i, 5).Value
    
    dictUnits(name) = dictUnits(name) + units
    dictAmount(name) = dictAmount(name) + amount
Next i

x = 2

For Each key In dictUnits
    Cells(x, 8).Value = key
    Cells(x, 9).Value = dictUnits(key)
    Cells(x, 10).Value = dictAmount(key)
    x = x + 1
Next key

End Sub

Please remember to use code tags when posting code.

WBD

Thank you, this code is so simple and worked perfect
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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