Hello,
I am trying to write a code where I can highlight a row based on a comparison of status for each account comparing data in two different sheets. I found the below code that I thought would work perfectly, but it's not highlighting my rows. Column 10 in both sheets (Current sheet and Prior Sheet) is the account number that it needs to find and then column 7 is the status. the Current Sheet will have an updated status. finding the matching account number in both sheet, once the match is found, then compare the status. If status has changed, then highlight entire row in the Current sheet. What am i missing in the code I tweaked below?
I am trying to write a code where I can highlight a row based on a comparison of status for each account comparing data in two different sheets. I found the below code that I thought would work perfectly, but it's not highlighting my rows. Column 10 in both sheets (Current sheet and Prior Sheet) is the account number that it needs to find and then column 7 is the status. the Current Sheet will have an updated status. finding the matching account number in both sheet, once the match is found, then compare the status. If status has changed, then highlight entire row in the Current sheet. What am i missing in the code I tweaked below?
VBA Code:
Sub HiLiteRows()
Dim rng1 As Range, rng2 As Range, cmp1 As Variant
Dim i As Long, j As Long, k As Long, op As Range
Dim lRow As Long, lRow2 As Long
'Set last row used
lRow = Sheets("Audit_Plan").Range("J" & Rows.Count).End(xlUp).Row
lRow2 = Sheets("Audit_Plan_PRIOR").Range("J" & Rows.Count).End(xlUp).Row
' Set this range to the table that you want hilighted
Set rng1 = Sheets("Audit_Plan").Range("A7:CZ" & lRow)
' Set this range to the table you're comparing with
Set rng2 = Sheets("Audit_Plan_PRIOR").Range("A7:CZ" & lRow2)
' Set this array to the columns you want to compare. So here, we're comparing
' column 10 of Audit_Plan sheet with column 10 of Audit_Plan_PRIOR sheet, then column 7 of Audit_Plan sheet with column 7 of Audit_Plan_PRIOR sheet
' Add as many as you want
cmp1 = Array(10, 10, 7, 7)
' Clear formatting
rng1.Interior.Color = xlNone
' Set the output range to nothing
Set op = Nothing
d1 = rng1.Value
d2 = rng2.Value
For i = 1 To UBound(d1)
For j = 1 To UBound(d2)
For k = 0 To UBound(cmp1) - 1 Step 2
If d1(i, cmp1(k)) <> d2(j, cmp1(k + 1)) Then Exit For
Next k
If k > UBound(cmp1) Then Exit For
Next j
If j > UBound(d2) Then
If op Is Nothing Then
Set op = rng1.Offset(i - 1).Resize(1)
Else
Set op = Union(op, rng1.Offset(i - 1).Resize(1))
End If
End If
Next i
If op Is Nothing Then Exit Sub
op.Interior.Color = vbYellow
End Sub