Sub topic 6
Writing custom formula rules
This chapter assumes you can select ranges such as A2:F100 and write simple formulas such as =A2>B2 in a cell. It also assumes you have used the built-in conditions (for example "Text contains" or "Greater than") in the conditional formatting sidebar. Here you replace those conditions with your own formula.
What a custom formula rule does
A custom formula rule asks one question for every cell in its range: "Is this formula TRUE for this cell?" If the answer is TRUE, the cell gets the format. If the answer is FALSE, the cell is left alone.
Built-in conditions look only at the cell being formatted. A custom formula can look at any cell, so you can:
- Format a whole row based on one column.
- Compare two columns.
- Combine several tests with AND and OR.
- Count how often a value appears.
To create one:
- Select the range, for example A2:F100.
- Open Format > Conditional formatting.
- Under Format rules, open the Format cells if... dropdown and choose Custom formula is.
- Type a formula that starts with
=. - Choose a fill color or text style, then click Done.
How Sheets evaluates your formula
You write the formula once, but Sheets evaluates it for every cell in the range. It treats what you typed as if it sat in the top-left cell of the range, then shifts the references for every other cell, the same way copying a formula would.
If the range is A2:F100, Sheets reads your formula as written for A2. For cell B2 it shifts every relative column reference one column right. For cell A3 it shifts every relative row reference one row down. References locked with $ do not shift in that direction.
This is why the $ anchors from the previous chapter matter. The anchors decide which parts of the formula move and which stay fixed.
Worked example: highlight a whole row
Suppose a task list has headers in row 1 and data in A2:F100. Column C holds the status. You want every row whose status is "Done" shaded green.
- Select A2:F100.
- Choose Custom formula is.
- Enter the formula below.
=$C2="Done"The formula is written for A2. Here is how it behaves as Sheets moves across the range:
| Cell being checked | Formula Sheets evaluates | Why |
|---|---|---|
| A2 | `=$C2="Done"` | Starting point |
| B2 | `=$C2="Done"` | `$C` is locked, so the column does not move |
| F2 | `=$C2="Done"` | Same column check for every cell in the row |
| A3 | `=$C3="Done"` | `2` is relative, so the row moves down |
Every cell in a row asks the same question about column C of its own row. When C is "Done", the whole row turns green.
Compare the two mistakes you can make with the anchor:
=C2="Done"(no$) checks column C for A2, column D for B2, and so on. You get a diagonal scatter of formatted cells.=$C$2="Done"(both locked) checks only C2. Either the whole range is formatted or none of it is.
Matching the formula to the first row
The most common source of wrong highlighting is an off-by-one row. If the range is A2:F100 but the formula says =$C1="Done", every row looks one row too high. The row marked "Done" is not the row that gets colored. The row above it is.
If you enter several ranges in one rule, such as A2:A100, D2:D100, Sheets uses the top-left cell of the first range as the reference point. Keep the formula anchored to that cell. If the ranges start at different rows, split them into separate rules.
Building blocks for your formulas
Custom formulas can use most of the functions you already know. These are the most useful for formatting.
| Goal | Formula pattern | Notes |
|---|---|---|
| Compare two columns | `=$B2>$C2` | Highlights rows where B is bigger than C |
| Test several values | `=OR($C2="Blocked",$C2="Waiting")` | TRUE if either matches |
| Require several conditions | `=AND($D2<TODAY(),$E2<>"Done")` | TRUE only if both are true |
| Test for an empty cell | `=$B2=""` | TRUE for empty or empty-text cells |
| Match text by pattern | `=REGEXMATCH($A2,"(?i)^error")` | Case-insensitive match at the start |
| Count occurrences | `=COUNTIF($A$2:$A$100,$A2)>1` | Used to find duplicates |
| Alternate rows | `=MOD(ROW(),2)=0` | Even-numbered rows |
Combining conditions with AND and OR
An overdue-task rule needs two facts: the due date has passed, and the task is not finished. Apply this to A2:E100, with the due date in column D and the status in column E.
=AND($D2<TODAY(),$E2<>"Done")TODAY() returns today's date, so $D2<TODAY() is TRUE for any earlier date. $E2<>"Done" is TRUE for every other status, including an empty one. AND requires both to be TRUE.
Two details affect the result:
- Column D must hold real dates. If a date is stored as text, the comparison silently fails. A real date aligns to the right in the cell by default, and text aligns to the left.
TODAY()is recalculated whenever the sheet recalculates, so the highlighting updates on its own as days pass.
Finding duplicates
Apply this to A2:A100.
=COUNTIF($A$2:$A$100,$A2)>1Both parts of the range are locked with $ because the counted range must stay the same for every cell. The criterion $A2 is relative in the row, so each cell counts how often its own value appears. A count above 1 means the value has a duplicate. Both copies are highlighted, not only the second one.
Empty cells can cause surprises here. To skip blanks, add a test:
=AND($A2<>"",COUNTIF($A$2:$A$100,$A2)>1)Alternating row shading
Applied to a range, this formats every even-numbered row.
=MOD(ROW(),2)=0ROW() with no argument returns the row number of the cell being evaluated. MOD(n,2) gives the remainder after dividing by 2, so it is 0 for even rows. Change the test to =1 for odd rows. Because it uses the sheet's row numbers, the pattern does not depend on where your range starts.
Case, text, and number behavior
A few behaviors catch people who are used to other tools:
- The
=comparison is not case-sensitive.=$C2="done"matches "Done", "DONE", and "done". - Comparing a number to text gives FALSE without an error. A cell containing the text "5" does not equal the number 5, and the rule just does not fire.
- A date compared with a number works, because dates are stored as numbers, but the result is hard to read. Use real dates on both sides.
- Strings need straight double quotes (
"Done"). Curly quotes pasted from a document make the formula invalid.
Referencing other sheets
A custom formula rule cannot point directly at another sheet. Typing =COUNTIF(Lists!A:A,$A2)>0 is rejected. The workaround is INDIRECT, which turns a text string into a reference. Apply this to A2:A100 to highlight values that appear in column A of a sheet named Lists.
=COUNTIF(INDIRECT("Lists!A:A"),$A2)>0If the sheet name contains spaces, wrap it in single quotes inside the string, for example INDIRECT("'Price List'!A:A"). If the name is mistyped, the formula errors and the rule never fires.
Common mistakes
- Formula does not start with
=. Sheets then reads it as invalid. - Wrong anchor. Locking the wrong part (see the earlier examples) gives diagonal or all-or-nothing formatting.
- Range and formula start on different rows. The highlighting lands one row off.
- Using a whole column in COUNTIF over a large range.
$A:$Aworks but recalculates more than a bounded range such as$A$2:$A$100. Prefer bounded ranges. - Expecting formatting to change cell values. A rule only changes appearance. It cannot be read by other formulas.
A reliable debugging habit is to test the formula in a spare cell first. Type your formula into the cell that matches the top-left of the range, then check that it returns TRUE or FALSE as expected. Copy it across a few cells to confirm the anchors behave. Once it works, paste it into the rule.
Exercises
Exercise
A rule is applied to A2:F100 with the custom formula =C2="Done". Rows are not highlighted as a whole. Instead, cells appear in a diagonal pattern. What is wrong, and what is the corrected formula?
Show answer
The column reference C is relative, so it shifts to D for column B, E for column C, and so on. Each cell checks a different column. Lock the column with a dollar sign: =$C2="Done".
Exercise
Your data is in A2:E100. Column B holds a budget and column C holds actual spending. Write a custom formula that highlights every row where actual spending is higher than the budget.
Show answer
Apply the rule to A2:E100 with =$C2>$B2. Both columns are locked and the row is relative, so every cell in a row compares that row's two values.
Exercise
A rule applied to A2:A100 uses =COUNTIF($A$2:$A$100,$A2)>1. Blank cells in the range are also being checked. Rewrite the formula so that blank cells are never highlighted.
Show answer
=AND($A2<>"",COUNTIF($A$2:$A$100,$A2)>1). The first test is FALSE for blank cells, so AND returns FALSE and they are skipped.