Хорошо, вот примерное представление о том, что я упомянул в комментариях.
Это загрузить данные:
Sub test()
Dim parentdict As Object
Dim parentcls As Parent
Dim iter As Long
Dim lastrow As Long
Dim customer As String
Dim trans As Long
Dim transdate As Date
Set parentdict = CreateObject("Scripting.Dictionary")
With ActiveSheet ' Use a real sheet name
lastrow = .Cells(.rows.count, "A").End(xlUp).row
For iter = 2 To lastrow
customer = .Cells(iter, "A").value
trans = .Cells(iter, "B").value
transdate = .Cells(iter, "C").value
If Not parentdict.Exists(customer) Then ' Populate Parent Dictionary
Set parentcls = New Parent
parentcls.initialize transdate
parentdict.Add customer, parentcls
parentdict(customer).addtrans trans, transdate
Else
parentdict(customer).addtrans trans, transdate
End If
Next
End With
End Sub
Это родительский класс:
Option Explicit
Private plasttrans As Date
Private pcurrentmonth As Boolean
Private ptotaltrans As Long
Private pchilddict As Object
Private childcls As Child
Public Property Get lasttrans() As Date
lasttrans = plasttrans
End Property
Public Property Let lasttrans(llasttrans As Date)
plasttrans = llasttrans
End Property
Public Property Get currentmonth() As Boolean
currentmonth = pcurrentmonth
End Property
Private Sub togglecurrent()
If pcurrentmonth = False Then
pcurrentmonth = True
Else
pcurrentmonth = False
End If
End Sub
Public Property Get totaltrans() As Long
totaltrans = ptotaltrans
End Property
Public Sub addtrans(transaction As Long, transactiondate As Date)
ptotaltrans = ptotaltrans + 1
Set childcls = New Child
childcls.transdate = transactiondate
pchilddict.Add transaction, childcls
If Month(transactiondate) = Month(Date) Then
togglecurrent
End If
If transactiondate > plasttrans Then
plasttrans = transactiondate
End If
End Sub
Public Sub initialize(transactiondate As Date)
Set pchilddict = CreateObject("Scripting.Dictionary")
plasttrans = transactiondate
pcurrentmonth = False
ptotaltrans = 0
End Sub
А вот дочерний класс (я добавил только в одном свойстве):
Option Explicit
Private ptransdate As Date
Public Property Let transdate(ltransdate As Date)
ptransdate = ltransdate
End Property
Public Property Get transdate() As Date
transdate = ptransdate
End Property
Идея в том, что у вас есть словарь с Key: = Customer, Item: = Parent
Родительский класс - это класс, содержащий все на уровне клиента, т. Е. Флаг текущего месяца, последняя транзакция, текущий счетчик транзакций и еще один словарь для транзакций.
Словарь транзакций - это ключ: = транзакция, элемент: = дочерний элемент
Child - это класс, содержащий все на уровне транзакции. В моем примере это просто дата, но вы можете добавить суммы в долларах или все, что вам нужно. Если вам больше ничего не нужно, вы можете просто использовать словарь в Parent и вообще отказаться от дочернего класса.
После этого вы сможете получить доступ к любой транзакции, используя номер транзакции и клиента, или каждую транзакцию с циклом и клиентом.