ocah
asked on
Excel VB Conditional Formatting
I'm trying to get cells of different values to show up in different colors, but the following code doesn't seem to work when the cells are formulas instead of just plain old numbers. The cells I want to change color have formulas like "=AVERAGE(A2:N2)"
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Intersect(Target, Me.Range("r2:r66")) Is Nothing Then Exit Sub
On Error GoTo CleanUp:
With Target
Select Case .Value
Case 1 To 3: .Interior.ColorIndex = 3
Case 3 To 10: .Interior.ColorIndex = 10
'etc.
End Select
End With
CleanUp:
Application.EnableEvents = True
End Sub
Thanks!
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Intersect(Target, Me.Range("r2:r66")) Is Nothing Then Exit Sub
On Error GoTo CleanUp:
With Target
Select Case .Value
Case 1 To 3: .Interior.ColorIndex = 3
Case 3 To 10: .Interior.ColorIndex = 10
'etc.
End Select
End With
CleanUp:
Application.EnableEvents = True
End Sub
Thanks!
Hey there,
Your problem is that you're referring to a whole range of cells and then trying to use the .value property. If you refer to each cell individually, it points out the value fine. What you should do is the following:
For Each Cell in Target.Cells
With Cell
Select Case .Value
Case 1 To 3: .Interior.ColorIndex = 3
Case 3 To 10: .Interior.ColorIndex = 10
End Select
End With
Next
Also, 1 to 3, then 3 to 10 is going to confuse it a bit. Shouldn't it be 1 to 3 and then 4 to 10 or 1 to 2 and then 3 to 10?
Hope that helps,
TM
Your problem is that you're referring to a whole range of cells and then trying to use the .value property. If you refer to each cell individually, it points out the value fine. What you should do is the following:
For Each Cell in Target.Cells
With Cell
Select Case .Value
Case 1 To 3: .Interior.ColorIndex = 3
Case 3 To 10: .Interior.ColorIndex = 10
End Select
End With
Next
Also, 1 to 3, then 3 to 10 is going to confuse it a bit. Shouldn't it be 1 to 3 and then 4 to 10 or 1 to 2 and then 3 to 10?
Hope that helps,
TM
ASKER
Byundt -
The following code watches A2:N2 for a change, but then it only color codes those cells. Is there a way to watch those cells for a change, but then color code the calculated cell (R2), based on its value after the change? Maybe the calculation needs to be built into the Select Case statement?
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Intersect(Target, Me.Range("A2:N2")) Is Nothing Then Exit Sub
On Error GoTo CleanUp:
With Target
Select Case .Value
Case 4.4 To 100: .Interior.ColorIndex = 39
Case 4.25 To 4.399: .Interior.ColorIndex = 37
Case 4 To 4.249: .Interior.ColorIndex = 4
Case 3.75 To 3.999: .Interior.ColorIndex = 6
Case 3 To 3.749: .Interior.ColorIndex = 46
Case 0 To 2.999: .Interior.ColorIndex = 3
'etc.
End Select
End With
CleanUp:
Application.EnableEvents = True
End Sub
The following code watches A2:N2 for a change, but then it only color codes those cells. Is there a way to watch those cells for a change, but then color code the calculated cell (R2), based on its value after the change? Maybe the calculation needs to be built into the Select Case statement?
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Intersect(Target, Me.Range("A2:N2")) Is Nothing Then Exit Sub
On Error GoTo CleanUp:
With Target
Select Case .Value
Case 4.4 To 100: .Interior.ColorIndex = 39
Case 4.25 To 4.399: .Interior.ColorIndex = 37
Case 4 To 4.249: .Interior.ColorIndex = 4
Case 3.75 To 3.999: .Interior.ColorIndex = 6
Case 3 To 3.749: .Interior.ColorIndex = 46
Case 0 To 2.999: .Interior.ColorIndex = 3
'etc.
End Select
End With
CleanUp:
Application.EnableEvents = True
End Sub
ASKER
Thunder Moose - Reffering to each cell individually didn't seem to work...I still had to manually change the value of the cells before the color would change....I need the color to change when the average of several other cells changes... maybe it has to do with the Worksheet_Change event not occuring when the formula recalculates?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ocah,
Thanks for the grade!
Brad
Thanks for the grade!
Brad
Consider changing the exclusion range in your sub so that it watches cells A2:N2. Since those cells affect your average, you'll update your colors.
If Intersect(Target, Me.Range("A2:N2")) Is Nothing Then Exit Sub
Also, I suggest that you consider the possibility that more than one cell may be changed at a time:
Dim targ As Range, cel As Range
Set targ=Intersect(Target,Rang
If targ is Nothing Then Exit Sub
For Each cel in targ
'Your color code goes here
Next cel