tiredofit
Well-known Member
- Joined
- Apr 11, 2013
- Messages
- 1,924
- Office Version
- 365
- 2019
- Platform
- Windows
In the following code to find unique values and aggregate amounts and volumes:
This is Class1:
can someone please explain this line:
I understand that if the dictionary with the key CustomerID DOES NOT exist, then create a new instance of Class1 and add this instance of Class1 to the dictionary with CustomerID as the key but if the key CustomerID DOES exist, what is "Set oCustomer = dict(CustomerID)" doing?
Thanks
Code:
Option Explicit
Sub Start()
Dim dict As Scripting.Dictionary
Set dict = ReadData
End Sub
Function ReadData() As Dictionary
Dim dict As Scripting.Dictionary
Set dict = New Scripting.Dictionary
Dim rg As Range
Set rg = Sheet1.Range("A1").CurrentRegion
Dim i As Long, CustomerID As String, Amount As Currency
Dim Volume As Long, oCustomer As Class1
For i = 2 To rg.Rows.Count
CustomerID = rg.Cells(i, 1).Value
Amount = rg.Cells(i, 4).Value
Volume = rg.Cells(i, 5).Value
If dict.Exists(CustomerID) Then
Set oCustomer = dict(CustomerID)
Else
Set oCustomer = New Class1
dict.Add CustomerID, oCustomer
End If
oCustomer.Amount = oCustomer.Amount + Amount
oCustomer.Volume = oCustomer.Volume + Volume
Next i
Set ReadData = dict
End Function
This is Class1:
Code:
Option Explicit
Private pAmount As Currency
Private pVolume As Long
Public Property Get Amount() As Currency
Amount = pAmount
End Property
Public Property Let Amount(ByVal vNewValue As Currency)
pAmount = vNewValue
End Property
Public Property Get Volume() As Long
Volume = pVolume
End Property
Public Property Let Volume(ByVal vNewValue As Long)
pVolume = vNewValue
End Property
can someone please explain this line:
Code:
If dict.Exists(CustomerID) Then
Set oCustomer = dict(CustomerID) 'PUZZLES ME ?????
Else
Set oCustomer = New Class1
dict.Add CustomerID, oCustomer
End If
I understand that if the dictionary with the key CustomerID DOES NOT exist, then create a new instance of Class1 and add this instance of Class1 to the dictionary with CustomerID as the key but if the key CustomerID DOES exist, what is "Set oCustomer = dict(CustomerID)" doing?
Thanks