Please help fix VBA codes below. When I ran the code it color the whole column instead of mismatch values. I'm trying to compare sheet1 column D and sheet2 column C. if column C values does not exist on sheet1 column D, color the cell pink.
VBA Code:
Sub CompareAndColor()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim rng1 As Range
Dim rng2 As Range
Dim cell1 As Range
Dim cell2 As Range
' Set references to the worksheets
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("Sheet2")
' Set the range to compare in Sheet1 (excluding header)
Set rng1 = ws1.Range("D2:D" & ws1.Cells(ws1.Rows.Count, "D").End(xlUp).Row)
' Set the range to compare in Sheet2 (excluding header)
Set rng2 = ws2.Range("C2:C" & ws2.Cells(ws2.Rows.Count, "C").End(xlUp).Row)
' Loop through each cell in rng2 and compare with rng1
For Each cell2 In rng2
For Each cell1 In rng1
If cell1.Value <> cell2.Value Then
cell2.Interior.Color = RGB(255, 192, 203) ' Pink color
Exit For ' Exit the loop once a mismatch is found
End If
Next cell1
Next cell2
End Sub