A word of caution, having more than 5 colors would probably be bad design,
but you can extend the example as you see fit.
What you are asking about would be an Event macro, are the cells text
constants, or are they formulas.
Text is a better, since the cell you test is the same cell identified for the
test, and is also the same cell as you change the coloring in.
http://www.mvps.org/dmcritchie/excel/event.htm#case
If you used a formula you would have to idenify the none formula
cell on the same sheet that you actually change and test the value
of the cell that gets changed.
This is an Event macro and will only apply to the one tab.
Right-click on the worksheet tab, choose View code
and paste in the following code., replacing original Option Explicit
with all of the following. Change the range to cells you want
to change.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
'David McRitchie, 2000-08-08 rev. 2000-08-14
'
http://www.mvps.org/dmcritchie/excel/event.htm
' modified in newsgroup reply 2009-02-25 by D.McR
Dim vText As String
Dim vColor As Long
Dim cRange As Range
Dim cell As Range
'***************** check range ****
Set cRange = Intersect(Range("A:A"), Range(Target(1).Address))
If cRange Is Nothing Then Exit Sub
'**********************************
For Each cell In Target
vText = UCase(Trim(cell.Value))
'see colors.htm for use of ColorIndex
'
http://www.mvps.org/dmcritchie/excel/colors.htm
vColor = 0 'default is no color
Select Case vText
Case "HELLO"
vColor = 3 'Red
Case "GOOD BYE"
vColor = 38 'Pink
Case "C"
vColor = 39
Case "D"
vColor = 41
Case "E"
vColor = 34
Case "F"
vColor = 37
Case "G"
vColor = 35
End Select
Application.EnableEvents = False 'should be part of Change macro
cell.Interior.ColorIndex = vColor
Application.EnableEvents = True 'should be part of Change macro
Next cell
'use to color cell to right
' cell.Offset(0, 1).Interior.colorindex = vColor
'Use to color the entire row
' cell.EntireRow.Interior.ColorIndex = vColor
'To color Text use text instead of Interior
End Sub