Answers / Question 21

Conditional formatting custom formula highlights the wrong rows: relative vs absolute references ($B2, $B$2, B$2)

Why a custom-formula rule colors the row below, every row or only one column, how the formula is read from the top-left cell of the range, and which dollar signs to use in Google Sheets and Excel.

The problem

You set up a Custom formula is rule to color a row when its status says Done. The formula looks right, but the color lands one row off, or every row lights up, or only some columns change color in a pattern that doesn't match the status. Editing the formula seems to move the problem around rather than fix it.

Inspired by a public question on Stack Overflow. The problem above is described in our own words and the solution below is ours.

How the rule reads your formula

You write the formula once, for the top-left cell of the Apply to range. For every other cell in the range, the spreadsheet shifts the references by the same distance, exactly as if you had copied the formula into that cell. A dollar sign stops the shift: $B keeps the column fixed, $2 keeps the row fixed.

So for a table in A2:E100 with the status in column B, the rule is written for cell A2:

Apply to A2:E100: color the whole row when its status is DoneFormula
=$B2="Done"

In cell C7 that rule checks B7: the column stays on B because of the $, and the row moves from 2 to 7 because it has none.

Match the symptom to the fix

Same table (A2:E100, status in B), four ways to write the rule:

If the color is off by several rows, check the Apply to range: it may have been changed (say from A2 to A5) while the formula still refers to row 2. Change one to match the other.

Useful patterns, and which parts to lock

Overdue and unpaid: due date in D, status in E (apply to A2:E100)Formula
=AND($D2<>"", $D2<TODAY(), $E2<>"Paid")
Amount above a limit typed in H1 (apply to A2:E100)Formula
=$C2>$H$1
Largest value in each column (apply to B2:E100)Formula
=B2=MAX(B$2:B$100)
Shade every other row (apply to A2:E100)Formula
=ISEVEN(ROW())

The rule of thumb: lock what should stay put. A settings cell like H1 is locked both ways ($H$1). A per-row value is locked by column only ($C2). A range that each column checks against itself is locked by row only (B$2:B$100).

Excel: the same rule, with two traps

In Excel, choose Home → Conditional Formatting → New Rule → Use a formula to determine which cells to format. The formula is read relative to the active cell when you create the rule. That's normally the top-left of your selection, but if you selected from the bottom up, the active cell is at the bottom and the rule shifts accordingly. Select from the top-left, or check the white cell in the selection before you start.

Also, clicking a cell while typing the formula inserts a fully locked reference such as $B$2. Press F4 until it shows $B2. Excel needs fixed end rows in ranges (B$2:B$100, not B$2:B).

Good to know

Formulas follow documented Google Sheets / Excel behavior; they aren't machine-tested, so try them on a copy first. Always try new automations on a copy of your sheet first.