Sub ChangeColorBasedOnValue()
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=NOT(ISERROR(MATCH(A2,Sheet1!$A:$A,0)))"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 192
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=ISERROR(MATCH(A2,Sheet1!$A:$A,0))"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = -0.249946592608417
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub
Code 2:
Sub ChangeColorBasedOnValue()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet2")
ws.Activate
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=NOT(ISERROR(MATCH(A2,Sheet1!$A:$A,0)))"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 192
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=ISERROR(MATCH(A2,Sheet1!$A:$A,0))"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = -0.249946592608417
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub
Code 3:
Sub ChangeColorBasedOnValueNew()
Dim lastRowSheet1, lastRowSheet2 As Integer
Dim i, j As Integer
Dim tempName As String
Dim found As Boolean
lastRowSheet1 = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
lastRowSheet2 = Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastRowSheet2
tempName = Sheets("Sheet2").Cells(i, 1).Value
If Sheets("Sheet2").Cells(i, 1).Value <> "" Then
found = False
For j = 2 To lastRowSheet1
If Sheets("Sheet1").Cells(j, 1).Value <> "" Then
If tempName = Sheets("Sheet1").Cells(j, 1).Value Then
Sheets("Sheet2").Cells(i, 1).Interior.Color = vbRed
found = True
Exit For
End If
End If
Next j
End If
If Not found And Sheets("Sheet2").Cells(i, 1) <> "" Then
Sheets("Sheet2").Cells(i, 1).Interior.Color = vbGreen
End If
Next i
End Sub