I have two datasets that I need to compare and extract a match from. I have a composite key from 5 columns in each dataset, end a 6th column i need to extract. The columns are composed of text, date and integers. Both sets are slightly under 500k rows.
Currently I use a for loop in table a and loop through table b. Compare the rows with an if statement with the and argument to get the composite key.
Sub ArraySearch()
Dim Main As Long
Dim Search As Long
Dim arrData() As Variant
Dim arrSource As Variant
arrData = Sheets("Sheet1").Range("H3:M500000").Value
arrSource = Sheets("Ark1").Range("A3:H500000").Value
Main = 1
Search = 1
For Main = 1 To UBound(arrSource, 1)
For Search = 1 To UBound(arrData, 1)
If arrSource(Main, 3) = arrData(Search, 1) And _
arrSource(Main, 4) = arrData(Search, 2) And _
arrSource(Main, 1) = arrData(Search, 3) And _
arrSource(Main, 2) = arrData(Search, 4) And _
arrSource(Main, 5) = arrData(Search, 5) _
Then
arrSource(Main, 8) = arrData(Search, 6)
Exit For
End If
Next
Next
Sheets("Sheet2").Range("A3:H500000") = arrSource
End Sub
The fastest way so far is to load both tables into an array and do an in memory loop.
This is taking for ever. We are talking about hours not minutes.
Are there any methods that will increase the speed? Or do I need to use some other programs? (load it into a database and use SQL, use visual studio with normal VB.net, SSIS)
I was hoping this could be done in VBA, so any pointers would be much appreciated.
EDIT
Would hashing the 5 column key improve speed, or is it the share volume of rows that has to be iterated that creates the lag?