More than 3 conditional formats

kaptep

New Member
Joined
Feb 9, 2004
Messages
17
I Need to be able to format a large range of cells according to the number that they contain. Basically the cells are numbered 1-12 based on a variety of equations from other data and are updated frequently. I need the cells to be 12 different colors based on the value. Conditional formatting works great but only for 3 conditions. What is the easiest way to expand my conditions? I have read several tips for similar scenarios, but I cannot get any of the code to execute. :oops: Please give me some suggestions!!!
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
kaptep said:
Also, I tried the code you posted on another topic as listed below. I am just missing a basic concept of VBA. So I have numbers between 1-30 in cells a:1-a:10. I paste the code in as instructed, but none of the formatting changes and the only thing I get is that when I try to change one of the numbers I now get an error message. WHAT AM I NOT GETTING HERE???

code:
--------------------------------------------------------------------------------


Private Sub Worksheet_Change(ByVal Target As Range)
'Getting past Conditional Formattings 3 Criteria Limit
'This code must be placed in the Private Module of the Worksheet. _
To get there right click on the sheet name tab and select "View Code".
'Excel's very handy Conditional Formatting unfortunately only allows up to 3 conditions. _
The method below gets around this limit. It is set to work on A1:A10 only. event.

Dim icolor As Integer
If Not Intersect(Target, Range("A1:A10")) Is Nothing Then
Select Case Target
Case 1 To 5
icolor = 6
Case 6 To 10
icolor = 12
Case 11 To 15
icolor = 7
Case 16 To 20
icolor = 53
Case 21 To 25
icolor = 15
Case 26 To 30
icolor = 42
Case Else
'Whatever you want
End Select
Target.Interior.ColorIndex = icolor
End If

End Sub

Hi kaptep, you asked quite a few questions here so I'll just answer this one first then you can specify the problem again from there. Im not sure if you understand the concept of the above code so Ive included a brief bit on the worksheet_change event.

1. Worksheet_Change event
Some macros are designed to run when you ask them to run (by selecting Tools|Macro|Macros) while others run automatically based upon an event. The above code is the second type of macro and it needs to be stored in the Sheet module as opposed to stand alone module. If its not in there, then the code wont run.

Right click the sheet concerned and select View Code then paste the code in.

2. What does If Not Intersect etc mean?
The worksheet_change event runs every time you change the value in a cell (but not formatting). So this means that the code will run on every change, even though you may only want to format certain cells. This will slow your workbook down as its doing unnecessary changes.

To solve this you first check whether the cell that was changed is in the area where you want to do formatting. Thats what this line does.

The Intersect(Target, Range("A1:A10")) Is Nothing part means see if the target (I'll explain that next) doesnt intersect with a particluar range, in this case A1:A10. We actually want to see if it does intersect, so we use the If Not in front - a double negative type of operation.

In fact this line has an error as target should actually be Range(Target.Address) but this needs to be changed anyway as per my next comment

3. Target & Flaw In This Code
I mentioned the value Target before. Target is an argument automatically included with the Worksheet_Change event and represents the range of cells that were changed in the last action by the user.

Target can refer to multiple cells (like A1:A10) or can be one cell. You would have multiple cells changed if you copied and pasted for example. With the above code it does not cater for multiple cells being changed, so will return an error if you change more than one cell.

To fix this, use this code instead. Note that a new variable called c is created and used which represents an element(cell) in the range.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Getting past Conditional Formattings 3 Criteria Limit
'This code must be placed in the Private Module of the Worksheet. _
To get there right click on the sheet name tab and select "View Code".
'Excel's very handy Conditional Formatting unfortunately only allows up to 3 conditions. _
The method below gets around this limit. It is set to work on A1:A10 only. event.

Dim icolor As Integer, c

For Each c In Target
    If Not Intersect(Range(c.Address), Range("A1:A10")) Is Nothing Then
        Select Case c.Value
        Case 1 To 5
            icolor = 6
        Case 6 To 10
            icolor = 12
        Case 11 To 15
            icolor = 7
        Case 16 To 20
            icolor = 53
        Case 21 To 25
            icolor = 15
        Case 26 To 30
            icolor = 42
        Case Else
            icolor = 0 'no color
        End Select
    c.Interior.ColorIndex = icolor
    End If
Next c

End Sub

If you want me to explain anything more let me know. :)

hth
 
