Sub topic 15
Highlighting cells with conditional formatting
Conditional formatting changes how a cell looks based on its value or on a formula. The stored value stays the same. Use it to flag overdue dates, mark outliers, or shade a whole row when one column meets a condition.
This chapter assumes you can select ranges, type formulas, and tell relative references (B2) from absolute ones ($B$2) from earlier chapters. It also assumes you know that a cell's displayed look can differ from its stored content.
How rules work
A conditional formatting rule has three parts:
- Apply to range: the cells the rule can change.
- Condition: a test that is true or false for each cell in the range.
- Style: what to apply when the test is true. For a single-color rule, you can set bold, italic, underline, strikethrough, text color, and fill color.
To create a rule, select a range and choose Format > Conditional formatting. The sidebar has two tabs. Single color applies one style when a condition is true. Color scale shades cells along a gradient according to their values.
Sheets tests each cell in the range separately. Cells where the condition is false keep whatever normal formatting they already have.
Built-in conditions
The Format cells if dropdown offers conditions that need no formulas:
| Group | Conditions |
|---|---|
| Blank checks | Is empty, Is not empty |
| Text | Text contains, Text does not contain, Text starts with, Text ends with, Text is exactly |
| Dates | Date is, Date is before, Date is after |
| Numbers | Greater than, Greater than or equal to, Less than, Less than or equal to, Is equal to, Is not equal to, Is between, Is not between |
| Formula | Custom formula is |
Use a built-in condition when you can. It is easier to read and harder to get wrong. A rule such as Greater than 100 on B2:B50 highlights every cell in that range above 100.
Color scales
A color scale needs a Minpoint, a Maxpoint, and optionally a Midpoint. Each point has a type:
- Min value / Max value: the lowest or highest value in the range.
- Number: a fixed value you type.
- Percent: a position between the min and max.
- Percentile: the value below which that percentage of the data falls.
Percentile points reduce the effect of one extreme value. With a plain min-to-max scale, a single huge outlier makes every other cell look almost the same color.
Custom formulas
Choose Custom formula is when the condition depends on other cells, on several tests combined, or on anything the built-ins cannot express. The formula must return TRUE or FALSE. It starts with an equals sign.
This rule, applied to A2:C100, highlights every row where column C holds a value above 100:
=$C2>100Two details make this work:
- The formula is written for the top-left cell of the range. Here that is
A2. Sheets adjusts the references for every other cell, as when you copy a formula. - The
$locks the column.$C2always points at column C, so A2, B2, and C2 all test C2. The row number has no$, so row 3 tests C3, row 4 tests C4, and so on.
If you wrote =C2>100 instead, cell A2 would test C2, but B2 would test D2 and C2 would test E2. That is the most common custom formula error. When a highlight lands on the wrong cells, check which references need a $.
Common formula patterns
=$B2="Overdue"
=AND($C2<>"",$C2<50)
=$D2<TODAY()
=COUNTIF($A$2:$A$100,$A2)>1
=MOD(ROW(),2)=0=$B2="Overdue"highlights a row when column B contains exactly that word. The comparison ignores letter case.=AND($C2<>"",$C2<50)highlights numbers below 50 and skips blanks.=$D2<TODAY()highlights dates before today. It recalculates whenever the sheet recalculates.=COUNTIF($A$2:$A$100,$A2)>1highlights every cell in column A whose value appears more than once. The range is absolute, because it must not shift as the formula adjusts down the rows. The criterion$A2shifts with each row.=MOD(ROW(),2)=0shades every second row. For a simple banded look, Format > Alternating colors is easier. A formula is useful when you need the banding to follow a condition.
Rule order and conflicts
All the rules for a range appear in the sidebar as a list. Sheets checks them from top to bottom. When two true rules set the same style property, such as fill color, the rule higher in the list wins. Drag a rule by its handle to change its priority.
A common design is to put the most specific rule first. For example, put "Status is Overdue → red" above "Date is before today → yellow", so overdue items stay red.
The rules do not stop each other. A top rule that sets only bold text does not stop a lower rule from adding a fill color.
Edge cases and common errors
- Blank cells count as zero in numeric comparisons. A rule
=$C2<50highlights blank cells in column C. Guard it with$C2<>"", as in theANDpattern above, or useISNUMBER($C2). - Text can pass a "greater than" test. In Sheets, text compares as greater than any number. If a column contains the text
"n/a", a rule like=$C2>100highlights it. Combine the test withISNUMBER:=AND(ISNUMBER($C2),$C2>100). - Numbers stored as text do not match numeric rules. The cell shows
150but is text. Fix the data first, using the data type tools from earlier chapters. - Other sheets need INDIRECT. A custom formula cannot point at another sheet directly. Wrap the reference, for example
=COUNTIF(INDIRECT("Lists!A:A"),A2)>0. - Copying cells copies rules. A normal paste brings along the source cell's conditional formatting and can leave you with many overlapping rules over time. Use Paste special > Conditional formatting only to copy just the rules. Use Paste special > Values only to avoid bringing them along.
- Splitting ranges. If you insert rows in the middle of a range, the rule's range usually grows with it. If you paste over part of a range, the rule can split into several smaller rules. Review the sidebar periodically and merge duplicates by editing their ranges.
- Formatting is not data. Filtering and formulas cannot see conditional colors. If you need to count or filter "the red rows", use the same condition in a helper column, such as
=$C2>100.
Worked example
You track invoices in A1:D100. Column A holds the invoice ID, B the customer, C the amount, and D the due date. Row 1 contains headers.
- Select
A2:D100. - Open Format > Conditional formatting and stay on Single color.
- Set Format cells if to Custom formula is and enter
=$D2<TODAY(). - Choose a light red fill and click Done.
- Click Add another rule. Use
=AND(ISNUMBER($C2),$C2>1000)with bold text and a yellow fill.
Rows with past due dates now turn red. Rows with amounts above 1000 get bold text. A row that is both gets the red fill from the top rule and the bold from the second rule, because the two rules set different properties. Change the second rule's fill color to see the top rule win.
Exercise
A sheet has names in A2:A50 and scores in B2:B50. You select A2:B50 and enter the custom formula =B2<60 to highlight failing students' rows. Some cells in column A get highlighted even when their row's score is above 60, and some failing rows are only half highlighted. What is wrong, and what is the corrected formula?
Show answer
The reference B2 has no $ before the column letter. In column A the formula tests column B, but in column B it tests column C, which is empty and counts as 0 (so those cells highlight wrongly). The column must be locked: =$B2<60. To skip blank scores too, use =AND($B2<>"",$B2<60).
Exercise
You have two rules on the same range. Rule 1 (top) fills cells red when the value is above 100. Rule 2 fills cells green when the value is above 50. What color is a cell containing 120, and how do you make 120 green instead?
Show answer
It is red, because Rule 1 is higher in the list and both rules set the fill color. To make it green, drag Rule 2 above Rule 1, or change the conditions so that they do not overlap, for example by making Rule 2 apply only between 50 and 100.