chriscorpion786

Board Regular
Joined
Apr 3, 2011
Messages
108
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.....
NameUnitAmount
Jonathan 215,544 657,015
Mustafa 263,448 1,208,102
Mahmood 217,368 662,254
Daniel 198,912 606,677
Sam Benjamin 202,560 617,457
Mike Hallet 226,800 691,292
Harry Stanley 207,696 632,754
Christopher 220,152 671,133
Tasawar 195,960 597,515

<colgroup><col><col span="2"></colgroup><tbody>
</tbody>
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
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,215,046
Messages
6,122,849
Members
449,096
Latest member
Erald

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