chriscorpion786
Board Regular
- Joined
- Apr 3, 2011
- Messages
- 112
- Office Version
- 365
- Platform
- 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]
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]