Sub topic 7
Controlling which cells a rule covers
This chapter assumes you can select ranges, apply a built-in highlight rule from the Home tab, and write a simple formula such as =A1+B1. You will learn how the Applies to range decides which cells a rule formats. You will also learn how to change that range, and why ranges tend to break up over time.
Two ranges with two jobs
Every conditional formatting rule involves two separate ideas:
- The Applies to range is the set of cells the rule can format. It is where the formatting appears.
- The condition is the test Excel runs on each of those cells. For a formula rule, the formula may refer to cells inside or outside the Applies to range.
A rule never formats a cell outside its Applies to range, however true its formula is there. The reverse also holds. A cell inside the range stays plain unless the condition is true for that cell.
Excel does not run the formula once. It runs it separately for every cell in the range. For each cell it shifts the relative parts of the references to match that cell's position. The next chapters on formula rules and references depend on this. Here the point is that the range sets how many times, and for which cells, the test runs.
How the range is set when you create a rule
When you create a rule from the Home tab, the Applies to range is whatever you had selected at that moment. Select A2:E50, add a rule, and the range becomes $A$2:$E$50. Excel always stores it with absolute references.
The selection also matters for formula rules. The active cell is the cell that stays white inside a selected block, and it is normally the top-left cell. Excel reads the formula you type as if it were written for that cell. For example, you select A2:E50 with A2 active and enter the following formula.
=$C2>1000Excel applies it as: for each cell, look at column C of that cell's row. Row 2 tests C2, row 3 tests C3, and so on. This highlights whole rows.
If you instead selected the block by dragging from the bottom-right to the top-left, the active cell would be E50. $C2 would then be read relative to E50, and the rule would test the wrong rows. Always make sure the active cell is the top-left cell of the range before typing a formula rule.
Viewing and editing the range in the Rules Manager
Open Home > Conditional Formatting > Manage Rules. The dialog lists rules in a table with columns for the rule, the format, Applies to, and Stop If True. The Show formatting rules for drop-down at the top controls which rules you see:
| Setting | What it lists |
|---|---|
| Current Selection | Only rules that touch the cells you selected before opening the dialog |
| This Worksheet | Every rule on the active sheet |
| A named sheet | Every rule on that other sheet |
Many users open the dialog, see an empty list, and conclude there are no rules. The cause is usually that Current Selection is active and the selected cell has no rules. Switch to This Worksheet to see everything.
To change a range, click the rule, then edit the Applies to box. You can type a new reference or click the small collapse button at the right of the box and drag across the sheet. Click Apply to see the result without closing the dialog.
=$A$2:$E$50Changing the range does not rewrite the formula. The formula keeps the same relative logic and now runs over the new cells. Extend $A$2:$E$50 to $A$2:$E$200 and the rule tests rows 51 to 200 in the same way.
Ranges made of several areas
One rule can cover separate blocks. Separate the areas with commas in the Applies to box.
=$B$2:$B$100,$F$2:$F$100This is useful when the same highlight should apply to two columns that are not next to each other, and you want one rule to maintain instead of two. You can also create it by holding Ctrl while selecting the blocks and then adding the rule.
Be careful with formula rules over several areas. Excel anchors the formula to the top-left cell of the whole range, which is B2 here. Excel then shifts it for each cell by the same offset. A formula like =B2>1000 works as expected in column B. In column F it tests the cell in the same row, four columns to the left of F2, which is B2, relative to the position of each cell. If you want a different cell tested, use a mixed reference such as $B2.
Use separate rules when the two areas need different tests. A shared rule over scattered areas is easy to misread later.
Choosing the right size for the range
You have three common choices.
- Exact data block, such as
$A$2:$E$50. This is precise and fast. Rows added below the block are not covered. - Generous block, such as
$A$2:$E$1000. This covers future rows. Empty cells in the range are tested too. A rule like=$C2>1000ignores them. A rule like=$C2<1000highlights every empty row, because an empty cell counts as zero. - Whole column, such as
$A:$A. This is easy to write and works, but it covers over a million rows and includes the header. Large workbooks with many whole-column rules can recalculate more slowly.
An Excel Table (Insert > Table) avoids much of this. When you add a row to a table, the rules in it extend to the new row automatically. If your data grows, converting it to a table is often better than guessing a larger range.
How the range changes when you edit the sheet
The Applies to range is not fixed. Excel adjusts it as you change the sheet.
- Inserting rows inside the range makes the range larger. A rule on
$A$2:$E$50becomes$A$2:$E$51after you insert a row at row 10. - Inserting a row directly below the last row usually does not extend it. The new row is outside the range and gets no formatting.
- Deleting rows shrinks the range. Delete all of them and the rule disappears.
- Pasting normal cells over the range replaces the formatting in the target cells with the source formatting. Excel does this by splitting the range. The rule's Applies to box can end up with many fragments.
- Sorting does not change the range. The rule follows the position of the cells, not the data. A formula rule such as
=$C2>1000therefore stays correct after a sort, because it is re-evaluated against whatever now sits in each row.
Fragmentation is the most common long-term problem. After a lot of copying and pasting, the Rules Manager may show dozens of duplicate rules, each with a small Applies to range such as $A$12:$E$12. Each one is correct on its own, but together they are slow and hard to edit. To avoid this, paste with Paste Special > Values when you only want the data. This leaves the formatting in the destination alone.
The cleanup is to delete the fragmented rules and create one rule over the whole block again. The chapter on copying and clearing rules covers the tools for this.
A worked example
You have a task list in A1:E50. Row 1 holds headers. Column C holds amounts. You want rows with an amount over 1000 shaded, and you want it to keep working for the next 150 rows.
- Click
A2, then selectA2:E200so thatA2stays the active cell. - Choose Conditional Formatting > New Rule > Use a formula to determine which cells to format.
- Enter
=$C2>1000and pick a fill color. - Click OK, then open Manage Rules and set Show formatting rules for to This Worksheet.
- Confirm that Applies to reads
=$A$2:$E$200.
The condition ignores empty rows because an empty C cell is not greater than 1000. If you later need to cover more rows, edit the range to =$A$2:$E$500 and leave the formula as it is.
Exercise
A rule has the formula =$C2>1000 and an Applies to range of $A$2:$E$50. You paste a block of cells with different formatting over A10:E10, and afterward row 10 no longer highlights even when C10 is above 1000. What happened, and how do you fix it without retyping the formula?
Show answer
The paste overwrote the formatting in A10:E10, and Excel split the rule's Applies to range so that it no longer includes row 10 (for example $A$2:$E$9,$A$11:$E$50). Open Conditional Formatting > Manage Rules, set the drop-down to This Worksheet, select the rule, and edit Applies to back to $A$2:$E$50. The formula stays as it is. To avoid a repeat, use Paste Special > Values when you only want the data.
Exercise
You select the range from the bottom-right cell E50 up to A2, so the active cell is E50, and enter =$C2>1000 as a formula rule. Why do the wrong rows highlight, and what should you do?
Show answer
Excel reads the formula relative to the active cell. With E50 active, $C2 means "column C, same row as the active cell", shifted for every other cell by its offset from E50. The rule therefore tests rows that are 48 rows off from the intended ones. Delete the rule, click A2, select A2:E50 so A2 is the active cell, and enter the formula again.