Sub topic 9
Locking ranges in SUMIF, COUNTIF and VLOOKUP
This chapter assumes that you can enter formulas, use A1 cell addresses such as A2, and copy a formula to other cells. You should also know basic arithmetic operators and the purpose of a function.
When you copy a formula, Google Sheets normally changes its references. That behavior is useful for a changing criterion, such as D2 becoming D3. It is a problem when the formula must keep using the same source data. Add $ signs to lock a range.
Why ranges need locking
A range is a group of cells, such as A2:A100. A relative range changes when its formula moves. For example, copying =SUMIF(A2:A100, D2, B2:B100) down one row changes the ranges to A3:A101 and B3:B101.
Usually, the data range should stay fixed while the criterion changes. Write the formula as:
=SUMIF($A$2:$A$100, D2, $B$2:$B$100)The dollar signs lock both the columns and the rows. D2 remains relative, so it changes to D3, D4, and so on when you copy the formula down.
| Reference | What stays fixed when copied |
|---|---|
| `A2:A100` | Nothing |
| `$A$2:$A$100` | The column and row |
| `$A2:$A100` | The column only |
| `A$2:A$100` | The row only |
SUMIF with a fixed data range
SUMIF adds values that meet a condition. Suppose column A contains product categories, column B contains sales, and column D contains categories to summarize.
Use this formula in E2:
=SUMIF($A$2:$A$100, D2, $B$2:$B$100)The arguments are:
$A$2:$A$100is the range to test.D2is the criterion.$B$2:$B$100is the range to add.
Copy E2 down. The formula in E3 becomes:
=SUMIF($A$2:$A$100, D3, $B$2:$B$100)Only the criterion changes. The source ranges remain aligned.
COUNTIF with a fixed data range
COUNTIF counts cells that meet a condition. If column A contains order statuses and column D lists statuses to count, enter:
=COUNTIF($A$2:$A$100, D2)Copying the formula down changes D2 to D3, while the status range remains $A$2:$A$100.
A common error is to lock the criterion accidentally:
=COUNTIF($A$2:$A$100, $D$2)This formula counts the status in D2 on every row. Use this only when every result should use the same criterion.
VLOOKUP with a fixed lookup table
VLOOKUP searches the first column of a table and returns a value from another column. Suppose product codes are in column A, and prices are in column B. A separate list of codes begins in D2.
Enter:
=VLOOKUP(D2, $A$2:$B$100, 2, FALSE)Here, D2 is the changing lookup value. $A$2:$B$100 is the fixed lookup table. 2 tells Sheets to return the second column of that table. FALSE requests an exact match.
When you copy the formula down, D2 changes to D3, but the lookup table does not move.
Troubleshooting copied formulas
When results become wrong after copying, inspect the formula in a later row.
- If the source range changed from
$A$2:$A$100to$A$3:$A$101, the range was not fully locked. - If every row uses the same criterion, you may have locked
D2when it should beD2,D3, and so on. - If SUMIF returns incorrect totals, confirm that the criteria range and sum range cover the same rows.
- If VLOOKUP returns
#N/A, check the spelling, extra spaces, data type, and whetherFALSEis present for an exact match. - If VLOOKUP returns a value from the wrong field, check the column index. The index counts from the first column of the lookup range, not from column A of the sheet.
Exercise
Exercise
Write a COUNTIF formula in E2 that counts the status in D2 using the fixed range A2:A500, then copy it down.
Show answer
=COUNTIF($A$2:$A$500, D2)