This chapter explores conditional formatting, one of Excel’s most versatile features. You can apply conditional formatting to a cell so that the cells look different, depending on its content.
Conditional formatting has improved significantly in Excel 2007, and it’s now a useful tool for visualizing numeric data. In some cases, you may be able to use conditional formatting in lieu of a chart.
Conditional formatting enables you to apply cell formatting selectively and automatically, based on the contents of the cells. For example, you can set things up so that all negative values in a range have a light-yellow background color. When you enter or change a value in the range, Excel examines the value and checks the conditional formatting rules for the cell. If the value is negative, the background is shaded. If not, no formatting is applied.
Conditional formatting is a useful way to quickly identify erroneous cell entries or cells of a particular type. You can use a format (such as bright-red cell shading) to make particular cells easy to identify.
Figure 21.1 shows a worksheet with nine ranges, each with a different type of conditional formatting rule applied. Here’s a brief explanation of each:
Greater than 10: Values greater than 10 are highlighted with a different background color. This rule is just one of many numeric value related rules that you can apply.
Above average: Values that are higher than the average value are highlighted.
Duplicate values: Values that appear more than one time are highlighted.
Words that contain X: If the cell contains X (upper or lower case), the cell is highlighted.
Data bars: Each cell displays a horizontal bar, proportional to its value.
Color Scale: The background color varies, depending on the value of the cells. You can choose from several different color scales or create your own.
Icon Set: One of many icon sets. It displays a small graphic in the cell. The graphic varies, depending on the cell value.
Icon Set: Another icon set.
Custom rule: The rule for this checkerboard pattern is based on a formula:
=MOD(ROW(),2)=MOD(COLUMN(),2)
To apply a conditional formatting rule to a cell or range, select the cells and then use one of the commands on the Home Styles Conditional Formatting drop-down to specify a rule. The choices are
Highlight Cell Rules: Examples rules include highlighting cells that are greater than a particular value, between two values, contain specific text string, or are duplicated.
Top Bottom Rules: Examples include highlighting the top 10 items, the items in the bottom 20 percent, and items that are above average.
Data Bars: Applies graphic bars directly in the cells, proportional to the cell’s value.
Color Scales: Applies background color, proportional to the cell’s value.
Icon Sets: Displays icons directly in the cells. The icons depend on the cell’s value.
New Rule: Enables you to specify other conditional formatting rules, including rules based on a logical formula.
Clear Rules: Deletes all the conditional formatting rules from the selected cells.
Manage Rules: Displays the Conditional Formatting Rules Manager dialog box, in which you create new conditional formatting rules, edit rules, or delete rules.
When you select a conditional formatting rule, Excel displays a dialog box that’s specific to that rule. These dialog boxes have one thing in a common: a drop-down list with common formatting suggestions. Figure 21.2 shows the dialog box that appears when you choose Home Styles Conditional Formatting Highlight Cells Rules Between. This particular rule applies the formatting if the value in the cell falls between two specified values. In this case, you enter the two values (or enter cell references), and then use the drop-down control to choose the type of formatting to display if the condition is met.
The formatting suggestions in the drop-down control are just a few of thousands of different formatting combinations. In most cases, none of Excel’s suggestions is what you want, so you choose the Custom Format option to display the Format Cells dialog box. You can specify the format in any or all of the four tabs: Number, Font, Border, and Fill.
The Format Cells dialog box used for conditional formatting is a modified version of the standard Format Cells dialog box. It doesn’t have the Number, Alignment, and Protection tabs; and some of the Font formatting options are disabled. The dialog box also includes a Clear button that clears any formatting already selected.
For do-it-yourself types, Excel provides the New Formatting Rule dialog box, shown in Figure 21.3. Access this dialog box by choosing Home Styles Conditional Formatting New Rules.
The New Formatting Rule dialog box lets you recreate all the conditional format rules available via the Ribbon, as well as new rules.
First, select a general rule type from the list at the top of the dialog box. The bottom part of the dialog box varies, depending on your selection at the top. After you specify the rule, click the Format button to specify the type of formatting to apply if the condition is met. An exception is the first rule type, which doesn’t have a Format button (it uses graphics rather than cell formatting).
Following is a summary of the rule types:
Format all cells based on their values: Use this rule type to create rules that display data bars, color scales, or icon sets.
Format only cells that contain: Use this rule type to create rules that format cells based on mathematical comparisons (greater than, less than, greater than or equal to, less than or equal to, equal to, not equal to, between, not between). You can also create rules based on text, dates, blanks, nonblanks, and errors. This rule type is very similar to how conditional formatting was set up in previous versions of Excel.
Format only top or bottom ranked values: Use this rule type to create rules that involve identifying cells in the top n, top n percent, bottom n, and bottom n percent.
Format only values that are above or below average: Use this rule type to create rules that identify cells that are above average, below average, or within a specified standard deviation from the average.
Format only unique or duplicate values: Use this rule type to create rules that format unique or duplicate values in a range.
Use a formula to determine which cells to format: Use this rule type to create rules based on a logical formula. See “Formula-Based Conditions,” later in this chapter.
This section describes the three conditional formatting options that are new to Excel 2007: data bars, color scales, and icons sets. These types of conditional formatting can be useful for visualizing the values in a range.
The data bars conditional format displays horizontal bars directly in the cell. Length of the bar is based on the value of the cell, relative to the other values in the range.
Figure 21.4 shows a simple example of data bars. It’s a list of customers and sales amounts. I applied data bar conditional formatting to the values in column B. You can tell at a glance where the higher values are.
The examples in the section are available on the companion CD-ROM. The workbook is named data bars examples.xlsx.
Excel provides quick access to six data bar colors via the Home Styles Conditional Formatting Data Bars command. For additional choices, click the More Rules option, which displays the New Formatting Rule dialog box. Use this dialog box to:
Show the bar only (hide the numbers)
Adjust how the bars relate to the values (use the Type and Value controls)
Change the color of the bars
Data bars are always displayed as a color gradient (from dark to light), and you can’t change the display style. Also, the colors used are not theme colors. If you apply a new document theme, the data bar colors do not change.
If you make adjustments in this dialog box, you can use the Preview button to see the formats before you commit to them with the OK button.
You may notice something odd about the data bars in Figure 21.4. Contrary to what you may expect, a cell with a zero value displays a data bar. Data bar conditional formatting always displays a bar for every cell, even for zero values. The smallest value in the range always has a bar length equal to 10 percent of the cell’s width. Unfortunately, Excel provides no direct way to modify the minimum percent setting. But, if you’re familiar with VBA, you can use a statement like the following to set the minimum display width for a range that uses conditional formatting data bars:
Range("B2:B123").FormatConditions(1).PercentMin = 1
After this statement is executed, the minimum value in the range will display a bar length equal to 1 percent of the cell’s width—and zero value cells will not display a data bar.
Using the data bars conditional formatting can sometimes serve as a quick alternative to creating a chart. Figure 21.5 shows a three-column table of data, with data bars applied in the third column. The third column of the table contains references to the values in the second column. The conditional formatting in the third column uses the Show Bars Only option.
Figure 21.6 shows an actual bar chart created from the same data. The bar chart takes about the same amount of time to create and is a lot more flexible. But for a quick and dirty chart, data bars are a good option—especially when you need to create several such charts.
The color scale conditional formatting option varies the background color of a cell based on the cell’s value, relative to other cells in the range.
Figure 21.7 shows a range of cells that use color scale conditional formatting. It depicts the number of employees on each day of the year. This is a 3-color scale that uses red for the lowest value, yellow for the midpoint, and green for the highest value. Values in between are displayed using a color within the gradient.
Excel provides four 2-color scale presets and four 3-color scales presets, which you can apply to the selected range by choosing Home Styles Conditional Formatting Color Scales.
To customize the colors and other options, choose Home Styles Conditional Formatting Color Scales More Rules. This command displays the New Formatting Rule dialog box, shown in Figure 21.8.
It’s important to understand that color scale conditional formatting uses a gradient. For example, if you format a range using a 2-color scale, you will get a lot more than two colors. You’ll get colors with the gradient between the two specified colors.
Figure 21.9 shows an extreme example that uses color scale conditional formatting on a range of 10,000 cells (100 rows X 100 columns). The worksheet is zoomed down to 20% to display a very smooth 3-color gradient. The range contains formulas like this one, in cell C5:
=SIN($A2)+COS(B$1)Values in column A and row 1 range from 0 to 4.0, in increments of 0.04.
When viewed on your screen, the result is stunning (it loses a lot when converted to greyscale).
Yet another conditional formatting option is to display an icon in the cell. The icon displayed depends on the value of the cells.
To assign an icon set to a range, select the cells and choose Home Styles Conditional Formatting Icon Sets. Excel provides 17 icon sets to choose from (and you can’t create your set of icons). The number of icons in the sets ranges from 3 to 5.
Figure 21.10 shows a simple example that uses the icon set named Three Symbols (Uncircled). The symbols graphically depict the status of each project, based on the value in column C.
All the icon set examples in this section are available on the companion CD-ROM. The workbook is named icon set examples.xlsx.
By default, the symbols are assigned using percentiles. For a 3-symbol set, the items are grouped into three percentiles. For a 4-symbol set, they’re grouped into 4 percentiles. And for a 5-symbol set, the items are grouped into five percentiles.
If you would like more control over how the icons are assigned, choose Home Styles Conditional Formatting Icon Sets More Rules to display the New Formatting Rule dialog box. Figure 21.11 shows how to modify the icon set rules such that only projects that are 100% completed get the check mark icons. Projects which are 0% completed get the X icon. All other projects get the exclamation point icon.
Figure 21.12 shows the task list after making this change.
Figure 21.13 shows a table that contains two test scores for each student. The Change column contains a formula that calculates the difference between the two tests. The Trend column uses an icon set to display the trend graphically.
This example uses the icon set named 3 Arrows, and I customized the rule:
Up Arrow: When value is >=5
Level Arrow: When value <5 and >= -5
Down Arrow: When value is >=5
In other words, difference of five points or less in either direction is considered an even trend. An improvement of more than five points is considered a positive trend, and a decline of more than five points is considered a negative trend.
In some cases, you may want to display only one icon from an icon set. Excel doesn’t provide this option directly, but displaying a single icon is possible if you use two rules. Figure 21.14 shows a range of values. Only the values greater than or equal to 80 display an icon.
Here’s how to set up an icon set such that only values greater than or equal to 80 display an icon:
Select the cells, choose Home Styles Conditional Formatting Icon Sets, and select any icon set. Keep in mind that only the last icon of the set will be used.
With the range selected, choose Home Styles Conditional Formatting Manage Rules. Excel displays its Conditional Formatting Rules Manager dialog box.
Click Edit Rule to display the Edit Formatting Rule dialog box.
Change the first icon setting to When Value Is >= 80 and specify Number as the Type; leave the other icon settings as they are, and click OK to return to the Conditional Formatting Rules Manager.
Click New Rule and then choose this rule type: Format Only Cells That Contain.
In the bottom section of the dialog box, specify Cell Value Less Than 80 and click OK to return to the Conditional Formatting Rules Manager. The range now has two rules.
Place a check mark next to Stop If True for the first rule. Figure 21.15 shows the completed dialog box.
Click OK.
The first rule checks to see whether the value is less than 80. If so, rule checking stops, and no conditional formatting is applied. If the value is greater than or equal to 80, the second rule kicks in. This rule indicates that values greater than or equal to 80 are displayed with an icon.
Excel’s conditional formatting feature is versatile, but sometimes it’s just not quite versatile enough. Fortunately, you can extend its versatility by writing conditional formatting formulas.
The examples later in this section describe how to create conditional formatting formulas for the following:
To identify text entries
To identify dates that fall on a weekend
To format cells that are in odd-numbered rows or columns (for dynamic alternate row or columns shading)
To format groups of rows (for example, shade every two groups of rows).
To display a sum only when all precedent cells contain values
Some of these formulas may be useful to you. If not, they may inspire you to create other conditional formatting formulas.
To specify conditional formatting based on a formula, select the cells and then choose Home Styles Conditional Formatting New Rule. This command displays the New Formatting Rule dialog box. Click the rule type labeled Use A Formula To Determine Which Cells To Format, and you’ll be able to specify the formula.
You can type the formula directly into the box, or you can enter a reference to an existing formula. As with normal Excel formulas, the formula you enter here must begin with an equal sign (=).
The formula must be a logical formula that returns either TRUE or FALSE. If the formula evaluates to TRUE, the condition is satisfied, and the conditional formatting is applied. If the formula evaluates to FALSE, the conditional formatting is not applied.
If the formula that you enter into the Conditional Formatting dialog box contains a cell reference, that reference is considered a relative reference, based on the upper-left cell in the selected range.
For example, suppose that you want to set up a conditional formatting condition that applies shading to cells in range A1:B10 only if the cell contains text. None of Excel’s conditional formatting options can do this task, so you need to create a formula that will return TRUE if the cell contains text and FALSE otherwise. Follow these steps:
Select the range A1:B10 and ensure that cell A1 is the active cell.
Choose Home Styles Conditional Formatting New Rule to display the New Formatting Rule dialog box.
Click the rule type labeled Use A Formula To Determine Which Cells To Format.
Enter the following formula in the Formula box:
=ISTEXT(A1)
Click the Format button to display the Format Cells dialog box.
In the Format Cells dialog box, click the Fill tab and specify the cell shading that will be applied if the formula returns TRUE.
Click OK to return to the New Formatting Rule dialog box (refer to Figure 21.16).
In the New Formatting Rule dialog box, click the Preview button to make sure that the formula is working correctly and to see a preview of your selected formatting.
If the preview looks correct, click OK to close the New Formatting Rule dialog box.
Notice that the formula entered in Step 4 contains a relative reference to the upper-left cell in the selected range.
Generally, when entering a conditional formatting formula for a range of cells, you’ll use a reference to the active cell, which is normally the upper-left cell in the selected range. One exception is when you need to refer to a specific cell. For example, suppose that you select range A1:B10, and you want to apply formatting to all cells in the range that exceed the value in cell C1. Enter this conditional formatting formula:
=A1>$C$1
In this case, the reference to cell C1 is an absolute reference; it will not be adjusted for the cells in the selected range. In other words, the conditional formatting formula for cell A2 looks like this:
=A2>$C$1
The relative cell reference is adjusted, but the absolute cell reference is not.
Each of these examples uses a formula entered directly into the New Formatting Rule dialog box, after selecting the rule type labeled Use A Formula To Determine Which Cells To Format. You decide the type of formatting that you apply conditionally.
The companion CD-ROM contains all the examples in this section. The file is named conditional formatting formulas.xlsx.
Excel provides a number of conditional formatting rules that deal with dates, but it doesn’t let you identify dates that fall on a weekend. Use this formula to identify weekend dates:
=OR(WEEKDAY(A1)=7,WEEKDAY(A1)=1)
This formula assumes that a range is selected and that cell A1 is the active cell.
The conditional formatting formula that follows was applied to the range A1:D18, as shown in Figure 21.17, to apply shading to alternate rows.
=MOD(ROW(),2)=0
Alternate row shading can make your spreadsheets easier to read. If you add or delete rows within the conditional formatting area, the shading is updated automatically.
This formula uses the ROW
function (which returns the row number) and the MOD
function (which returns the remainder of its first argument divided by its second argument). For cells in even-numbered rows, the MOD
function returns 0, and cells in that row are formatted.
For alternate shading of columns, use the COLUMN
function instead of the ROW
function.
The following formula is a variation on the example in the preceding section. It applies formatting to alternate rows and columns, creating a checkerboard effect.
=MOD(ROW(),2)=MOD(COLUMN(),2)
Here’s another rows shading variation. The following formula shades alternate groups of rows. It produces four rows of shaded rows, followed by four rows of unshaded rows, followed by four more shaded rows, and so on.
=MOD(INT((ROW()-1)/4)+1,2)
Figure 21.18 shows an example.
For different sized groups, change the 4 to some other value. For example, use this formula to shade alternate groups of two rows:
=MOD(INT((ROW()-1)/2)+1,2)
Figure 21.19 shows a range with a formula that uses the SUM
function in cell C6. Conditional formatting is used to hide the sum if any of the four cells above is blank. The conditional formatting formula for cell C6 (and cell C5, which contains a label) is
=COUNT($C$2:$C$5)=4
This formula returns TRUE only if C2:C5 contains no empty cells.
Figure 21.20 shows the worksheet when one of the values is missing.
This section describes some additional information about conditional formatting that you may find useful.
The Conditional Formatting Rules Manager dialog box is useful for checking, editing, deleting, and adding conditional formats. Access this dialog box by choosing Home Styles Conditional Formatting Manage Rules.
You can specify as many rules as you like by clicking the New Rule button. As you can see in Figure 21.21, cells can even use data bars, color scales, and icon sets all at the same time—although I can’t think of a good reason to do so.
Conditional formatting information is stored with a cell much like standard formatting information is stored with a cell. As a result, when you copy a cell that contains conditional formatting, you also copy the conditional formatting.
To copy only the formatting (including conditional formatting), use the Paste Special dialog box and select the Formats option.
If you insert rows or columns within a range that contains conditional formatting, the new cells have the same conditional formatting.
When you press Delete to delete the contents of a cell, you do not delete the conditional formatting for the cell (if any). To remove all conditional formats (as well as all other cell formatting), select the cell. Then choose Home Editing Clear Clear Formats. Or, choose Home Editing Clear Clear All to delete the cell contents and the conditional formatting.
To remove only conditional formatting (and leave the other formatting intact), use Home Styles Conditional Formatting Clear Rules.
Excel’s Find And Replace dialog box includes a feature that allows you to search your worksheet to locate cells that contain specific formatting. This feature does not locate cells that contain formatting resulting from conditional formatting.
You can’t tell, just by looking at a cell, whether it contains conditional formatting. You can, however, use Excel’s Go To dialog box to select such cells.
Choose Home Editing Find & Select Go To Special.
In the Go To Special dialog box, select the Conditional Formats option.
To select all cells on the worksheet containing conditional formatting, select the All option; to select only the cells that contain the same conditional formatting as the active cell, select the Same option.
Click OK. Excel selects the cells for you.