How-to recipes · Formatting
How to use conditional formatting with a formula in Excel
Colour cells or whole rows based on your own rule, such as overdue and unpaid invoices, using a formula rule with the right $ references.
The built-in conditional formatting rules (greater than, duplicate values, top 10, data bars) cover simple cases, but real rules are usually more specific: colour the whole row of an invoice that is past its due date and not yet paid; highlight weekends in a calendar; mark employees whose certificate expires within 30 days. For all of these you write a formula that returns TRUE or FALSE, and Excel formats every cell where it is TRUE. It works the same in every version from Excel 2010 to Microsoft 365.
The example data
Five invoices in A1:D6 with an amount, a due date and a status. To keep the example reproducible, today's date (03 December 2026) is in F1; in a real sheet you would use TODAY(). The goal: colour every invoice that is overdue and still open.
Step 1: write and test the formula in a cell
Before creating the rule, test the logic in a spare column. The rule must return TRUE for the rows to colour:
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| 1 | Invoice | Amount | Due | Status | Overdue? | 2026-12-03 |
| 2 | INV-201 | 1200 | 2026-11-20 | Paid | ||
| 3 | INV-202 | 860 | 2026-11-28 | Open | TRUE | |
| 4 | INV-203 | 430 | 2026-12-15 | Open | ||
| 5 | INV-204 | 2150 | 2026-11-30 | Open | ||
| 6 | INV-205 | 690 | 2026-12-01 | Paid |
INV-202 was due on 28 Nov and is still Open, so the test is TRUE.
Result: TRUE
Copied down, the test returns FALSE, TRUE, FALSE, TRUE, FALSE: INV-202 and INV-204 are overdue. INV-201 is past due but paid; INV-203 is not yet due. Testing in cells first is the single best habit for formula rules, because the rule dialog shows no results and no errors.
Step 2: create the rule
- Select the whole data range A2:D6, starting from A2 so that A2 is the active cell.
- Go to Home > Conditional Formatting > New Rule.
- Choose Use a formula to determine which cells to format.
- Enter the formula for the active cell's row:
=AND($C2<TODAY(),$D2<>"Paid"). - Click Format, choose a light red fill, and click OK twice.
Every cell of rows 3 and 5 turns red. When an invoice is marked Paid, or a new day makes another one overdue, the colours update automatically. You can delete the test column now.
Understanding the dollar signs
The rule is written for the active cell (A2) and then applied to every other cell in the selection, adjusting the references exactly as if the formula were copied. That is why the $ signs decide what happens:
| Reference | Effect | Use for |
|---|---|---|
$C2 | column fixed, row changes | colour the whole row based on one column |
C$2 | row fixed, column changes | colour whole columns based on a header row |
$F$1 | both fixed | a single input cell, such as a threshold |
C2 | both change | each cell tests itself |
Forgetting the $ before C is the most common mistake: then cell B2 would test D2, cell C2 would test E2, and the colours appear in random-looking places. Cell references: relative, absolute and mixed explains mixed references.
More useful rule formulas
| Goal | Rule formula (active cell in row 2) |
|---|---|
| Due within the next 7 days | =AND($C2>=TODAY(),$C2<=TODAY()+7) |
| Weekend dates in a calendar row | =WEEKDAY(B$1,2)>5 |
| Row contains the word refund | =ISNUMBER(SEARCH("refund",$B2)) |
| Value above the column average | =B2>AVERAGE(B$2:B$100) |
| Customer appears in a blacklist | =COUNTIF(Blacklist,$A2)>0 |
| Every other row | =MOD(ROW(),2)=0 |
Each one returns TRUE or FALSE. Functions such as WEEKDAY, SEARCH and COUNTIF work in rules exactly as in cells.
Managing rules
Open Home > Conditional Formatting > Manage Rules and set Show formatting rules for to This Worksheet to see every rule. Rules are evaluated from top to bottom; tick Stop If True to prevent lower rules from also applying. The Applies to box shows and edits the range, which is where to fix rules that were accidentally split into many pieces by copying and inserting rows.
Pitfalls
- Relative references to the wrong row. If you select A2:D6 but A6 is the active cell (because you selected from the bottom), write the formula for row 6.
- Text vs numbers.
$B2>1000is TRUE for any text, because text sorts after numbers. Make sure the column contains real numbers. - Empty cells count as 0 or as a date in 1900, so
$C2<TODAY()is TRUE for an empty due date. Add$C2<>""to the AND. - Performance. Hundreds of rules over whole columns with volatile functions (TODAY, INDIRECT, OFFSET) slow the workbook. Apply rules to the used range only.