Sub topic 2
Highlighting Cells by Value, Text and Duplicates
This chapter covers the Highlight Cells Rules group on the Home tab. It assumes you can select ranges, apply a fill or font color by hand, and write a simple formula such as =A1+B1. Later chapters cover Top/Bottom rules, data bars, formula-based rules, and the Rules Manager. Here you learn what each built-in rule does, how Excel stores it, and where it commonly goes wrong.
Where the rules live
Select the cells you want to format, then choose Home > Conditional Formatting > Highlight Cells Rules. The submenu contains these entries:
| Menu entry | Tests | Works on |
|---|---|---|
| Greater Than | value > threshold | numbers, dates |
| Less Than | value < threshold | numbers, dates |
| Between | low ≤ value ≤ high | numbers, dates |
| Equal To | value = target | numbers, text, dates |
| Text that Contains | target text appears anywhere in the cell | text |
| A Date Occurring | date falls in a named period | dates |
| Duplicate Values | value appears more than once, or exactly once | numbers, text |
| More Rules | opens the full rule dialog | all |
Each entry opens a small dialog with a value box on the left and a format list on the right. Choosing an entry such as Greater Than creates a rule, assigns it to your selected range, and applies the format you pick. Excel shows a live preview while the dialog is open.
The selection becomes the Applies to range
The cells selected when you create a rule become its Applies to range. If you select only C2:C50, the rule affects only those cells. Select the range first, and select exactly the cells you want formatted. Header cells included in the selection are evaluated too. A header containing text is usually not highlighted by a numeric rule, but it can be by a text rule. The Applies to range is covered in detail in a later chapter.
Comparison rules: Greater Than, Less Than, Between, Equal To
These four rules compare each cell in the range with a value you supply.
- Select
C2:C50, which holds order totals. - Choose Home > Conditional Formatting > Highlight Cells Rules > Greater Than.
- Type
1000in the left box. - Pick a format, such as Light Red Fill with Dark Red Text, and select OK.
Every total above 1000 now has the red format. If you change a total to 900, the highlight disappears. The format is not stored in the cell. Excel re-evaluates the rule whenever the value changes.
Using a cell reference as the threshold
The value box accepts a cell reference as well as a constant. Click the collapse button at the right of the box and click a cell, or type the reference. Excel inserts it as an absolute reference, such as =$G$1.
Storing the threshold in a cell has a practical benefit. You change one cell, and the highlighting updates everywhere. This is better than editing the rule when the threshold changes often, such as a monthly target.
=$G$1Typing =$G$1 in the Greater Than box makes the rule compare each cell with the number in G1.
Between is inclusive
Between highlights values that equal either limit as well as values between them. If the limits are 50 and 100, a cell holding 100 is highlighted. If you need an exclusive range, you must use a formula rule, which a later chapter covers.
Equal To with text and numbers
Equal To works for text as well. Typing Overdue highlights cells whose entire content is Overdue. The match is not case-sensitive, so overdue and OVERDUE match too. A cell containing Overdue - call client does not match, because Equal To tests the whole cell. Use Text that Contains for partial matches.
Text that Contains
Text that Contains highlights any cell where the text you enter appears anywhere in the cell.
- Typing
northhighlightsNorth,North-East, andFar north office. - The match is not case-sensitive.
- It also matches inside numbers shown as text. Typing
10highlightsA-103, and it can also highlight the number 2105 because the value contains the characters10.
Behind the scenes, Excel writes this rule as a formula using SEARCH. You can see this in the Rules Manager: the rule type reads "Format only cells that contain" with the condition "Specific Text containing".
Wildcards in text matching
In Text that Contains, * and ? are not wildcards in the way they are in some other tools. Excel treats a typed * as a wildcard for the underlying SEARCH call, so a*c can match abc and a123c. If you need to match an actual asterisk, prefix it with a tilde: ~*.
Blank cells
Text-based rules with an empty search value do not behave consistently across rule types. If you want to highlight blank cells, use More Rules > Format only cells with > Blanks, or a formula such as =$B2="". Do not rely on Equal To with an empty box.
Highlighting dates with A Date Occurring
A Date Occurring offers a list of relative periods: Yesterday, Today, Tomorrow, In the last 7 days, Last week, This week, Next week, Last month, This month, Next month. The result moves with the system date. A rule set to Today highlights today's date when you open the file today, and a different cell tomorrow.
This rule only works on real dates. Excel stores a date as a serial number, which is a count of days since 1 January 1900. A date typed as text, such as one imported from a CSV file with a leading apostrophe, will not be highlighted. If a date-based rule does nothing, check that the cell is right-aligned by default and that changing the format to General shows a number.
Custom date logic, such as overdue dates, uses formulas and is covered in a later chapter.
Duplicate Values
Duplicate Values has two modes, chosen from the first drop-down in the dialog.
- Duplicate highlights every cell whose value appears more than once in the range.
- Unique highlights cells whose value appears exactly once.
Both modes compare only the cells in the Applies to range. If you select A2:A100 and the same value appears in A150, that value is not counted.
# What Duplicate Values does, expressed as code
from collections import Counter
values = ["Ana", "Ben", "ana", "Cy", "Ben"]
counts = Counter(v.lower() for v in values)
flagged = [v for v in values if counts[v.lower()] > 1]
print(flagged)The script prints ['Ana', 'Ben', 'ana', 'Ben']. Every occurrence is flagged, not only the second one, and case is ignored.
Duplicates across several columns
If you select a block such as A2:C100, Excel treats it as one pool of values. A name in column A that also appears in column C is flagged. It does not check whole rows. To find rows that are duplicated as a unit, you need a formula rule using COUNTIFS, which is covered with formula-based rules.
Limits of Duplicate Values
- Case:
Anaandanacount as duplicates. - Spaces:
BenandBen(with a trailing space) are different values, so they are not flagged. Clean the data withTRIMin a helper column first. - Numbers and text: the number
100and the text"100"are different values. - Long text: values over 255 characters can be wrongly flagged as duplicates. This is a known limitation of the underlying comparison. For long strings, use a formula rule with a helper column instead.
- Blanks: empty cells are ignored.
Choosing the format
The format list on the right of each dialog includes presets such as Light Red Fill with Dark Red Text, Yellow Fill with Dark Yellow Text, and Green Fill with Dark Green Text. Choose Custom Format to open the Format Cells dialog, with these tabs:
- Number, to change how the value displays.
- Font, for bold, italic, strikethrough, and color. You cannot change font name or size.
- Border, for line styles. Thick borders are not available.
- Fill, for background color, patterns, and gradients.
The limits on font and border options are deliberate. Conditional formats can change only properties that do not alter cell size or layout.
A rule's format overrides the cell's manual formatting only for the properties it sets. If a cell has a yellow fill applied by hand and a rule sets only the font color, the yellow stays visible when the rule fires. If the rule sets a fill, the rule's fill hides the manual fill while the condition is true. When it becomes false, the manual fill returns.
Seeing and changing the rule afterward
To edit a rule, select a cell it covers and choose Home > Conditional Formatting > Manage Rules. Set "Show formatting rules for" to This Worksheet, select the rule, and choose Edit Rule. Changing the value or the format there updates the existing rule. Creating the rule again from the menu would add a second rule on top, leaving the first one active.
This is a frequent cause of "stuck" highlighting. If a cell remains highlighted after you edit what you think is the only rule, open the Rules Manager and look for earlier rules that cover the same cells.
Common errors
- Selecting the wrong range. The rule appears to do nothing because the cells you are looking at are not in the Applies to range.
- Comparing text with a number. In a Greater Than rule, text cells are treated as larger than any number, so a cell containing
N/Acan be highlighted. Use a formula rule such as=AND(ISNUMBER(C2),C2>1000)if you need to avoid this. - Hard-coded thresholds. Typing 1000 into twenty rules means twenty edits later. Point to one cell instead.
- Dates stored as text. The rule is valid but never fires.
- Adding duplicates of a rule. Re-creating a rule instead of editing it stacks rules.
Quick reference
Exercise
A column of customer names in A2:A200 contains Smith in row 5 and Smith (with a trailing space) in row 80. You apply Duplicate Values with the Duplicate option. Neither cell is highlighted. Why, and how do you fix it?
Show answer
Excel compares values exactly apart from case, so Smith and Smith with the trailing space are different values and neither counts as a duplicate. Remove the extra spaces, for example by creating a helper column with =TRIM(A2) and applying the rule to it, or by cleaning the original data. Once both values read Smith, both cells are highlighted.
Exercise
You want to highlight totals in C2:C50 that are greater than a target which changes every month. What is the best way to set this up with a built-in rule?
Show answer
Put the target in a single cell, for example G1. Select C2:C50, choose Highlight Cells Rules > Greater Than, and enter =$G$1 in the value box. When the target changes, edit G1 and the highlighting updates without editing the rule.
Exercise
A cell contains the text Overdue - call client. Which rule highlights it when you enter Overdue: Equal To or Text that Contains? Why?
Show answer
Text that Contains. Equal To tests the entire cell content, so it matches only a cell holding exactly Overdue. Text that Contains matches the word anywhere in the cell.