Sub topic 8
Writing Formula-Based Rules
This chapter assumes you can select ranges, write simple formulas such as =A1+B1, and apply a fill color. It also assumes you have used the built-in rules on the Home tab.
Why use a formula rule
The built-in rules compare a cell with a fixed value or with other cells in the same range. They cannot look at a different column. A formula rule can. You write a formula that returns TRUE or FALSE, and Excel applies your format to every cell where the result is TRUE.
Use a formula rule when you want to:
- Highlight a whole row based on one column.
- Combine several conditions.
- Compare a cell with a value in another column or on another sheet's named range.
Create a formula rule
- Select the range to format, for example
A2:E50. Keep the active cell at the top left, hereA2. - Choose Home > Conditional Formatting > New Rule.
- Choose Use a formula to determine which cells to format.
- Type the formula in the box. It must start with
=. - Choose Format, pick a fill or font color, and select OK twice.
Excel writes the formula as if it were entered in the active cell. It then adjusts the references for every other cell in the range. This is why the position of the active cell matters.
How Excel evaluates the formula
Any non-zero number also counts as TRUE, and zero counts as FALSE. The formula does not have to be a comparison.
Highlight a whole row
Select A2:E50 with A2 active and use this formula:
=$C2>1000The dollar sign locks the column to C, so every cell in a row checks the same value in column C. The row number has no dollar sign, so each row checks its own column C value. This is a mixed reference. The next chapter covers these in detail.
Combine conditions
Use AND when all conditions must be true, and OR when any one is enough.
=AND($B2<>"",$C2="")This highlights rows where column B has an entry but column C is empty. It is useful for spotting incomplete records.
Find duplicates with a counting formula
=COUNTIF($A$2:$A$100,$A2)>1The counting range $A$2:$A$100 is fully locked, so it stays the same for every cell. The criterion $A2 moves down with each row. Apply this rule to A2:A100.
Band alternate rows
=MOD(ROW(),2)=0ROW() returns the row number of the cell being tested. MOD returns the remainder after dividing by 2, so even rows give 0 and are shaded. The banding stays correct after sorting or filtering, which manual fills do not.
Common mistakes
| Mistake | Result | Fix |
|---|---|---|
| Formula typed without `=` | Excel treats it as text and nothing is formatted | Start with `=` |
| Active cell not at top left | Highlighting is shifted by one or more rows | Edit the rule and retype the formula for the correct cell |
| Missing `$` before the column | Only the first column of a row-wide rule works | Use `$C2`, not `C2` |
| Clicking cells while typing | Excel inserts absolute references such as `$C$2` | Type the references by hand |
Exercise
A sheet has data in A2:E50 and you want each row shaded when column D equals "Late". You select A2:E50 with A2 active. What formula do you enter, and why is the dollar sign needed?
Show answer
Enter =$D2="Late". The dollar sign locks the column to D, so every cell in the row checks column D. The row number stays relative, so each row checks its own value. Without the dollar sign, cell B2 would test E2, and the shading would be wrong.
Exercise
Write a rule formula for A2:A100 that highlights any value appearing more than once in that range.
Show answer
=COUNTIF($A$2:$A$100,$A2)>1