Upvote 0
Parry, (and everyone else) THANK YOU VERY MUCH FOR THE HELP! I really appreciated all the explanation because I actually have some idea of what I am trying to do now. I am going to try this out again and do a little more research on my own and hopefully I can not only get this to work but also understand how to use it later. I may be back with more questions though, so I hope you will check back with me. Thanks again!! :biggrin:
 
Upvote 0
Your welcome. :biggrin:

The above code has various colours changed on the value of the cell. If you want to use different colours then you need to find its number value as represented in the ColorIndex property.

To find the numbers of different colours do this.

1. Colour one cell
2. Run the macro then write down the number so you can later use in your macro.

Note, this macro is only designed to find the value of one cell so dont select multiple cells when you run it.

Place in a normal stand alone module
Code:
Sub FindColor()
MsgBox Selection.Interior.ColorIndex
End Sub
 
Upvote 0
YES, I still have questions

Okay, I think I got all that, now I am ready for my next lesson. So the value (1-12) in the cells I want formatted is based off equations and if statements. So the formatting updated great if I actually change the cells, but what I actually want to change is the cells referenced in the equations. How do I gett the formatting to update as a result of the formula result inside the cell changing rather than the cell themselves being changed?
 
Upvote 0
For a solution that doesn't need any VBA support and uses worksheet cells to simulate a color effect, see the all new tutorial on the Excel/Tutorials/Multiple Conditional Formats page of my web site.
 
Upvote 0
Re: YES, I still have questions

kaptep said:
Okay, I think I got all that, now I am ready for my next lesson. So the value (1-12) in the cells I want formatted is based off equations and if statements. So the formatting updated great if I actually change the cells, but what I actually want to change is the cells referenced in the equations. How do I gett the formatting to update as a result of the formula result inside the cell changing rather than the cell themselves being changed?

Hi, Im not sure that I understand you. Could you give an example of a formula and describe what should happen.

eg Cell A1 has this formula
=IF(C1=100,1,2)

If cell A1 = 1 then colour cell C1 Blue
If cell A1 = 2 then colour cell C1 Green.

If this is the case then I would just conditionally format cell C1 to either Blue or Green based on whether its 100 or not.

Do you have > 3 nested IF statements or else you wouldnt ask for formatting on >3 conditions?
 
Upvote 0
12 conditions

Right now I need to have 12 conditions. The basic idea is that the color codes reflect phases of a project. The information on what phase each of several project is on is coming from several differnt sources but is summarized by a number 1-12 in these cells. The color formatting is used for presentation purposes to create a visual representation of the schedule and progress for the project.

=IF(ISERROR(N45)=TRUE,O44,N45) is an example of one of the cell formulas. The cells referenced in this equation then refer to vlookups and if statements.

So, I get the "hard work" done by getting the correct number 1-12 in each cell. Then I convert this number to a color format through the macro. But I then change the targeted finish date--or the hours required or a number of other factors that the result is based on--the number in the cell updates correctly, but the color formatting does not. That is where I am stuck.
 
Upvote 0
Tusharm's website (refrenced above) illustrates what I am looking for a lot better than I can explain it. Except that for my purposes I need all of the data and representative collors in 1 column rather than 4. (I also doing this over about 600 rows and 30 projects/columns)

Thanks! Tusharm for the link. I am going to check out some other things on your site it looks like you have some good info.
 
Upvote 0
I don't understand why you believe the solution has to be one column. After all, one column of width 10 occupies more space than four columns each of width 1!

Also, you indicated this is to track the phases of various projects. I would argue that by 'moving' the colored pattern you are providing additional visual information to the user!

To me a non-programmatic solution is usually easier and cleaner to implement -- and maintain.

But, it's your task and you know your environment better than I do. ;)

If the 1-12 value is the result of a formula, you cannot use the Worksheet_Change event. You will have to use the _Calculate event. That event provides no information about what cells have changed! So, each time XL recalculates anything on the worksheet, you will have to check every cell of interest to you -- all 600 * 30 cells.

kaptep said:
Tusharm's website (refrenced above) illustrates what I am looking for a lot better than I can explain it. Except that for my purposes I need all of the data and representative collors in 1 column rather than 4. (I also doing this over about 600 rows and 30 projects/columns)

Thanks! Tusharm for the link. I am going to check out some other things on your site it looks like you have some good info.
 
Upvote 0

Forum statistics

Threads
1,215,097
Messages
6,123,079
Members
449,094
Latest member
mystic19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top