Sub topic 6
Working with ranges such as B2:B10
A range is a rectangular block of cells that a formula treats as one unit. Most useful spreadsheet functions work on ranges, so this chapter covers how to write them, how they behave when copied, and where they cause errors.
This chapter assumes you can write a formula that starts with =, that you know A1 notation (column letter, then row number), and that you understand relative and absolute references from the earlier chapters.
What a range is
A range is written as two cell addresses joined by a colon. The first address is one corner of the block and the second is the opposite corner. The colon means "through".
=SUM(B2:B10)This adds the values in B2, B3, and so on down to B10. That is nine cells in one column.
Ranges come in three common shapes:
| Shape | Example | Cells covered |
|---|---|---|
| One column | `B2:B10` | B2 through B10 (9 cells) |
| One row | `B2:F2` | B2 through F2 (5 cells) |
| Rectangle | `B2:D10` | Three columns by nine rows (27 cells) |
A single cell such as B2:B2 is also a valid range, though you will rarely write it that way.
Range corners can be written in either order
B10:B2 and B2:B10 mean the same block. Google Sheets normalizes the range when you press Enter. For readability, write the top-left corner first.
Entire columns and rows
You can leave out the row numbers to cover a whole column, or the column letters to cover a whole row.
=SUM(B:B)
=SUM(3:3)The first formula adds every number in column B. The second adds every number in row 3. Whole-column references are convenient, but they have one trap. If the formula itself sits inside the column it reads, you create a circular dependency, and Sheets shows #REF! with a circular-dependency message. Put totals outside the range they total.
Typing and selecting ranges
You do not have to type a range by hand. While editing a formula, you can drag across cells with the mouse, or click the first cell and then Shift-click the last one. Sheets inserts the range text and colors the matching block on the sheet so you can check it visually.
Keyboard users can hold Shift and press the arrow keys to extend a selection while editing. Pressing F4 (or Fn+F4 on some keyboards) cycles the reference through relative, absolute, and mixed forms.
Functions that take ranges
Many functions accept a range as an argument. Here is a small set to practice with.
| Function | What it does | Example |
|---|---|---|
| `SUM` | Adds the numbers | `=SUM(B2:B10)` |
| `AVERAGE` | Mean of the numbers | `=AVERAGE(B2:B10)` |
| `MIN` / `MAX` | Smallest or largest value | `=MAX(B2:B10)` |
| `COUNT` | Counts cells that contain numbers | `=COUNT(B2:B10)` |
| `COUNTA` | Counts cells that are not empty | `=COUNTA(A2:A10)` |
Two behaviors matter here. First, these functions ignore text and empty cells inside a numeric range. AVERAGE divides by the number of numeric cells, not by the size of the range. Second, a number stored as text (for example, one that was pasted with a leading apostrophe) is ignored too, so your total can be lower than expected without any error.
Worked example
Suppose a sheet holds a small order list:
| Row | A | B | C |
|---|---|---|---|
| 1 | Item | Quantity | Unit price |
| 2 | Pens | 10 | 1.50 |
| 3 | Notebooks | 4 | 3.00 |
| 4 | Staplers | 2 | 8.25 |
| 5 | Totals |
In B5, enter the total quantity:
=SUM(B2:B4)The result is 16. In C5, you could find the highest price:
=MAX(C2:C4)The result is 8.25. Both formulas read a block of three cells instead of naming each cell one at a time, so they are shorter and easier to check.
Ranges across several columns
A rectangular range lets one function read a whole table.
=SUM(B2:D10)Sheets adds every number in the rectangle. Use COUNT on the same range to see how many cells actually held numbers:
=COUNT(B2:D10)If the count is lower than you expect, some cells are empty or contain text. Checking the count before trusting a sum is a quick habit that catches many data problems.
How ranges move when you copy or fill
Each end of a range is its own reference. By default both ends are relative, so both move when you copy the formula.
If B12 contains =SUM(B2:B10) and you fill it to the right into C12, the formula becomes =SUM(C2:C10). Both ends shifted one column right, and the rows stayed the same because you moved horizontally.
If you copy that same formula down one row to B13, it becomes =SUM(B3:B11). Both ends moved down one row. The range slid down, which is rarely what you want for a total.
To keep one end of a range fixed while the other moves, lock it with $. This pattern builds a running total:
=SUM($B$2:B2)In row 2 this adds only B2. Filled down to row 3 it becomes =SUM($B$2:B3), which adds B2 and B3. The start is pinned and the end follows the formula, so each row shows the total so far.
To lock the whole range, put $ on both ends: =SUM($B$2:$B$10). Use this when many formulas must read the same block, such as a lookup table.
Ranges and a changing sheet
Sheets adjusts ranges for you when you change the layout of the sheet, but the adjustment depends on what you do.
- Insert a row inside the range. If you insert a row between rows 4 and 5,
B2:B10becomesB2:B11. The new row is counted. - Insert a row just below the range.
B2:B10staysB2:B10. The new row at 11 is not counted. This is the most common reason a total misses a new entry. - Delete a row inside the range.
B2:B10shrinks toB2:B9. - Delete every cell that an end of the range points to. The range shrinks to fit the remaining cells. If the whole range is deleted, the formula returns
#REF!.
The second case is worth remembering. If you add data at the bottom of a list, a fixed range will not include it. Later chapters cover open-ended ranges, which solve this problem.
Operators on ranges
The colon is not the only range operator, but it is the one you will use constantly. Inside a formula you can also combine range pieces by passing several arguments to a function:
=SUM(B2:B10, D2:D10, 100)This adds two ranges and the number 100. Arguments are separated by commas. In some locales Sheets uses semicolons instead, depending on the spreadsheet's locale setting, so if a copied formula fails to parse, check File > Settings.
Common range mistakes
Most range problems come from a short list of causes.
- Range too short. The range stops before the last row of data. Check the highlighted block while editing the formula.
- Range includes the header or the total. If
B1holds the text "Quantity" it is ignored bySUM, butCOUNTAwill count it. If the total cell is inside the range, you get a circular dependency. - Range slides when filled. A formula meant to read a fixed block drifts down or across. Add
$to the ends that must stay put. - Numbers stored as text. The cells look like numbers but are left-aligned and not counted. Convert them with Format > Number, or multiply the column by 1 in a helper column.
- Mismatched shapes. Some functions need ranges of the same size. A range of nine cells against a range of eight gives an error, so count the rows at both ends.
Quick reference
| You want | Write |
|---|---|
| Sum a column block | `=SUM(B2:B10)` |
| Sum a rectangle | `=SUM(B2:D10)` |
| Sum a whole column | `=SUM(B:B)` |
| Lock a whole range | `=SUM($B$2:$B$10)` |
| Running total | `=SUM($B$2:B2)` |
| Count numeric cells | `=COUNT(B2:B10)` |
Exercise
Cell E2 holds =SUM($B$2:B2). You fill it down to E5. What formula appears in E5, and what does each row show?
Show answer
E5 holds =SUM($B$2:B5). The start of the range is locked on B2 and the end moves down with the row. Each row therefore shows the running total of column B from row 2 down to that row.
Exercise
A list of sales is in B2:B20 and the total in B22 is =SUM(B2:B20). You add a new sale in B21 and the total does not change. Why, and how can you make the total include the new row?
Show answer
The range ends at B20, and inserting or typing a value directly below the range does not extend it. Edit the formula to =SUM(B2:B21), or insert the new row inside the range (above row 20) so Sheets extends the range automatically. Open-ended ranges such as B2:B avoid the problem entirely.
Exercise
Column B holds the values 10, 20, an empty cell, and the text "n/a". What do =COUNT(B1:B4) and =COUNTA(B1:B4) return?
Show answer
COUNT returns 2 because only two cells contain numbers. COUNTA returns 3 because it counts every non-empty cell, including the text.