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:
=$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:
=$B2="Done"→ correct: each row checks its own status.=$B1="Done"→ the color is one row off: every row checks the row above it. The formula row (1) doesn't match the first row of the range (2). Write the formula for the range's first row.=$B$2="Done"→ all rows or none: every cell checks B2, so the whole table follows the first record. Remove the$before the row number.=B2="Done"→ only part of the row follows the status. Written for A2,B2means “one column to the right”, so column A checks B, column B checks C, column C checks D, and so on. Put a$before the column letter.
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
=AND($D2<>"", $D2<TODAY(), $E2<>"Paid")=$C2>$H$1=B2=MAX(B$2:B$100)=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
- In Google Sheets a custom formula can't point at another tab directly. Wrap the reference in INDIRECT, for example
=$B2>INDIRECT("Settings!B1"). - When two rules apply to the same cell, the one higher in the list wins. Drag rules into the order you want in the side panel (Sheets) or the Rules Manager (Excel).
- The formula must produce TRUE (color it) or FALSE (leave it). For rows that repeat a value, see our answer on highlighting rows with duplicate values.
- Inserting or deleting rows inside the range updates the rule automatically. Pasting cells copied from elsewhere can split a rule into several ranges; check Apply to range afterwards and merge them back into one.
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.