'Using Dictionary In Combination With Class Module
below is my code I got from the help of a youtube guide. It looks at Counts and Amounts of sales based on an ID for each member. It works perfectly to sum up all of members Counts and Amounts.
Sub DictionarySum()
'Set Worksheet Variable
Set ws = ThisWorkbook.Worksheets("Example Data")
'Create Dictionary Based On ReadData Function
Set dict = ReadData()
'Write Data Back To Worksheet
Call WriteDict(dict)
End Sub
Function ReadData()
'Set Range Of Data
Set rng = ws.Range("A1").CurrentRegion
LastRow = ws.Range(rng.Address).Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
'Loop Through All Rows Of Data
For i = 2 To LastRow
'Assign Values
ID = rng.Cells(i, 1)
MbrName = rng.Cells(i, 2)
Cnt = rng.Cells(i, 4)
Amt = rng.Cells(i, 5)
'Check To See If ID Exists In Dictionary
If dict.Exists(ID) = True Then
Set cMember = dict(ID)
Else
Set cMember = New memberclass
dict.Add ID, cMember
cMember.MbrName = MbrName
End If
cMember.Amt = cMember.Amt + Amt
cMember.Cnt = cMember.Cnt + Cnt
Next i
Set ReadData = dict
End Function
Function WriteDict(dict As Dictionary)
RowVar = 2
For Each key In dict
Set cMember = dict(key)
ws.Cells(RowVar, 7) = key
ws.Cells(RowVar, 8) = cMember.MbrName
ws.Cells(RowVar, 9) = cMember.Cnt
ws.Cells(RowVar, 10) = cMember.Amt
RowVar = RowVar + 1
Next key
End Function
Below is my sample data:
| ID | Name | SaleType | Cnt | Amt |
|---|---|---|---|---|
| A001 | John | Phone | 2 | 75 |
| A002 | Tim | Phone | 5 | 100 |
| A003 | Jane | Online | 4 | 60 |
| A002 | Tim | Online | 2 | 45 |
| A003 | Jane | Phone | 5 | 125 |
| A001 | John | Online | 4 | 90 |
But what I want to be able to do is throw in another qualifier, called "SaleType" and be able to call to a members sales just a certain SaleType. Essentially instead of outputting "cMember.Cnt", I want to be able to do something like "cMember.SaleType("Phone").Cnt" Is this possible? Thanks in advance.
EDIT: Here is an attempt at a nested dictionary approach, it errors out when attempting to loop through the innerkeys. Anyone have any ideas on how to fix?
Sub Nested()
Set outer = New Dictionary
'Set Worksheet Variable
Set ws = ThisWorkbook.Worksheets("Example Data")
'Set Range Of Data
Set rng = ws.Range("A1").CurrentRegion
LastRow = ws.Range(rng.Address).Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
For j = 0 To 1
If j = 0 Then WantSaleType= "Phone"
If j = 1 Then WantSaleType= "Online"
'Loop Through All Rows Of DatA
For i = 2 To LastRow
Set inner = New Dictionary
'Assign Values
ID = rng.Cells(i, 1)
MbrName = rng.Cells(i, 2)
SaleType = rng.Cells(i, 3)
Cnt = rng.Cells(i, 4)
Amt = rng.Cells(i, 5)
If SaleType <> WantSaleTypeThen GoTo NextRow
'Check To See If ID Exists In Dictionary
If inner.Exists(ID) = True Then
Set cMember = inner(ID)
Else
Set cMember = New memberclass
inner.Add ID, cMember
cMember.MbrName = MbrName
End If
cMember.Amt = cMember.Amt + Amt
cMember.Cnt = cMember.Cnt + Cnt
NextRow:
Next i
outer.Add WantSaleType, inner
Next
Dim outerkey As Variant
Dim innerkey As Variant
For Each outerkey In outer.Keys
RowVar = 2
For Each innerkey In outer(outerkey)
Set cMember = outer(outerkey)(innerkey)
'Debug.Print key, cMember.Amt, cMember.Cnt
ws.Cells(RowVar, 6) = outerkey
ws.Cells(RowVar, 7) = innerkey
ws.Cells(RowVar, 8) = cMember.MbrName
ws.Cells(RowVar, 9) = cMember.Cnt
ws.Cells(RowVar, 10) = cMember.Amt
RowVar = RowVar + 1
Next innerkey
Next outerkey
End Sub
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
