Sub topic 7
Common patterns: row highlighting, duplicates, and overdue dates
This chapter collects the custom formula patterns you will use most often. It assumes you can select ranges such as A2:F100, enter simple formulas, and open the conditional formatting sidebar (Format > Conditional formatting). It also assumes you have read the earlier chapters on custom formula rules and on relative and absolute references.
Each pattern below follows the same recipe. You choose the range, pick "Custom formula is" as the condition, and write a formula that returns TRUE or FALSE. Sheets formats each cell in the range where the formula returns TRUE.
The mental model: write the formula for the first cell
You write the formula as if it applied to the top-left cell of the range. Sheets then shifts the relative parts of the formula for every other cell.
Suppose the range is A2:F100 and the formula is =$C2="Done". For cell A2, the formula reads C2. For cell B2, it still reads C2, because the column is anchored with $. For cell A3, it reads C3, because the row is relative and moves down by one.
So the dollar sign decides what stays fixed, and the lack of one decides what moves.
| Reference | Column when copied right | Row when copied down | Typical use |
|---|---|---|---|
| `C2` | moves | moves | Format each cell by its own value |
| `$C2` | fixed | moves | Format a whole row by one column |
| `C$2` | moves | fixed | Format a whole column by one header cell |
| `$C$2` | fixed | fixed | Compare every cell to one single cell |
Pattern 1: highlight a whole row based on one column
Use this when a status column should color the entire record. In a task list with columns A to F, column C holds the status.
- Select A2:F100. Start at row 2 so the header row is excluded.
- Open Format > Conditional formatting.
- Under "Format cells if", choose "Custom formula is".
- Enter the formula below.
- Choose a fill color and select Done.
=$C2="Done"Every cell in a row turns the same color when column C in that row says Done. The comparison is not case sensitive, so "done" and "DONE" also match. A trailing space does match differently, though. "Done " is not equal to "Done", which is a common reason a rule seems to skip rows.
Variations on the row pattern
You can change the test without changing the structure.
- Match several values:
=OR($C2="Done",$C2="Closed"). - Match part of a text value:
=REGEXMATCH($C2,"Done"). - Match a number threshold:
=$B2>=1000. - Highlight blank statuses:
=$C2="". If the range includes empty rows below your data, this also colors those empty rows, so keep the range tight.
A text match with = must equal the whole cell. Use REGEXMATCH or SEARCH for a partial match.
Pattern 2: mark duplicates
To flag values that appear more than once in a column, count how often each value occurs.
Apply this to A2:A100:
=COUNTIF($A$2:$A$100,$A2)>1Read it in two parts. COUNTIF($A$2:$A$100,$A2) counts how many cells in the whole column range equal the current cell. The range is fully anchored, so it never moves. The criterion $A2 moves down one row for each cell. When the count is greater than 1, the value is a duplicate and both the first and later occurrences are formatted.
Highlighting only the repeats
Sometimes you want to keep the first occurrence plain and color only the later copies. Make the counted range grow as it goes down by anchoring only its start:
=COUNTIF($A$2:$A2,$A2)>1For row 2, the range is A2:A2. For row 3, it is A2:A3. The count reaches 2 only on the second appearance of a value.
Duplicates across several columns
To flag a row that repeats an entire combination, such as the same name and date, use COUNTIFS and apply the rule to the full row range, for example A2:C100:
=COUNTIFS($A$2:$A$100,$A2,$B$2:$B$100,$B2)>1Edge cases for duplicates
COUNTIFtreats criteria that begin with>,<, or=as operators. A cell containing text such as>5can match unexpectedly.- Wildcards matter. A cell containing
*or?is read as a wildcard in the criterion, so such values can match more than you expect. - Blank cells in the range are counted as matches for other blanks only in some situations. To avoid coloring empty cells, add a guard:
=AND($A2<>"",COUNTIF($A$2:$A$100,$A2)>1). - Numbers stored as text and real numbers can look identical but may not match. Check the alignment: text aligns left by default, numbers right.
Pattern 3: flag overdue dates
An item is overdue when its due date is before today and it is not finished. Suppose the due date is in column D and the status is in column E. Apply this to A2:E100:
=AND($D2<TODAY(),$E2<>"Done")AND returns TRUE only when both tests pass. TODAY() returns the current date, and Sheets recalculates it, so the formatting updates when the date changes.
Both references use $ on the column, so the whole row from A to E is judged by columns D and E. If you wanted only the due date cell colored, you would apply the rule to D2:D100 and write =AND($D2<TODAY(),$E2<>"Done") or the shorter =AND(D2<TODAY(),$E2<>"Done").
Related date tests
| Goal | Formula (first row is 2) |
|---|---|
| Due today | `=$D2=TODAY()` |
| Due within the next 7 days | `=AND($D2>=TODAY(),$D2<=TODAY()+7)` |
| Overdue by more than 30 days | `=$D2<TODAY()-30` |
| In the current month | `=AND(MONTH($D2)=MONTH(TODAY()),YEAR($D2)=YEAR(TODAY()))` |
Edge cases for dates
- An empty date cell counts as 0 in a comparison, which is far earlier than today. The formula
$D2<TODAY()is therefore TRUE for blank rows. Guard against it:=AND($D2<>"",$D2<TODAY(),$E2<>"Done"). - A date typed as text, such as
2026-13-40or a value left-aligned in the cell, is not a real date. Comparisons then return unexpected results. Select the column and check Format > Number. - A date with a time of day, such as 2026-11-28 14:00, is greater than
TODAY()on that day, becauseTODAY()is midnight. Wrap the cell inINT($D2)to drop the time. - The spreadsheet's time zone decides what "today" means. Check it under File > Settings.
Pattern 4: alternating row shading
To shade every even-numbered row, use the row number:
=MOD(ROW(),2)=0ROW() returns the row number of the cell being evaluated. MOD returns the remainder after dividing by 2. An even row gives 0, so the format applies. Use =MOD(ROW(),2)=1 for odd rows.
This approach has a drawback. If you filter or hide rows, the shading follows the underlying row numbers, so visible rows may not alternate. For tables you only sort or scroll, it works well. Sheets also has a separate Alternating colors feature under Format, which is simpler when you need no other logic.
Pattern 5: match values from another sheet
A conditional formatting formula cannot refer directly to another sheet in the usual way. Wrap the reference in 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)>0The sheet name and range are text, so they sit inside quotes. If the sheet name contains spaces, put single quotes inside the string: INDIRECT("'Approved List'!A:A"). A later chapter covers this technique in detail.
Combining patterns
Because each formula returns TRUE or FALSE, you can join them with AND, OR, and NOT. For example, to color rows that are overdue and high priority, where column F holds the priority:
=AND($D2<TODAY(),$E2<>"Done",$F2="High")Order the tests so the cheap ones come first. It does not change the result, but it keeps the formula readable.
If two rules can both be TRUE for the same cell, the rule higher in the list wins for any property they both set. Rule order is covered in its own chapter, so for now, give overlapping rules different formatting properties, such as one that sets fill and another that sets text color, if you want both to show.
Checking a formula before you save the rule
You can test any rule's logic in an empty cell first. Type the same formula into a spare cell next to the first data row, such as =$C2="Done". The cell shows TRUE or FALSE. Drag it down to confirm each row gives the answer you expect, then delete the test cells.
If the sidebar shows "Invalid formula," look for these causes:
- Missing quotes around text, such as
=$C2=Done. - A semicolon used as a separator in a locale that expects commas, or the reverse.
- A direct reference to another sheet without
INDIRECT. - A formula that does not start with
=.
Summary of the patterns
| Pattern | Range | Formula |
|---|---|---|
| Whole row by status | A2:F100 | `=$C2="Done"` |
| Duplicates | A2:A100 | `=COUNTIF($A$2:$A$100,$A2)>1` |
| Overdue and not done | A2:E100 | `=AND($D2<TODAY(),$E2<>"Done")` |
| Alternating rows | A2:F100 | `=MOD(ROW(),2)=0` |
| Value in another sheet | A2:A100 | `=COUNTIF(INDIRECT("Lists!A:A"),$A2)>0` |
Exercise
A sheet has tasks in A2:E100. Column D holds due dates and column E holds the status. You apply the rule =AND(D2<TODAY(),E2<>"Done") to A2:E100, and only column A and column D cells behave correctly while the rest of each row is wrong or unformatted. What is wrong, and what is the corrected formula? Also, how would you stop empty rows from being flagged?
Show answer
The references have no dollar sign, so the column shifts as the rule moves to the right. In column B, the formula reads E2 and F2, and so on. Anchor the columns: =AND($D2<TODAY(),$E2<>"Done"). To stop empty rows from being flagged, add a blank check: =AND($D2<>"",$D2<TODAY(),$E2<>"Done"), because an empty cell is treated as 0, which is earlier than today.
Exercise
You want to color only the second and later occurrences of a value in A2:A100, leaving the first occurrence uncolored. Which formula do you use, and why does it work?
Show answer
Use =COUNTIF($A$2:$A2,$A2)>1. The range start is anchored at $A$2 but the end row is relative, so the counted range grows by one row for each cell. The count of the current value reaches 2 only at its second appearance, so the first occurrence stays plain.