View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
JulieD JulieD is offline
external usenet poster
 
Posts: 618
Default automatically changing the background color of a cell

Hi Martin

if you have only one criteria (or up to 3), choose the range b2:p2, choose
format / conditional formatting
choose
formula is
in the white line type
=$A$2=15
click on the format button, go to the patterns tab and choose the background
you want
click OK twice
(use the ADD button to add other criteria)

however, if you have more than 3 criteria or you really want to do it
programatically then here is an example of worksheet_change code that
demonstrates 9 different values in cell A2 and associated fill colour in the
range B2:P2

Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Range("A2")) Is Nothing Then
With Target
Select Case .Value
Case 1: Range("B2:P2").Interior.ColorIndex = 4
Case 2: Range("B2:P2").Interior.ColorIndex = 3
Case 3: Range("B2:P2").Interior.ColorIndex = 0
Case 4: Range("B2:P2").Interior.ColorIndex = 6
Case 5: Range("B2:P2").Interior.ColorIndex = 13
Case 6: Range("B2:P2").Interior.ColorIndex = 46
Case 7: Range("B2:P2").Interior.ColorIndex = 11
Case 8: Range("B2:P2").Interior.ColorIndex = 7
Case 9: Range("B2:P2").Interior.ColorIndex = 55
End Select
End With
End If
ws_exit:
Application.EnableEvents = True
End Sub

---
Please let us know if you need instructions on how to change or use this
code.

Cheers
JulieD


"martin" wrote in message
...
Dear all,

I need Excel to automatically change the background color of a cell. The
problem is as follows:
I have a number, say 15 in A2. I need Excel to change the background color
of b2:p2, as this is 15 cells on the right from that.

Is that possible,
All the best,
and thanks,
Martin.