Hi all!
I would appreciate any suggestions on how to speed up the following piece of code. The code processes a data sheet against a log sheet. Essentially, for each data row (there's about 7000 of them), it looks for a matching key value in the log sheet (which currently has about 8000 rows). When a log match is found, it updates the log row with the values from the data sheet row.
The code currently takes an eternal 15 minutes to run. Here's the whole sub, but I'm primarily interested in speeding up the find/write loop:
Thanks in advance for any suggestions!
VBAjedi
I would appreciate any suggestions on how to speed up the following piece of code. The code processes a data sheet against a log sheet. Essentially, for each data row (there's about 7000 of them), it looks for a matching key value in the log sheet (which currently has about 8000 rows). When a log match is found, it updates the log row with the values from the data sheet row.
The code currently takes an eternal 15 minutes to run. Here's the whole sub, but I'm primarily interested in speeding up the find/write loop:
Code:
Sub UpdateSummaryStats()
Dim x, y, z, a, b
Dim shSS As Object, shDA As Object
Dim LastSummaryRow As Integer, LastDataRow As Integer
Dim match As Object
Dim NewRecordsCounter As Integer
a = MsgBox("NOTE: This routine takes about two minutes " _
& "per thousand data rows to run (i.e. 7000 rows " _
& "would take about 15 minutes). Excel will be " _
& "unavailable during this time. Continue?", vbOKCancel)
If a = 2 Then Exit Sub
Set shSS = Sheets("Summary Stats")
Set shDA = Sheets("DATA")
shDA.Activate
NewRecordsCounter = 0
x = RotateWeeksData()
' Find LastDataRow and LastSummaryRow
For x = 3 To 50000
If Len(shDA.Range("I" & x).Value) = 0 And _
Len(shDA.Range("F" & x).Value) = 0 Then
' No doc # or cust ID
GoTo LastDataRowFound:
End If
Next x
LastDataRowFound:
LastDataRow = x - 1
For x = 13 To 50000
If Len(shSS.Range("I" & x).Value) = 0 And _
Len(shSS.Range("F" & x).Value) = 0 Then
' No doc # or cust ID
GoTo LastSummaryRowFound:
End If
Next x
LastSummaryRowFound:
LastSummaryRow = x - 1
' USE FIND FUNCTION TO LOCATE MATCHING SUMMARY ROW FOR
' EACH DATA ROW
a = Now
For x = 3 To LastDataRow
With shSS.Range("I13:I" & LastSummaryRow)
Set match = .Find(shDA.Range("I" & x).Value)
If Not match Is Nothing Then
y = match.row
GoTo MatchFound:
Else
GoTo NoMatch:
End If
End With
NoMatch:
NewRecordsCounter = NewRecordsCounter + 1
LastSummaryRow = LastSummaryRow + 1
y = LastSummaryRow
shSS.Range("V" & y).Value = "New This Week"
MatchFound:
shSS.Range("A" & y).Value = shDA.Range("A" & x).Value
shSS.Range("B" & y).Value = shDA.Range("B" & x).Value
shSS.Range("C" & y).Value = shDA.Range("C" & x).Value
shSS.Range("D" & y).Value = shDA.Range("D" & x).Value
shSS.Range("E" & y).Value = shDA.Range("E" & x).Value
shSS.Range("F" & y).Value = shDA.Range("F" & x).Value
shSS.Range("G" & y).Value = shDA.Range("G" & x).Value
shSS.Range("H" & y).Value = shDA.Range("H" & x).Value
shSS.Range("I" & y).Value = shDA.Range("I" & x).Value
shSS.Range("J" & y).Value = shDA.Range("J" & x).Value
shSS.Range("K" & y).Value = shDA.Range("K" & x).Value
shSS.Range("L" & y).Value = shDA.Range("L" & x).Value
shSS.Range("M" & y).Value = shDA.Range("M" & x).Value
shSS.Range("N" & y).Value = shDA.Range("N" & x).Value
shSS.Range("O" & y).Value = shDA.Range("O" & x).Value
shSS.Range("P" & y).Value = shDA.Range("P" & x).Value
shSS.Range("Q" & y).Value = shDA.Range("Q" & x).Value
shSS.Range("R" & y).Value = shDA.Range("R" & x).Value
shSS.Range("S" & y).Value = shDA.Range("S" & x).Value
shSS.Range("T" & y).Value = shDA.Range("T" & x).Value
shSS.Range("U" & y).Value = shDA.Range("U" & x).Value
shDA.Range("A1").Value = x ' provides visual progress
' indicator, doesn't seem to affect speed much
NextX:
Next x
MsgBox ("Successfully updated the Summary Sheet. " _
& NewRecordsCounter & " new records added.")
End Sub
Thanks in advance for any suggestions!
VBAjedi