chriscorpion786
Board Regular
- Joined
- Apr 3, 2011
- Messages
- 108
- 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.....
<colgroup><col><col span="2"></colgroup><tbody>
</tbody>
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.....
Name | Unit | Amount |
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>