Hi All,
I have written an code that compares two worksheets based on a unique ID and performs actions based on the differences (e.g. highlight new rows in green). The code works great, but I notice that when I have a lot of rows it takes a real long time to run as it is looping through each individual cell. I read that the code can be improved using Application.Match, but I have not been able to be successful coding it. Would someone please point me in the right direction? Attached is a snippet of my code. Happy to post more of the code if needed, but I felt this was the relevant part:
Thanks so much in advance for your help!
I have written an code that compares two worksheets based on a unique ID and performs actions based on the differences (e.g. highlight new rows in green). The code works great, but I notice that when I have a lot of rows it takes a real long time to run as it is looping through each individual cell. I read that the code can be improved using Application.Match, but I have not been able to be successful coding it. Would someone please point me in the right direction? Attached is a snippet of my code. Happy to post more of the code if needed, but I felt this was the relevant part:
VBA Code:
Sub compareReport1()
Application.ScreenUpdating = False
Application.EnableEvents = False
Dim oldrep1 As Range, newrep1 As Range, uido As String, uidn As String
Dim inoldrep1 As Variant, innewrep1 As Variant
Dim wsold As Worksheet
Dim wsnew As Worksheet
Dim lastrowo As Long
Dim lastrown As Long
Dim endDate As Long
Set wsold = Sheets(4)
Set wsnew = Sheets(3)
lastrowo = wsold.Range("A" & Rows.Count).End(xlUp).Row
lastrown = wsnew.Range("A" & Rows.Count).End(xlUp).Row
endDate = wsnew.Range("J" & Rows.Count).End(xlUp).Row
Set oldrep1 = wsold.Range("N1:N" & lastrowo) 'set for UID column in old report
Set newrep1 = wsnew.Range("N1:N" & lastrown) 'set for UID column in new report
Set enddatecol = wsnew.Range("J4:J" & endDate) 'set for End Date column if present
wsnew.Activate 'Bring New Report to front
'compare Old sheet (sheets (4)) to New sheet (Sheets(3)) and Highlight new rows in Green on New sheet
For i = 4 To lastrown 'row 4 to last column in new sheet
For j = 4 To lastrown 'row 4 to last column in old sheet
If Sheets(3).Cells(i, 14).Value = Sheets(4).Cells(j, 14).Value Then ' change (i, xx) to reflect column# of UID
For k = 1 To 30 'considering 30 columns in both sheets
If Sheets(4).Cells(j, k).Value = "" And Sheets(4).Cells(j, k).Value <> Sheets(3).Cells(i, k) Then
Sheets(3).Cells(i, k).Interior.Color = vbGreen
End If
Next k
End If
Next j
Next i
Thanks so much in advance for your help!