Format cell based on data in a different cell

Closed
MK - Oct 16, 2009 at 07:03 AM
venkat1926 Posts 1863 Registration date Sunday June 14, 2009 Status Contributor Last seen August 7, 2021 - Oct 17, 2009 at 01:43 AM
Hello,

I have a request to format a cell based on certain ranges and display in a "traffic light" (red/yellow/green) like manner for a dashboard.
For example: If the value of A1 = 0 then cell B2 should have a background color of red and display the word "Red". By having the word "Red" in the cell, if the spreadsheet is printed in gray scale, the user will still know what color it is. If A1 (> 0 and <= .08) then cell B2 should display the word "Yellow" and have yellow as a background color. If A1 > .08 then B2 should display the word "Green" and have the background color of green.
Is this possible?
Thanks,
MK

1 response

venkat1926 Posts 1863 Registration date Sunday June 14, 2009 Status Contributor Last seen August 7, 2021 811
Oct 17, 2009 at 01:43 AM
right click sheet tab and click view code
in the resulting window copy this event code

Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range
If Target.Address <> "$A$1" Then Exit Sub
Set r = Range("B2")

If Target = 0 Then
With r
.Value = "red"
.Interior.ColorIndex = 3
End With
ElseIf Target > 0 And Target <= 0.08 Then
With r
.Value = "yellow"
.Interior.ColorIndex = 6
End With
ElseIf Target > 0.08 Then
With r
.Value = "green"
.Interior.ColorIndex = 4
End With
End If

End Sub



now type
0 in A1 see what happens to B2
.05
.09
-1