Color Cells via VBA

M

Mike Eckardt

I'm trying to set the background color (highlight) cells
in my spreadsheet based on the value of a cell. I haven't
been able to find a way to access this programmatically,
and I don't want to do it by hand every time the value
changes. Any clues?
 
D

Don Guillett

If you have 3 or fewer conditions try format>conditional format. If not come
back for code.
 
M

mangesh_yadav

this could be one example

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Value = 1 Then
Target.Cells.Interior.ColorIndex = 1
ElseIf Target.Value = 2 Then
Target.Cells.Interior.ColorIndex = 4
Else
Target.Cells.Interior.ColorIndex = 3
End If

End Sub

note: the code has to be placed in the sheet module


Alternatively you could also use conditional formatting

- Mangesh
 
Top