In my worksheet, the cells in column 4 contain a formula. Sometimes the formula returns a number, and sometimes it returns the string "data!". For the latter result, I want the font color to change to red. The following event handler code fails:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
'font color red if error
If Target.Column = 4 Then
If Target = "data!" Then
Range("D" & Target.Row).Font.ColorIndex = 3
Else
Range("D" & Target.Row).Font.ColorIndex = 1
End If
End If
Application.EnableEvents = True
End Sub
Perhaps the code is seeing the formula instead of the value the formula returns, so I added the evaluate method to operate on Target:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
'font color red if error
If Target.Column = 4 Then
If Evaluate(Target) = "data!" Then
Range("D" & Target.Row).Font.ColorIndex = 3
Else
Range("D" & Target.Row).Font.ColorIndex = 1
End If
End If
Application.EnableEvents = True
End Sub
This also does not work. No error, just no font color change. What is the problem?