Sub topic 8
Writing formula-based rules
This chapter assumes you can already write simple formulas such as =A1+B1 and select ranges. It also assumes you have applied a built-in rule from the Home tab. Here you will write your own rules, which handle cases the built-in rules cannot. Examples are whole-row highlighting, conditions that involve several columns, and tests against other cells.
Why formulas are needed
The built-in Highlight Cells Rules test one cell against one condition, and they format only that cell. A formula-based rule can look at any cell and format any other cell.
Typical jobs that need a formula:
- Highlight an entire row because of one cell in it.
- Combine conditions, such as "overdue and not done".
- Compare a cell with another column.
- Count how often a value occurs.
- Apply a pattern, such as banded rows.
Creating a formula rule
- Select the cells you want to format. Start the selection at the cell you want to be the active cell, usually the top-left one.
- On the Home tab, choose Conditional Formatting, then New Rule.
- Choose Use a formula to determine which cells to format.
- Type the formula in the box Format values where this formula is true.
- Choose Format, set the fill, font or border, and confirm with OK twice.
The formula must start with =. It must produce TRUE or FALSE. Excel also accepts numbers, where 0 counts as false and any other number counts as true. The rule formats a cell only when the formula is true for that cell.
How Excel evaluates the formula
You write the formula once, but Excel evaluates it for every cell in the Applies to range. It treats the formula as if you had typed it in the active cell, and for every other cell it shifts the relative references. This is the same idea as copying a formula down and across.
Take a rule applied to A2:E50 with the active cell at A2 and the formula =$C2>1000:
- For A2, Excel tests
$C2>1000. - For B2, the column is locked with
$, so it still tests$C2>1000. - For A3, the row is relative and moves down, so it tests
$C3>1000.
The consequence is that the formula's references are relative to the active cell, not to the whole range. Most formula-rule mistakes come from this point, and the next chapter covers it in detail. In this chapter, the rule is that the formula is written from the point of view of the first cell in the range.
Formatting a whole row
To highlight a row based on one column, select the whole table body, lock the column, and leave the row relative.
=$C2>1000Select A2:E50 so that A2 is the active cell, then create the rule. Every cell in a row checks the same cell in column C, so the whole row lights up together. Without the $, the formula would become D2>1000 in column B, E2>1000 in column C, and so on. The highlighting would then look scattered.
Do not include the header row in the selection. If row 1 holds headers, the header text would be compared with 1000. Text is greater than any number in Excel's comparison rules, so the header would be highlighted.
Combining conditions
Use AND, OR and NOT to build compound tests. Each returns TRUE or FALSE, so they work directly in a rule.
=AND($D2<TODAY(), $E2<>"Done")This highlights rows whose due date in column D has passed and whose status in column E is not "Done". Both conditions must be true. Use OR when either is enough.
You can also build a test by multiplying or adding conditions, but AND and OR are easier to read months later. Prefer them.
Text in a formula goes in double quotes. A test for blank cells uses "".
=AND($B2<>"",$C2="")This highlights rows where column B has an entry but column C is still empty. It is useful for spotting incomplete records. The <>"" test is TRUE for any cell that holds something, and ="" is TRUE for an empty cell.
Counting with COUNTIF
A rule can use functions that look at a whole range. This rule marks every cell in column A whose value appears more than once in A2:A100.
=COUNTIF($A$2:$A$100,$A2)>1The counting range $A$2:$A$100 is fixed with $ on both parts, so it stays the same for every cell. The criterion $A2 has a relative row, so each cell counts how many times its own value occurs. If the count is above 1, the cell is a duplicate.
The built-in Duplicate Values rule does something similar, but the formula version lets you change the logic. For example, you could count duplicates across two columns, or ignore blank cells by adding $A2<>"" with AND.
Patterns that do not depend on values
A formula can also depend on the position of the cell.
=MOD(ROW(),2)=0ROW() returns the row number of the cell being evaluated. MOD(ROW(),2) is the remainder after dividing by 2, so it is 0 on even rows. The rule shades every even-numbered row. Because the test uses the real row number, the banding stays correct when rows are sorted or filtered. Banding that you apply by hand does not.
One caveat: after filtering, hidden rows still count in ROW(). The visible rows may therefore not alternate every time. Excel tables have their own banded-row style that avoids this problem.
Typing the formula without breaking it
The formula box has a few habits that catch people out.
Other points worth knowing:
- The formula is saved relative to the active cell at the moment you create the rule. If you create the rule with the wrong cell active, the rule will test the wrong cells. Delete it and recreate it, or correct the references in the Rules Manager.
- Rules can refer to cells on other worksheets in current versions of Excel. In older versions you needed a named range.
- A rule's formula cannot change any values. It can only return a result that decides the format.
- If the formula has a syntax error, Excel often refuses to save it. A wrong but valid formula is saved and simply never matches, so test your formulas.
Testing a rule before you trust it
Test the formula in a spare cell first. Enter it in the same position as the active cell, for example A2, then copy it across and down. Look at the TRUE and FALSE results. If they match what you expect, paste the formula into the rule. Delete the test cells afterward.
This method shows you the exact result for each cell. It is much faster than looking for a faulty formula in the dialog.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Nothing is highlighted | Formula never returns TRUE, or the Applies to range is wrong | Test the formula in a cell and check the range |
| Everything is highlighted | Absolute reference such as `$C$2`, or a formula that is always true | Change to `$C2` and test again |
| Highlight is shifted by one row | Rule created with a different active cell | Rewrite the reference from the first cell of the range |
| Only the first column is highlighted | Formula has no `$` on the column | Add `$` before the column letter |
| Header row is highlighted | The range includes the header | Exclude row 1 from the range |
| Text test fails | Text not in double quotes | Write `"Done"` in quotes |
Summary of the working model
- A formula rule is a
TRUEorFALSEtest that Excel repeats for each cell in the range. - The formula is written from the viewpoint of the active cell, and relative references shift for the other cells.
- Lock a column with
$to format whole rows, and fix a range with$on both parts when you count or look it up. - Check the formula in a worksheet cell before you put it in a rule.
Exercise
A table occupies A2:E50 with headers in row 1. You want every row to be shaded when the value in column B is "Late". You select A2:E50 with A2 active and enter the formula =B2="Late". Only column B is shaded, and in other columns the shading depends on the wrong cells. What is wrong, and what formula should you use?
Show answer
The reference B2 has a relative column. In column C Excel tests C2="Late", in column D it tests D2="Late", and so on, so each column checks itself. The column must be locked with a dollar sign: =$B2="Late". The row stays relative so each row checks its own column B cell.
Exercise
Write a rule formula for A2:A100 that highlights a value only if it appears more than once and the cell itself is not blank.
Show answer
=AND($A2<>"",COUNTIF($A$2:$A$100,$A2)>1). The AND stops blank cells from being flagged, and the fixed counting range $A$2:$A$100 stays the same for every cell while $A2 follows the row.