Conditional Formatting: Adding Customized Rules to Excel 2007
Summary: The conditional formatting user interface (UI) in Excel 2007 now makes it easier than ever to create rules that increase the impact of your data. Learn how to take advantage of the powerful conditional formatting options from the UI and programmatically. (6 printed pages)
Frank Rice, Microsoft Corporation
March 2007
Applies to: 2007 Microsoft Office Suites, Microsoft Office Excel 2007
Contents
-
Overview
-
An Improved User Interface
-
Controlling Rules Programmatically
-
Conclusion
-
All About Conditional Formatting in Excel 2007
Overview
One of the primary goals of the Excel 2007 team when they were planning updates to conditional formatting was to make a greater number of scenarios possible without the need to write formulas. Let me briefly explain what I mean. Excel 2003 gives you two choices when creating conditional formats: you can either set up numerical comparisons (for example, this cell is greater than a value, less than a value, equal to a value, etc.), or you can use a formula to specify conditions. While formulas are powerful and flexible, they take more time to implement and typically require a deeper knowledge of the product to set them up. For example, it can be challenging to set up conditional formatting rules like "highlight the top 20% of this range."
When doing research, the team found that there were a set of very common things that users wanted to do with conditional formatting, many of which required writing formulas, although not all were obvious. Based on these findings, a number of new conditional formatting rules are added in Excel 2007 to address the most common customer scenarios. These rules are broken down into a few categories in the new user interface (UI) to make it easier to find the correct rule type. Specifically, without writing formulas, you can conditionally format cells:
-
With data bars, color scales, or icons based on the numeric value in the cell, percentages, percentiles, or a formula. For more information on these features, see All About Conditional Formatting in Excel 2007.
-
With conditional operators such as "Containing," "Not Containing," "Beginning With," or "Ending With" specific text. For example, "highlight parts containing certain characters in a parts catalog."
-
Containing dates that match dynamic conditions like "Yesterday," "Today," "Tomorrow," "In the Last 7 Days," "Last Week," "This Week," "Next Week," "Last Month," "This Month," and "Next Month." For example, "highlight all items dated yesterday." A big plus with these types of rules is that Excel 2007 handles calculating the date based on the system clock, so you do not need to worry about updating the rules for conditions like leap years.
-
To test whether they are blank or not blank.
-
To test whether they have errors or do not have errors.
-
To see if the data is in the top n of a selected range (where n is some number) OR that the data is in the top n percent of a selected range (again, where n is adjustable). For example, "highlight the top 10 investment returns in a table of 1,000 investments."
-
That have the bottom n values or cells that are the bottom n percent of a selected range.
-
That are "Above Average," "Below Average," "Equal To or Above average," "Equal to or Below average," 1 standard deviation above, 1 standard deviation below, 2 standard deviations above, 2 standard deviations below, 3 standard deviations above, and 3 standard deviations below a selected range.
-
That contain duplicate values or, conversely, cells that contain unique values.
-
That are based on comparisons between two columns in tables. For example, "highlight values where the values in the Actual Sales column are less than in the Sales Target column." When working with tables, it is now easy to format the entire row based on the results of a condition.
Note: |
|---|
|
You can still create the conditional formatting rule types from Excel 2003 in Excel 2007 worksheets. |
Other benefits of the changes to conditional formatting in Excel 2007 are the ability to specify more than three conditions, to reorder conditions, and to have more than one condition resolve to True.
An Improved User Interface
Let’s take a look at the more advanced conditional formatting UI and how it allows you to address these and other conditions. First, let’s look at the Conditional Formatting Rules Manager dialog box:
-
Start Excel 2007.
-
On the Home tab, click Conditional Formatting and then click Manage Rules. The Conditional Formatting Rules Manager is shown (see Figure 1):
Figure 1. The Conditional Formatting Rules Manager dialog box
The Conditional Formatting Rules Manager dialog box is the one-stop place to view and manage all of the conditional format rules in a workbook. Using this dialog, you can:
-
View the rule condition, see the range it is applied to, and the formatting applied by the rule. The Show formatting rules for list at the top of the dialog box allows you to change the scope of the rules. You can view rules for the selected cells, the current worksheet, or other worksheets in the workbook.
-
Add new rules with the New Rule button. (More on this shortly.)
-
Change the order in which rules are evaluated. This is accomplished by using the up and down arrow buttons.
-
Edit existing rules with the Edit Rule button. The Edit Rule UI is the same as the New Rule UI.
-
Delete rules with the Delete Rule button.
-
Control whether more than one rule can evaluate to True. (We'll discuss this in more detail later in this column.)
When you press New Rule on the Conditional Formatting Rules Manager dialog box, or when you select More Options from the conditional formatting gallery, you see the New Formatting Rule dialog box (see Figure 2):
Figure 2. The New Formatting Rule dialog box
This dialog allows you to add new conditional formats to the selected range. Through this UI, you can add all the conditional formats that are available through the Ribbon as well as a set of additional, slightly more sophisticated conditional formats. In addition, you can change the different settings and parameters on any conditional format rule. The conditional formatting options are grouped into a few categories that are listed in the top of the New Formatting Rule dialog box under Select a Rule Type. The bottom part of the dialog contains the Edit the Rule Description section, which changes based on the rule type selected. Here is a summary of the categories:
-
Format all cells based on their values. Use this to create a data bar, 2-color or 3-color color scale, or icon set rule.
-
Format only cells that contain. Use this option to create the Excel 2003-style rules and more (format cells greater than, less than, greater than or equal to, less than or equal to, equal to, not equal to, between, not between). This is also the entry point to create rules of type: specific text, date occurring, blanks, no blanks, errors, no errors.
-
Format only top or bottom ranked values. Use this to create top n, top n%, bottom n, bottom n% rule types.
-
Format only values that are above or below average. Use this to create above, below, equal or above, equal or below, 1 or 2 or 3 standard deviation above, or 1 or 2 or 3 standard deviation below rule types.
-
Format only unique or duplicate values. Use this to create rules that format unique or duplicate values.
-
Use a formula to determine which cells to format. Use this to create Excel 2003-style rules where you can enter a formula to determine whether a format should be applied.
Finally, as I touched on briefly in some of my previous columns, there are significant improvements in the infrastructure of conditional formatting. For example, Excel 2007 introduces the following changes:
-
Number of conditions on a range is no longer limited by number. In Excel 2003, you could create conditional formatting rules with three conditions. In Excel 2007, you are no longer limited by number; you are only limited by system memory.
-
Multiple conditions evaluating to true. In Excel 2003, you could apply only one conditional format to a given cell. If more than one conditional format evaluated to true, then only the format associated with the first conditional format was applied.
Excel 2007 now allows you to have multiple conditional formats on a cell applied if more than one condition evaluates to True. For example, assume you have one conditional format that makes font style bold when true, and another that makes the cell background color red when true. If both conditions are true, you see both formats applied (bold text and red cell background color). When there are conflicts when applying rules, (for example, apply red font AND green font), the first rule wins. You can trigger this effect on or off for any rule by using the checkboxes on the Conditional Formatting Rules Manager dialog box.
Controlling Rules Programmatically
In most scenarios, anything you can do in the UI, you can also do programmatically. The Excel 2007 object model supports all conditional formatting functionality that is supported in the UI. This includes creating, editing, or deleting rules, as well as changing the priority of rules.
As those of you who have written conditional formatting by using Microsoft Visual Basic for Applications (VBA) in previous versions of Excel are aware, the FormatConditions collection is derived from the Range object. The following examples demonstrate how this new functionality is exposed in the FormatConditions collection.
Creating a Rule
You can create the new conditional formatting features introduced in Excel 2007 (Databars, Color Scales, Icon Sets, Top n) using the Add method in the FormatConditions collection. For example, to add a Databar object, use:
Editing the Rule
To edit the rule, by using an index into the FormatConditions collection, you then modify the correct properties. For example, to change the bar color, use:
In this statement, the number 1 indexes the first rule on the range.
Editing the Priority
Excel 2007 introduces the idea of rule priorities to support multiple conditions on a range. The priority determines the order of application of the rule. In the object model, you have the Priority property on the FormatConditions object. This property is tracked at the worksheet level. For example, to verify the priority of a rule, use:
To make this rule the lowest priority:
To assign a specific priority:
Note that if you had three rules, setting the priority to 3 and using SetLastPriority has the same effect.
Deleting the Rule
You can delete a specific rule by using its index and then calling the Delete method:
To delete all rules in the specific range, call the Delete method on the FormatConditions collection.
References to Other Worksheets
In Excel 2003, you could not refer to ranges in other worksheets in a conditional formatting rule. Excel 2007 allows cell references anywhere in the workbook.
Support for Number Formatting
In Excel 2007, the Number tab in the Format Cells dialog box is available so that you can set conditional formatting to be a number format.
Conclusion
As you see from this series of Office Talk columns, conditional formatting in Excel 2007 is much easier to apply, with many more options, than in previous versions. The new Fluent UI gives you much more control and versatility in the way you visually present data. You can further refine this control through the FormatConditions collection in the VBA object model. I would encourage you to experiment with conditional formatting on your own data to maximize its impact on your customers.
In another column, I discuss using the new conditional formatting features with Microsoft PivotTable views.
All About Conditional Formatting in Excel 2007
For more in this series about Conditional Formatting in Excel 2007, see the following resources:
-
Conditional Formatting: Adding Custom Conditional Formatting to Excel 2007
-
Conditional Formatting: Adding Customized Color Scales to Excel 2007
-
Conditional Formatting: Adding Customized Data Bars to Excel 2007
-
Conditional Formatting: Adding Customized Icon Sets to Excel 2007
-
Conditional Formatting: Adding Customized Color Scales to Excel 2007
Offer Total MTD
row1 10K 4.5K
row2 12K 7.5K
Thanks for any ideas,
Jason
Response: Jason, you have the right idea removing the $. The $ makes a reference sticky, but there are 2 parts of a cell reference: Column and Row, so there are 2 places you might want to use the $.
Let's clarify your example to a 3 color scale to highlight the rows if MTD<10% of total, between 10% and 50%, and above 50%
Assuming "Offer" resides in cell A1, you would highlight cells A2 through C3 and then create a new conditional formatting rule, and "Use a formula to determine which cells to format".
- Format values where this formula is true: "=C2>B2*0.5" - Enter everything inside the " marks but not the " marks
- Set the Format you want to apply if the condition is true
Conditional Formatting will take the formula you set exactly as you entered it, and apply it to the top left cell in the range to be formatted (A2 here). Every subsequent cell cheked to the right of A2 will shift your formula over by the same number of cells. So if you used the formula above, when it checks cell B2, it will apply the formula as =D2>C2*0.5. The same goes for rows: For every row below A2 it will shift the formula down by the same number of rows, so when it checks cell B3, it will apply the formula as =D3>C3*0.5. By adding $, you can lock the column or row of a cell reference in a formula.
By changing the formula above to "=$C2>$B2*0.5" you can get the formula to always compare the "Total" column to the "MTD" column, but it will change the row in the formula to keep pace with the row being formatted.
Repeat the steps above with the following formula: "=$C2<=$B2*0.5" and set the format for cells that meet that condition
Repeat the steps above with the following formula: "=$C2<$B2*0.1" and set the format for cells that meet this condition
Now you have three rules in the rules manager box. It will compare each cell in the range you set (A2 through C3) in the order that the rules are listed in this box from top to bottom. If you have an entry in the MTD column that is less than 10% of the value of the entry in the Total column, it would meet the conditions of two of your formulas (<50% of total AND <10%). Whichever rule is highest in the rule manager box will be applied FIRST, but then the next rule down that applied would change the format again.
Rearrange the rules to make the most specific rule lowest (here =$C2<$B2*0.1). Now, if an MTD is less than 10% of total, it will meet the condition of <50% of total and have that format applied, then when it checks to see if it is <10% it will change to that format. Make sure that none of the "Stop If True" boxes are checked.
Hope that helps
I use the numbers to keep a precentage of the report for the boss's to show how their employee's are doing.
I would like to write a formula that would be allow me to use the numbers in Col A to format the client ID in Col B.
Suggestions?
- 1/27/2012
- csrich
Im having trouble creating the right formula. For example, I would like cellA6 to become highlighted yellow if the amount in
CellD6 becomes <CellE6. Is there a way to do this? I would appreciate any help.
RESPONSE: I think you have to do some code in VB for that. -Jibbs
RESPONSE: (6/1/2011) Excel 2007: Select Column A > "Home" tab > "Manage Rules". "Show formatting rules for:" should be "Current Selection". Click "New Rule". Select the Rule Type of "Use a formula to determine which cells to format". "Format values where this formula is true" will be: =$A1<$D1 Click the "Format" button. Go to your "Fill" tab > Choose the color you would like under the "Background Color". This color should then show up in the "Sample" box. Click "OK" and "OK" and "Apply". If you already have data in your columns A & D, the cells meeting the critera in Column A should turn the color you chose. -Sheila
Hiow about if I have a column of cells and I want to compare each to a single cell for my conditional formatting. Do I have to set up a separate rule for every single cell? - Rob
- 2/24/2011
- Jay Mongii
- 11/28/2011
- raweber
I want to set up a spreadsheet with conditional formatting where midrange numbers are the target, and highs and lows are flagged.
E.g. If I could use >5, and <2 makes a red flag appear or shade the cell red, and numbers 2 through 5 show a green flag or fill the cell in with green
The icons seem to only work where high numbers are given the green flag, check mark, etc, midrange get yellow, and low numbers get red, X, etc.
I managed to do this with color scales, but I don't want shades of colors, just one specific color for numbers within the range, and a second color for outside of the range (no shading as the numbers approach the range)
Can anyone help?
THANK YOU!
- 10/19/2011
- mikeh4700
I have a tracking sheet which have lot of dates in it and status in the next column.
I need that if status is "pending" and the date is > today(), then it should be highlighted with red.
Can anybody please help.
- 8/9/2011
- Mak22
- 9/1/2011
- Thomas Lee
- 7/1/2011
- geoff72
I have actually added a bar with c# I just have trouble with setting formula1 and bar color! Please example with that.
- 6/1/2011
- Wiktor Jespersen
I have tried the OR's/Not's/And's as well as the recommended formulas on the forum and still do not get the desired result . What am i doing wrong?
- 5/24/2011
- Ragnaroc
- 9/19/2010
- jdog1056
Conditional formatting is based on if a cell is blank or filled- if cell is filled another cell will show in green "Available" and if cell is blank the other cell will show in red "Not Available"
I have placed this spreadsheet out on our network drive share and others can open the file and all works as it should. I open the same file and it will not work.
I have had our IT department check my version, my personal settings, what else could be causing this?
Any suggestions would be greatly appreciated!!
- 9/8/2010
- Stevepwallace
1. Ignore if cell is empty, and, 2. Ignore if cell value equals "A".
(If I only use ( <> "A") then all empty cells format conditionally.)
In a range that describes a specific row I tried using two separate rules,
(If cell value <> ""), plus (if cell value <> "A") I experimented with Stop If True checked and unchecked.
I also tried embedding an AND function in a single rule, (If cell value <> AND("","A")
I tried OR instead of AND, I even tried, cell value = NOT(OR("","A")) , and cell value = NOT(AND("","A"))
The formatting fails in all cases.
Is there a bug, or am I just logically challenged?
================ After Kurt Clement's Response =========================
Thank you Kurt!
Allow me to reiterate; I am trying to apply the conditional formatting to a Range.
If I apply a fixed reference to each individual cell, that’s a LOT of conditional Formats!
===============================================================
=AND(NOT($B$23=""),NOT($B$23="A")) is what I am attempting - for EVERY cell in range $B$23:$CN$23.
So it seems the 2007 Rule Type must be "Format only cells that contain", rather than "Use a formula ..."
The rule accepts formula entries, but I think it treats them as just text strings.
On a secondary observation, If I enter: OR("","A")
, click OK, then edit the rule again, I find: ="OR("""",""A"")" This has yet to provide a working rule.
With so many Quotes supplied by Excel, many of my formulas must be re-edited.
Why is Excel doing that?
=AND($F$1=1,$F$2=2)
The same applies to the OR function
=OR($F$1=1,$F$2=2)
And you can next them
=AND($F$1=1,OR($F$2=2,$F$3=3))
I hope this helps someone. I tested in Excel 2007 and it worked.
- 2/24/2010
- Kurt Clement
Excel 2007, using Conditional Formatting, Above or Below Averate, Standard Deviation function. When using any one of the Standard Deviation (SD) checks, the Conditional formatting works correctly. When using one upper (say, plus 1) and one lower (-1) SD, it works correctly. However, when setting up for all six cases, I wanted to set up six different Conditional Formatting backgrounds for each of the six possible SD limits. The goal was to have it color one shade of Red for >= 3SD's, another shade of Red for <= -3SD's, and then shades of yellow for 2SD's and green for 1SD's, leaving the cell white for anything between +1 and -1 SD.
In other words, shade all the cells in a range according to how many SD's they were, positive or netagive.
I have stacked the rules many different ways and tried many different approaches, but I am convinced that the SD function does not work when all 6 rules are created. This bug evidences itself when 6 rules are created, in that it will not apply the correct formatting. Because the rules stack priority top down, I placed the 3SD limits at the top, then the 2SD, then the 1SD. If a 3SD limit is exceeded, any lower one in the stack is ignored. However, with all 6 rules, it doesn't behave according to the anticipated logic
Please contact me for an example.
Tim Jones
tim.jones (at) L-3com.com
- 1/25/2010
- riversofbelief
