View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
JWCardington JWCardington is offline
external usenet poster
 
Posts: 1
Default Conditional Formatting using VBA

Hello,
I have a number in cell B1 that is generated from a simple sum formula. I
want the background color in cell B4 to change depending on what value is
displayed in B1. I know how to do that using CF or by use of the Select
Case.Value code (See sample code below). Now what I want to do is also
display a TEXT string in cell B4 that also changes based on the value
displayed in cell B1. The color of the font in this text string will also
need to change based on the value displayed in cell B1.

Also I need help condensing this code a bit by using number ranges instead
of each possible number that may be displayed in cell b1

Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Range("B1")) Is Nothing Then
With Target
Select Case .Value
Case Is < 1: Range("B4").Interior.ColorIndex = 2
Case 1: Range("B4").Interior.ColorIndex = 10
Case 2: Range("B4").Interior.ColorIndex = 10
Case 3: Range("B4").Interior.ColorIndex = 10
Case 4: Range("B4").Interior.ColorIndex = 10
Case 5: Range("B4").Interior.ColorIndex = 10
Case 6: Range("B4").Interior.ColorIndex = 10
Case 7: Range("B4").Interior.ColorIndex = 6
Case 8: Range("B4").Interior.ColorIndex = 6
Case 9: Range("B4").Interior.ColorIndex = 6
Case 10: Range("B4").Interior.ColorIndex = 6
Case 11: Range("B4").Interior.ColorIndex = 6
Case 12: Range("B4").Interior.ColorIndex = 6
Case 13: Range("B4").Interior.ColorIndex = 3
Case 14: Range("B4").Interior.ColorIndex = 3
Case 15: Range("B4").Interior.ColorIndex = 3
Case 16: Range("B4").Interior.ColorIndex = 3
Case 17: Range("B4").Interior.ColorIndex = 3
Case 18: Range("B4").Interior.ColorIndex = 3
Case 19: Range("B4").Interior.ColorIndex = 3
Case 20: Range("B4").Interior.ColorIndex = 3
Case 21: Range("B4").Interior.ColorIndex = 3
Case 22: Range("B4").Interior.ColorIndex = 3
Case Is 22: Range("B4").Interior.ColorIndex = 3
End Select
End With
End If
ws_exit:
Application.EnableEvents = True
End Sub

P.S.
I borrowed the initial Case Is code from someone on here but cant remember
who it came from. But Thanks for the bones!

JW