Sub topic 5
Relative and Absolute References in Rule Formulas
What you need first
This chapter assumes you can select a range such as A2:F100 and type a simple formula into a cell. It also assumes you have opened the Format > Conditional formatting sidebar and know how to choose Custom formula is under "Format cells if". The earlier chapters on choosing the range and on single color rules cover those steps.
Custom formula rules apply one formula to many cells, and the formula usually gives a different answer for each cell. The $ signs decide which parts of the formula move from cell to cell and which stay fixed. Most rules that "highlight the wrong cells" have a mistake in these anchors.
The anchor cell rule
When you write a custom formula, you write it for one cell only: the top-left cell of the "Apply to range" box. Google Sheets then evaluates the formula for every other cell in the range, shifting each relative reference by the same offset, as if you had copied the formula across and down.
Suppose the rule applies to A2:F100 and the formula is =$C2="Done". Sheets reads this as "for cell A2, check C2". For B2 it uses the same column C. For A3 it moves down one row and checks C3. The formula is always a question about the cell being formatted.
If the formula returns TRUE, the cell gets the format. If it returns FALSE, the cell is left alone. Any non-boolean result, such as an error, counts as no match. A reference to a cell outside the range is allowed, and that is how a rule can compare each cell with a threshold stored elsewhere.
The four reference types
A cell reference has two parts, the column letter and the row number. A $ in front of a part locks it. Without a $, the part is relative and shifts with the cell being evaluated.
| Written as | Column | Row | Behavior |
|---|---|---|---|
| `C2` | relative | relative | Moves both ways. Cell one column right and one row down checks D3. |
| `$C2` | locked | relative | Always column C. The row follows the cell. |
| `C$2` | relative | locked | Always row 2. The column follows the cell. |
| `$C$2` | locked | locked | Always the single cell C2. |
The names to remember are "column-locked" ($C2), "row-locked" (C$2) and "fully locked" ($C$2). Pick the type by asking two questions about the formula you want:
- As the rule moves to the right, should the column it looks at change?
- As the rule moves down, should the row it looks at change?
If the answer to the first is no, put $ before the column letter. If the answer to the second is no, put $ before the row number.
Worked example: highlighting whole rows
You have a task list in A1:F100 with headers in row 1. Column C holds the status. You want every row whose status is "Done" shaded green.
- Select A2:F100. Starting at row 2 keeps the header out of the rule.
- Open Format > Conditional formatting.
- Under "Format cells if", choose Custom formula is.
- Enter the formula below and pick a green fill.
=$C2="Done"Every cell in the row asks the same question about column C, so the whole row turns green together. The row number is relative, so row 3 checks C3, row 4 checks C4, and so on.
What goes wrong without the dollar sign
Now write the formula without the anchor:
=C2="Done"Cell A2 checks C2, which works. Cell B2 checks D2, cell C2 checks E2, and cell D2 checks F2. Row 3 is shifted one column further for each cell. Instead of whole rows, you get a scattered pattern of single cells, often stepping diagonally. The pattern happens because both parts of the reference are free to shift, and the column part should not have been.
The clue is that a rule "works" for exactly one column and looks random elsewhere. When you see that, check for a missing $ before the column letter.
Choosing anchors for other layouts
The same two questions give the right answer for layouts beyond row highlighting.
Locking the row: formatting by header
You have monthly figures in B2:M50, and row 1 holds month labels. You want every cell under the header "Dec" shaded. The test depends on the column, not the row, so lock the row:
=B$1="Dec"Applied to B2:M50, cell B2 checks B1, cell C2 checks C1, and cell B3 still checks B1. The column shifts, and the row stays at 1.
Locking both: comparing with one threshold cell
Suppose cell H1 holds a target, and you want any value in B2:B100 above it highlighted. Every cell should compare against the same cell, so lock both parts of H1. The cell being tested stays relative:
=B2>$H$1Without the $ signs, B3 would compare against H2, B4 against H3, and so on. Those cells are probably empty, and a comparison with an empty cell silently treats it as 0, so almost everything would highlight.
Locking part of a range: comparing within a row
To highlight the largest value in each row of B2:F100, compare each cell with the maximum of its own row:
=B2=MAX($B2:$F2)The first B2 is fully relative because it names the cell being formatted. In $B2:$F2 the columns are locked, so every cell in the row looks at the same five values. The row is relative, so row 3 uses $B3:$F3. This is the same pattern as =$C2="Done", applied to a range instead of a single cell.
Ranges inside rule formulas follow the same rules as single references. $A$2:$A$100 is the same range from every cell. $A2:$A100 is a mistake in most cases, because the range slides down by one row for each row you move, and the rule ends up comparing against a window that shrinks or runs past the data.
Counting within a column
The duplicate-detection pattern needs two kinds of reference in one formula:
=COUNTIF($A$2:$A$100,$A2)>1The first argument is the fixed list, so it is fully locked. The second argument is the value being tested. It is column-locked because the rule applies to a single column, and row-relative so each cell tests its own value. When the rule covers only one column, $A2 and A2 behave the same, but the $ is a good habit because it keeps the rule correct if you later widen the range to more columns.
Where relative references start counting
Relative references are measured from the top-left cell of the range in the "Apply to range" box. That has three practical consequences.
- Write the formula for the first row of the range, not for row 1 of the sheet. If the range is A5:F100, the formula should refer to row 5 (
=$C5="Done"). If you write=$C2="Done"there, every cell is compared with a row three above it, and the highlight lands on the wrong lines. - Changing the start of the range changes the meaning. If you edit the range from A2:F100 to A3:F100 later, check the formula afterwards. Reading the rule again from the new first cell is the quickest way to confirm that it still points at the right cells.
- Multiple ranges share one formula. If you list two ranges in the box, such as
A2:A50, C2:C50, one formula is applied to both, and its relative references are interpreted from the first range's top-left cell. Rules with several ranges are harder to reason about. When the ranges need different anchors, make a separate rule for each.
Edge cases that look like anchoring bugs
Some rules are anchored correctly and still highlight cells you did not expect. Check these before you rewrite the formula.
- Blank cells count as zero. A rule such as
=$C2<5is TRUE for an empty C2, because an empty cell compares as 0. Add a blank test:=AND($C2<>"",$C2<5). - Text and numbers differ. A cell showing 5 as text is not less than 10 in the way a number is. Text compares as larger than any number, so a rule like
=$C2<10does not match it. Check with=ISNUMBER($C2)if a value does not respond. - Case does not matter in equality.
=$C2="done"matches "Done" and "DONE". UseEXACTif you need case-sensitive comparison. - Volatile functions recalculate.
TODAY()in=AND($D2<TODAY(),$E2<>"Done")is re-evaluated whenever the sheet recalculates, so highlighting changes over time with no edit from you. This is the intended behavior for overdue dates. - The formula must start with
=. Without it, Sheets treats the entry as text to compare, and nothing matches.
A quick method for checking anchors
Before you save a custom formula rule, run through this short test.
- Identify the first cell of the apply range. Write that address down.
- Substitute it into the formula mentally. Does the formula make sense for that cell?
- Move one cell right. Which references should shift? Which should not?
- Move one cell down. Repeat the same check.
- Look at the far corner of the range. Do the references still land on real data?
You can also test the formula in an empty cell first. Type the formula into the first cell of the range's neighbor column, fill it across and down, and look at the TRUE and FALSE pattern. If the pattern is what you want, the same formula works in the rule.
Exercises
Exercise
A rule applies to A2:E50 with the formula =B2>100. The goal is to shade whole rows where column B exceeds 100, but only some cells in a diagonal pattern are shaded. What is wrong, and what is the corrected formula?
Show answer
Both parts of B2 are relative, so each cell looks at a different column as the rule moves right. Cell C2 checks D2, cell D2 checks E2, and so on. Lock the column so every cell in a row checks column B: =$B2>100.
Exercise
Cells B2:M30 hold figures with month names in row 1. Write a custom formula that shades every cell in the column whose header is "Total", and explain which part of the reference is locked and why.
Show answer
=B$1="Total". The row is locked because every cell must look at the header in row 1, no matter how far down it is. The column is relative because each column must check its own header.
Exercise
A rule applies to A5:D80 and uses =$C2="Done". The highlighted rows are three rows above where they should be. Why does this happen, and how do you fix it?
Show answer
Relative references are measured from the first cell of the apply range, which is row 5 here. The formula was written for row 2, so every cell checks a row three above itself. Rewrite it for the first row of the range: =$C5="Done".