ambrosedheffernan
New Member
- Joined
- Nov 20, 2016
- Messages
- 5
I am learning VBA and trying to master collections. I am using collections because the ability to remove items seems likes it gives an advantage over arrays when using 2D.
I am looking to compare source 1 to source 2. I load both sets into collections.
Sample Data
source 1[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]A[/TD]
[TD]B[/TD]
[TD]c[/TD]
[/TR]
[TR]
[TD]3/10/12[/TD]
[TD]ab[/TD]
[TD]10[/TD]
[/TR]
[TR]
[TD]3/10/12[/TD]
[TD]ab[/TD]
[TD]10[/TD]
[/TR]
[TR]
[TD]5/10/12[/TD]
[TD]ab[/TD]
[TD]20[/TD]
[/TR]
[TR]
[TD]5/10/12[/TD]
[TD]ab[/TD]
[TD]20[/TD]
[/TR]
</tbody>[/TABLE]
Source 2
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]a[/TD]
[TD]b[/TD]
[TD]c[/TD]
[/TR]
[TR]
[TD]4/10/12[/TD]
[TD]abc[/TD]
[TD]20[/TD]
[/TR]
[TR]
[TD]6/10/12[/TD]
[TD]abc[/TD]
[TD]40[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
then I create a new collection to hold matched data, e.g. on the 3rd there is 20 in total, this matches the 4th in source 2
Anyway this is the collection structure I was looking for
'''''''''''''''''''
' matches
' xerotrans - collection of ranges
' banktrans - range
xerotrans - collection of ranges
' banktrans - range
' etc
'''''''''''''''''''
this is my code to loop over...
The matches.count is correct, but as I delete the data from the collection used to add the 2nd dimension to matches, that array is empty.
How to add this collection to matches by value rather than by ref?
I am looking to compare source 1 to source 2. I load both sets into collections.
Sample Data
source 1[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]A[/TD]
[TD]B[/TD]
[TD]c[/TD]
[/TR]
[TR]
[TD]3/10/12[/TD]
[TD]ab[/TD]
[TD]10[/TD]
[/TR]
[TR]
[TD]3/10/12[/TD]
[TD]ab[/TD]
[TD]10[/TD]
[/TR]
[TR]
[TD]5/10/12[/TD]
[TD]ab[/TD]
[TD]20[/TD]
[/TR]
[TR]
[TD]5/10/12[/TD]
[TD]ab[/TD]
[TD]20[/TD]
[/TR]
</tbody>[/TABLE]
Source 2
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]a[/TD]
[TD]b[/TD]
[TD]c[/TD]
[/TR]
[TR]
[TD]4/10/12[/TD]
[TD]abc[/TD]
[TD]20[/TD]
[/TR]
[TR]
[TD]6/10/12[/TD]
[TD]abc[/TD]
[TD]40[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
then I create a new collection to hold matched data, e.g. on the 3rd there is 20 in total, this matches the 4th in source 2
Anyway this is the collection structure I was looking for
'''''''''''''''''''
' matches
' xerotrans - collection of ranges
' banktrans - range
xerotrans - collection of ranges
' banktrans - range
' etc
'''''''''''''''''''
this is my code to loop over...
Code:
Dim XerodateSortCol As Integer
XerodateSortCol = 1
Dim BankdateSortCol As Integer
BankdateSortCol = 1
'all the dates should be in order so we can create a new collection to store the values with the same date and then match to source 2
Dim trans As Variant
Dim curDate As Variant
curDate = 0
Dim DateCount As Integer
DateCount = 0
Dim Banki As Variant
Dim ThisDateCollection As New Collection
Dim ThisDateXeroTrans As New Collection
Dim matches As New Collection
'''''''''''''''''''
' matches
' key = date
' xerotrans - collection of ranges
' banktrans - range
'
'''''''''''''''''''
For Each trans In XeroCreditCards
'date is not set yet
'set up a new date in matches......we are duplicating this but anyway, also located in when current date does not match previous
'date
If curDate = 0 Then
curDate = trans.Columns(XerodateSortCol)
End If
'check here if the date is different from the last date we have, if yes, finish off the day and find the match.
'Don't do if curDate = 0
If curDate = trans.Columns(XerodateSortCol) Then
ThisDateXeroTrans.Add trans
DateCount = DateCount + trans.Columns(valueLeftCol)
ElseIf curDate <> trans.Columns(XerodateSortCol) Then
'add the transaction to the xero transactions
ThisDateCollection.Add ThisDateXeroTrans, "XeroTransactions"
Debug.Print ThisDateCollection("XeroTransactions").Count
Debug.Print "new date"
For Each Banki In BankCreditCards
If Banki.Columns(valueRightCol) = DateCount Then
'we have a match, add to CCmatches
ThisDateCollection.Add Banki, "BankTransaction"
End If
Next Banki
matches.Add ThisDateCollection
'reset so we can do the next day
DateCount = 0
curDate = 0
While ThisDateCollection.Count <> 0
ThisDateCollection.Remove (ThisDateCollection.Count)
Wend
End If
Next trans
'we should have matched all here, output the transactions
Debug.Print "matches length = " & matches(1).Count
The matches.count is correct, but as I delete the data from the collection used to add the 2nd dimension to matches, that array is empty.
How to add this collection to matches by value rather than by ref?