Sub topic 6
Writing Custom Formula Rules
Assume you can select ranges, enter simple formulas, and read cell references such as A2 and $C2. A custom formula rule lets you use a Google Sheets formula to decide whether a cell or row receives formatting. The formula must evaluate to TRUE for the formatting to appear.
Custom formulas are useful when the built-in conditions do not describe your situation. For example, you can highlight an entire task row when its status is Done, mark repeated values, or flag overdue tasks that are still open.
How a custom formula is evaluated
To create a rule:
- Select the range to format.
- Open Format > Conditional formatting.
- In Format cells if, choose Custom formula is.
- Enter a formula beginning with
=. - Choose the formatting style.
- Select Done.
The formula is evaluated as if it were entered for the first cell in the selected range. Google Sheets then adjusts relative references for the other cells.
For example, suppose your task table occupies A2:F100:
| A | B | C | D | E | F |
|---|---|---|---|---|---|
| Task | Owner | Status | Due date | Priority | Notes |
To format the whole row when column C contains Done, apply the rule to A2:F100 and use:
=$C2="Done"For row 2, the formula checks C2. For row 3, it checks C3, and so on. The formula returns TRUE for rows whose status is Done.
Use dollar signs to control movement
A dollar sign makes part of a cell reference absolute, meaning that part does not change when Sheets evaluates the formula in another cell.
C2: column and row can both change.$C2: column C stays fixed, but the row can change.C$2: row 2 stays fixed, but the column can change.$C$2: both column C and row 2 stay fixed.
For a row-based rule, $C2 is usually the correct reference. Every cell in the row checks column C, while each row checks its own status.
If you use C2 instead, the reference can move across columns. When the rule applies to A2:F100, a cell in column D may check F2 rather than C2. That can cause the row to highlight unexpectedly or not at all.
If you use $C$2, every row checks only C2. The rule may appear to work for the first row but will not evaluate each row independently.
>callout note >Always compare the formula with the top-left cell of the apply range. If the range starts at A2, write the row-based formula as though it is checking row 2.
Build conditions with functions
A custom formula can use functions such as AND, OR, COUNTIF, MOD, and TODAY.
To highlight duplicate values in column A, apply this rule to A2:A100:
=COUNTIF($A$2:$A$100,$A2)>1The first range is absolute because the rule should always search the complete list. The final reference, $A2, keeps the column fixed while allowing the row to change.
To highlight overdue tasks whose status is not Done, apply this rule to A2:E100:
=AND($D2<TODAY(),$E2<>"Done")This checks whether the due date in column D is earlier than today and whether the status in column E is anything other than Done. Both column references stay fixed, while the row changes.
You can also create alternating row shading. Apply the following rule to the desired range:
=MOD(ROW(),2)=0ROW() returns the current row number. MOD returns the remainder after division, so the formula is TRUE for even-numbered rows.
Test and troubleshoot the rule
When a rule does not behave as expected, check these points:
- The Apply to range starts on the row used in the formula.
- The formula begins with
=. - Text values use quotation marks, such as
"Done". - The column anchors, such as
$C, point to the intended columns. - Dates are real date values, not text that only looks like a date.
- The formula returns
TRUEfor at least one test row. - Another conditional-formatting rule is not changing the visible result.
You can test the logic in an unused cell by entering the formula without the conditional-formatting rule. A result of TRUE means the condition matches that row.
Exercise
You have a table in A2:F100. Column E contains task status, and you want to format the entire row whenever the status is "Blocked". What apply-to range and custom formula should you use?
Show answer
Use A2:F100 as the apply-to range and use =$E2="Blocked". The dollar sign fixes the status column while the row number changes for each task.