First time poster long time reader.
My colleague and I have spent a while creating this code. Whilst it runs brilliantly for small data sizes, our full data set is two tables of 100k lines or so. We let it run for some 30-40 mins and it just grinds to a halt. We have no idea how to make it any faster.
The idea is that for every line in one table, we need to search the second table for a date closest to two days prior to the account date. We also find a date closest to 2 weeks after the date that is two days prior. The dates are sorted newest to oldest from top to bottom.
Once we have this range, we need to search another column to find the first Account ID that appeared within this date range. Once we know this row, we use it to look up two other cells in the row.
I imagine somehow doing it inside an array would be incredibly better but I have no idea how to get it to that level for what we're after. Potentially stick all of the dates within an array and figure out the array number and use those for the rows for the find later on?
Here's our code so far. I know our first problem is possibly because we have a loop that cycles through one table and feeds the account number and date into the function that does the work:
Function Find_Last(AccountNumber, AccountDate As Date)
'Function to find the first occurance of account number and associated quality within a two week range
Dim R As Range
Dim LastDiff1 As Date
Dim LastDiff2 As Date
Dim LastCell1 As Range, LastCell2 As Range
Dim SearchDate1
Dim SearchDate2
Dim Rng As Range
Dim DestSheet As Worksheet
Dim LastRow
Set DestSheet = Workbooks("Interim Referrals Report.xlsm").Worksheets("SA Wrap Up Data")
SearchDate1 = DateAdd("d", 14, AccountDate)
SearchDate2 = DateAdd("d", -2, AccountDate)
LastDiff1 = DateSerial(9999, 1, 1)
LastDiff2 = DateSerial(9999, 1, 1)
LastRow = Range("A" & Rows.Count).End(xlUp).Row
For Each R In DestSheet.Range("A2:A" & LastRow)
If IsDate(R.Value) Then
'Do Nothing
If Abs(R.Value - SearchDate1) < LastDiff1 Then
Set LastCell1 = R
LastDiff1 = Abs(R.Value - SearchDate1)
End If
End If
If IsDate(R.Value) Then
'Do Nothing
If Abs(R.Value - SearchDate2) < LastDiff2 Then
Set LastCell2 = R
LastDiff2 = Abs(R.Value - SearchDate2)
End If
End If
Next R
'Find the CR account number within the designated range in the SA cricket
'data worksheet, looks from bottom of range up
With DestSheet.Range("L" & LastCell1.Row & ":L" & LastCell2.Row)
Set Rng = DestSheet.Cells.Find(What:=AccountNumber, After:=.Cells(LastCell1.Row), LookIn:=xlFormulas, LookAt:=xlWhole, _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)
'if there is a match, return the row number
If Not Rng Is Nothing Then
Find_Last = Rng.Row
Else
Find_Last = "No Match"
End If
End With
End Function
Can anyone help?
screenupdating... set calculation to manual when code is running.