Sub topic 9
Locking ranges in SUMIF, COUNTIF and VLOOKUP
This chapter assumes you can enter formulas in Google Sheets and read A1 addresses such as B2. It also assumes you know the difference between a relative reference like A2 and an absolute reference like $A$2. Here you will apply those ideas to ranges, which is where most copy-and-fill mistakes in real spreadsheets happen.
Why ranges need locking
A range is a block of cells written as two corners joined by a colon, such as A2:A100. Each corner is an ordinary cell reference. So each corner follows the same rules as a single cell: it is relative unless you add $ signs.
When you copy a formula down one row, a relative range moves down one row as well. A2:A100 becomes A3:A101. The range keeps its size but slides. For a lookup table or a data column that must stay put, that slide is a bug.
The usual pattern is this:
- The criterion or lookup value (the thing you are searching for) is relative, so each row uses its own value.
- The range or table being searched is absolute, so every row searches the same data.
Range references and what each part does
You can lock each corner of a range separately. The table shows how A2:B100 behaves when you copy it one row down and one column right.
| Written as | After copying down 1 row | After copying right 1 column | Locked parts |
|---|---|---|---|
| `A2:B100` | `A3:B101` | `B2:C100` | Nothing |
| `$A$2:$B$100` | `$A$2:$B$100` | `$A$2:$B$100` | Both columns and rows |
| `$A2:$B100` | `$A3:$B101` | `$A2:$B100` | Columns only |
| `A$2:B$100` | `A$2:B$100` | `B$2:C$100` | Rows only |
The most common choice for lookups is $A$2:$B$100, with both corners fully locked.
SUMIF with a locked range
SUMIF(range, criterion, sum_range) adds the values in sum_range for every row where range matches criterion.
Suppose column A holds product categories, column B holds sales amounts, and column D holds a list of categories you want totals for. In E2 you write:
=SUMIF($A$2:$A$100, D2, $B$2:$B$100)Now fill E2 down to E3, E4 and so on. The formula in E3 becomes:
=SUMIF($A$2:$A$100, D3, $B$2:$B$100)Only D2 changed to D3. Both ranges stayed the same, so every row searches all of the data.
Here is what happens if you forget the dollar signs and write =SUMIF(A2:A100, D2, B2:B100). In E3 the formula becomes =SUMIF(A3:A101, D3, B3:B101). In E4 it becomes A4:A102, and so on. Each row skips one more row of data than the row above it. The first data row is dropped from E3's search, the first two from E4's, and so on. The result looks plausible, which is why this error is easy to miss.
Matching the size of ranges
SUMIF expects range and sum_range to have the same height. Locking both corners of both ranges keeps them aligned. If you lock only one of them, the two drift apart, and the pairing of criteria to amounts breaks. Lock both or neither, and for fixed data, lock both.
COUNTIF with a locked range
COUNTIF(range, criterion) counts the cells in range that match criterion. It follows the same pattern, with one fewer range:
=COUNTIF($A$2:$A$100, D2)Fill this down a list of categories in column D and each row returns the count for its own category.
A useful variation is to count against a threshold stored in one cell. If F1 holds the number 500, this counts sales above it:
=COUNTIF($B$2:$B$100, ">"&$F$1)The & joins the text ">" to the value in F1 to make the criterion ">500". Locking $F$1 keeps the threshold fixed if you copy the formula to other cells. This combines the fixed-input idea from earlier in the tutorial with a locked range.
VLOOKUP with a locked table
VLOOKUP(search_key, range, index, [is_sorted]) looks for search_key in the first column of range and returns the value from the column number index.
Suppose a price table sits in G2:H20, with product codes in column G and prices in column H. Column A of your order list holds product codes. In B2 you write:
=VLOOKUP(A2, $G$2:$H$20, 2, FALSE)Here is what each argument does:
A2is relative, so each row looks up its own product code.$G$2:$H$20is absolute, so each row searches the same price table.2means return the value from the second column of that table.FALSEmeans find an exact match only.
If you leave the table relative and fill the formula down, the table slides down one row each time. In B3 the table is G3:H21, and in B4 it is G4:H22. Products that sit in the early rows of the table are no longer inside the search range, so the formula returns #N/A for them. This is the classic cause of a lookup that works in the first row and fails further down.
Locking the whole column instead
If your table may grow, you can reference whole columns instead of a fixed block:
=VLOOKUP(A2, $G:$H, 2, FALSE)Whole-column ranges such as $G:$H have no row numbers, so there is nothing to slide vertically. They still need $ if you will copy the formula sideways. This form is simple and handles added rows. The cost is that it scans empty rows and is slightly less clear about where your data ends. For most lists of a few thousand rows the difference is not noticeable.
Mixed references in ranges
Sometimes you want to lock only one direction. A common case is a lookup whose column index should change as you fill across. Suppose a table in G1:J20 holds product codes in column G and three price columns (retail, wholesale and member) in H, I and J. Row 1 of your report has the labels 2, 3 and 4 in B1, C1 and D1, which are the column numbers you want.
=VLOOKUP($A2, $G$2:$J$20, B$1, FALSE)$A2locks the column, so every cell in the row uses the product code in column A, while the row changes as you fill down.$G$2:$J$20locks the table.B$1locks the row, so every cell in the column reads its index from row 1, while the column changes as you fill right.
You can write this once in B2 and fill it across B2:D20 to build a complete price report.
How the shifts happen, step by step
The diagram below shows what Sheets does when you fill a formula down. It adjusts each reference according to its type. Locked references pass through unchanged.
Common errors
These are the problems you will see most often with locked ranges.
| Symptom | Likely cause | Fix |
|---|---|---|
| Totals shrink as you go down the list | Range left relative, so it slides down | Add `$` to every corner: `$A$2:$A$100` |
| `VLOOKUP` returns `#N/A` for lower rows only | Lookup table slid out of position | Lock the table: `$G$2:$H$20` |
| `SUMIF` gives wrong totals or `#VALUE!` | Only one of `range` and `sum_range` was locked | Lock both with the same row numbers |
| Result is 0 for every row | Criterion cell was locked by mistake, so every row uses the first criterion... or points at a blank cell | Keep the criterion relative: `D2`, not `$D$2` |
| Formula breaks after inserting rows into the data | Fixed ranges do not always include new rows added at the end | Use a larger range, a whole column, or a named range |
| Formula works down but not across | Needed a mixed reference | Lock only the direction that must not move |
A quick test helps. After filling a formula, click the last cell and read its formula in the formula bar. Compare it with the first one. Anything that should have stayed fixed but changed is a missing $. Anything that should have changed but did not is a $ that should not be there.
Another check is to press Ctrl + ~ (on Mac, Control + ~) to show formulas instead of results. This lets you scan a whole column of formulas at once and spot the one that differs.
Partial range edge cases
Some details are easy to overlook.
- Locked ranges and sorting. If you sort the data that your formulas reference, the locked range still points to the same cells, so results stay correct. Sorting the formula cells themselves can break relative references, because rows move and their references can end up pointing at the wrong places. Prefer locking ranges and keeping lookup values in a stable column.
- Ranges that end too early.
$A$2:$A$100ignores anything in row 101 and beyond. If your data grows past that, results quietly leave out new rows. Either choose a bigger bound or use whole-column ranges like$A:$A. - Header rows in whole-column ranges.
SUMIFon$B:$Bincludes the header cell in row 1. Text is ignored by sums, so this is harmless forSUMIF, but it can matter forCOUNTIFwith broad criteria such as"<>", which would count the header. - Copy versus cut. Copying a formula adjusts relative references. Cutting and pasting a formula moves it without changing its references at all. Use cut when you want a formula to move unchanged, and copy when you want it to adapt.
Worked example from start to finish
You have this data:
| Row | A | B | D | E |
|---|---|---|---|---|
| 1 | Region | Sales | Region | Total |
| 2 | North | 120 | North | |
| 3 | South | 80 | South | |
| 4 | North | 200 | East | |
| 5 | East | 150 | ||
| 6 | South | 90 |
- Click E2 and type
=SUMIF($A$2:$A$6, D2, $B$2:$B$6). - Press Enter. E2 shows 320, the sum of 120 and 200.
- Select E2 and double-click the small square at its bottom-right corner. This autofill copies the formula down as far as column D has values.
- E3 now reads
=SUMIF($A$2:$A$6, D3, $B$2:$B$6)and shows 170. - E4 reads
=SUMIF($A$2:$A$6, D4, $B$2:$B$6)and shows 150.
If you had typed the ranges without $, E3 would search A3:A7, so the North row at the top would be missing from E3's range. E4 would search A4:A8 and miss two rows. Here E4 would still show 150 by luck, since the East row at row 5 lies inside both searches. Wrong results do not always show up in every row, which is why you should check the formulas and not only the numbers.
Exercise
A price table sits in G2:H30. In B2 you enter =VLOOKUP(A2, G2:H30, 2, FALSE) and fill it down to B20. The first few rows work, but later rows show #N/A even though the product codes exist in the table. Explain what went wrong and write the corrected formula for B2.
Show answer
The table reference G2:H30 is relative, so each row down slides it one row. In B3 it is G3:H31, in B4 it is G4:H32, and so on. Product codes near the top of the table fall outside the shifted range, so the lookup fails with #N/A. Lock the table so it stays fixed: =VLOOKUP(A2, $G$2:$H$30, 2, FALSE). Keep A2 relative so each row looks up its own code.
Exercise
Column A holds categories in A2:A200 and column B holds amounts in B2:B200. Column D lists the unique categories starting at D2. Write one formula for E2 that totals the amount for the category in D2 and can be filled down column E, and one formula for F2 that counts the rows in that category.
Show answer
E2: =SUMIF($A$2:$A$200, D2, $B$2:$B$200). F2: =COUNTIF($A$2:$A$200, D2). Both lock the data ranges so they do not slide when filled down, and both leave D2 relative so each row uses its own category.