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.

Intermediate AND OR TODAY COUNTIF ISNUMBER SEARCH WEEKDAY MOD ROW

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:

E3fx=AND($C3<$F$1,$D3<>"Paid")
ABCDEF
1InvoiceAmountDueStatusOverdue?2026-12-03
2INV-20112002026-11-20Paid
3INV-2028602026-11-28OpenTRUE
4INV-2034302026-12-15Open
5INV-20421502026-11-30Open
6INV-2056902026-12-01Paid

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

  1. Select the whole data range A2:D6, starting from A2 so that A2 is the active cell.
  2. Go to Home > Conditional Formatting > New Rule.
  3. Choose Use a formula to determine which cells to format.
  4. Enter the formula for the active cell's row: =AND($C2<TODAY(),$D2<>"Paid").
  5. 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:

ReferenceEffectUse for
$C2column fixed, row changescolour the whole row based on one column
C$2row fixed, column changescolour whole columns based on a header row
$F$1both fixeda single input cell, such as a threshold
C2both changeeach 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

GoalRule 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>1000 is 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.
⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 12 KB

More how-to recipes