Conditional Formatting Rules Simplified

imageThanks to Amit Velingkar for writing this blog post.  (We are re-publishing this post first published in April 2010.  Why? Because so many of you keep searching for it. This is the second in a series of oldies but goodies Excel posts.) 

Conditional formatting is a popular feature and is a great way to easily identify cells with a range that meet some criteria. However, users often want to create conditional formatting rules that go beyond comparing a cell’s value to a single value or a single cell reference - row or column comparisons are commonly requested operations. In this blog post, we will learn how to use relative references in conditional formatting rules to make such tasks easier.

For example, consider the spreadsheet below where we have sales data for different branches of an organization over the years 2005 to 2009:

image

Let’s say we want to highlight the cells where sales have decreased compared to the previous year. By default, conditional formatting inserts an absolute reference when we select another cell as a reference. (ExcellsFun has a great video that explains relative and absolute cell references). When absolute references are used in conditional formatting rules, every cell to which the conditional formatting rule is applied is compared to that single referenced cell.

 

Tedious Way – Lots Individual Conditional Formatting Rules

For complex conditional formatting rules, users often get around that problem by creating lots of individual rules applied to single cells. In our example, we select cell C2 and click on “Conditional Formatting -> Highlight Cell Rules -> Less Than” on the home tab. When the Less Than dialog pops up, we select the cell B2 against which we want to compare. Notice that the value is the dialog gets replaced by an absolute reference to cell B2 ($B$2).

image

To perform column comparisons in this fashion, the user ends up having to create a new rule for each cell and select the neighboring cell for comparison.

image

This small table consists of as many as 20 individual rules - each comparing the cell’s value to its neighbor on the left. Besides being a tedious task (users often automate such tasks by using subroutines), there are other problems with this approach:

- Performance hit: Having lots of individual conditional formatting rules can degrade performance.

- Difficult to Maintain: Such rules are hard to maintain. Besides having to edit multiple rules for any changes, the rules won’t adjust if you insert a new row or column.

 

Relative References – using a single Conditional Formatting Rule

There is an easier way to do this by using just one Conditional Formatting Rule applied to the entire range. To do this, select the entire range of cells to which you want to apply the rule (in our example: C2:F6) and click on “Conditional Formatting -> Highlight Cell Rules -> Less Than” on the home tab. When the Less Than dialog pops up, we again select the cell B2 to get an absolute reference ($B$2). However, this time we use change the reference to be a relative reference (B2).

TIP: you can use the F4 key to cycle through different types of relative references until you get B2 without the $ signs.

And there you have it; we have just one Conditional Formatting rules where every cell’s value is compared. We get exactly, the same result as before. And there’s more, the rules automatically adjust to if new rows are added in the middle of this range. For example, let us add information about a new branch in Boston.

image

image

 

How Relative References work in CF Rules?

In conditional formatting rules, cell references are relative to the top-left most cell in the Applies to range. To simplify things, I like to pretend as if I have only selected the top-left most cell and I am writing a formula in that particular cell. In our case, the top-left cell C2 was compared against the cell to its left (B2). Hence, all the other cells in the range were compared with the cell to their left.

The diagrams below are examples of how to setup two commonly used CF rules – row and column comparisons. The cells with blue borders indicate the range to which this CF rule is applied, the bold blue border is used to illustrate the top-left most cell in the CF range and the red border shows the cell referenced by the CF formula. The black arrow indicates the direct relationship between the top-left cell and the cell reference, while the gray arrows indicate the implied relationships that are formed as a result.


imageimage

You can always verify this by using the Conditional Formatting Rules Manager (select any cell within this range and clicking on the “Conditional Formatting -> Manage Rules” on the home tab) as shown below:

 

image

Also, you can toggle between the different types of relative references to compare against a particular column or a particular row. Relative references can also be used inside formulas to create complex Conditional Formatting rules.

Summary

Anytime you need to conditional formatting using complex cell comparisons, consider using relative references to make things easier. Remember, rules are relative to the top-left most cell in the Applies To range. I would love to hear more feedback on Conditional Formatting and ideas that you might have for future versions of Excel.

Office Blogs Comments

Comments: (19) Collapse

  • Just found that after the Excel file is saved and reopened, the Rules Manager will consolidate accordingly the rules and so Excel 2007 will show the rules as I would expect it to, so there is no inappropriate behavior of Excel 2007 for the second method that I show in my demo file. Still, for the first method used (the same that I use in Excel 2003 and the method that I would say is the more intuitive to use), there is the bug: a cell that has the conditional formatting applied to it when it should have not had it. Maybe there is some setting in Excel 2007 telling it to extend formatting to neighboring cells? That would explain the "bug".

  • Can anyone help?

    I want to format a table in a particular way. The font in the table is set as webdings so that if someone puts an "a" in a cell, it "ticks" the cell. What I want to do is format the table so that when I "tick" a cell, the whole row is highlighted.

  • It sounds to me as if you want "partial relative formatting".  So; you want a row of numbers (I to M) to be checked with the main number residing in column G, right ?  And this can apply to several rows. (2 to 10 for exampe).

    In that case the same example as above would apply, but you'd want to fix the column to check ('G') while the row (2 to 10) should remain variable (so that the same rule can be applied onto the whole range ($i$2:$m$10 for example)).

    So you'd use cell value $G2 which is then applied to your range of $I$2:$M$10.

  • Your formula seems to contain an error. "m5$A$1" points to no where. The OR formule needs a logical operand, but simply putting 2 cells in there isn't counted as such. Same applies to IF, it needs a condition yet you didn't specify it (note how "=1" sits outside the IF formule.