Solved

Excel VB Conditional Formatting

Posted on 2004-09-28
6
248 Views
Last Modified: 2010-05-02
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!

0
Comment
Question by:ocah
  • 3
  • 2
6 Comments
 
LVL 81

Expert Comment

by:byundt
ID: 12175944
The Worksheet_Change event occurs when the user changes the value of the cell, not when the formula changes it.

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,Range("A2:N2"))
If targ is Nothing Then Exit Sub
For Each cel in targ
'Your color code goes here
Next cel
0
 
LVL 3

Expert Comment

by:thunder_moose
ID: 12177610
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
0
 

Author Comment

by:ocah
ID: 12181487
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:ocah
ID: 12181535
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?
0
 
LVL 81

Accepted Solution

by:
byundt earned 300 total points
ID: 12182267
ocah,
Try the following sub. It watches A2:N2 for user initiated changes, but applies the "conditional formatting" colors to cells R2:R66. You will notice that I added cases for a blank cell and for values over 100. This latter change made the rest of the Select Case much simpler, yet still functions like your original brackets.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim cel As Range
If Intersect(Target, Me.Range("A2:N2")) Is Nothing Then Exit Sub
On Error GoTo CleanUp:
For Each cel In Range("R2:R66") 'Check every cell in R2:R66 for proper highlight color
    Select Case cel.Value
    Case ""     'Don't color blank cells
    Case Is > 100
    Case Is >= 4.4: cel.Interior.ColorIndex = 39    'You don't need a bracket here because values over 100 already taken care of
    Case Is >= 4.25: cel.Interior.ColorIndex = 37
    Case Is >= 4: cel.Interior.ColorIndex = 4
    Case Is >= 3.75: cel.Interior.ColorIndex = 6
    Case Is >= 3: cel.Interior.ColorIndex = 46
    Case Is >= 0: cel.Interior.ColorIndex = 3
    'etc.
    End Select
Next cel
CleanUp:
Application.EnableEvents = True
End Sub

Brad
0
 
LVL 81

Expert Comment

by:byundt
ID: 12185420
ocah,
Thanks for the grade!
Brad
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How does CurrentUser work? 10 31
clicking a shape in a frame array vb6 3 40
Macro Excel - Multiple If conditions 2 63
Problem to With line 4 39
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now