Sub topic 14
Basic formulas and cell references
This chapter covers how a formula is built, what a cell reference points to, and how references change when you copy a formula. It assumes you can type values into cells, select ranges, and copy and paste, as covered in earlier chapters. It also assumes you know that Sheets decides whether an entry is text, a number, or a date.
What makes an entry a formula
An entry is a formula when it starts with an equals sign. Sheets then calculates a result and shows it in the cell. The formula itself stays visible in the formula bar.
=SUM(B2:B10)This adds the numbers in cells B2 through B10. A formula can contain:
- Constants such as
5or"text". Text must be in double quotes. - Cell references such as
B2, or ranges such asB2:B10. - Operators such as
+,-,*,/,^, and&. - Functions such as
SUM, which take arguments in parentheses.
A formula recalculates whenever a cell it refers to changes. This is the main reason to use references instead of typing numbers into the formula.
Operators and order of evaluation
Sheets calculates in a fixed order. Parentheses come first, then ^, then * and /, then + and -. The & operator joins text and is evaluated after the arithmetic operators. Comparison operators such as =, <, and >= come last.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| `+` `-` | Add, subtract | `=10-4` | 6 |
| `*` `/` | Multiply, divide | `=10/4` | 2.5 |
| `^` | Power | `=2^3` | 8 |
| `&` | Join text | `="Q"&1` | Q1 |
| `>` `<` `=` | Compare | `=B2>100` | TRUE or FALSE |
Compare =2+3*4, which gives 14, with =(2+3)*4, which gives 20. When a result looks wrong, add parentheses to make your intended order explicit.
Cell references
A reference tells Sheets where to find a value. B2 means column B, row 2. A range uses a colon: B2:B10 is the nine cells from B2 down to B10. B2:D4 is a rectangle of nine cells, three columns wide and three rows tall.
You can also refer to whole columns or rows:
B:Bis all of column B. It also covers rows you add later.2:2is all of row 2.B2:Bruns from B2 to the bottom of the column.
To use another sheet in the same file, put the sheet name and an exclamation mark before the reference. Names with spaces need single quotes.
=Summary!B2+'Q1 Sales'!B2The easiest way to build a reference is to type = and then click the cell or drag across the range. Sheets writes the reference and colors each one to match its outline on the grid.
Relative references and why they change
By default, a reference is relative. Sheets does not store "B2" as a fixed address. It stores "the cell in column B, in this same row". When you copy the formula to another cell, the reference moves by the same offset.
Suppose C2 holds =B2*2. If you copy C2 to C3, the formula becomes =B3*2. If you copy it one column right to D2, it becomes =C2*2. This is the reason a single formula can be filled down a whole column and still work row by row.
Absolute and mixed references
A dollar sign locks the part of the reference that follows it. There are four forms:
| Form | Column | Row | Behavior when copied |
|---|---|---|---|
| `B2` | relative | relative | Both move |
| `$B$2` | locked | locked | Neither moves |
| `$B2` | locked | relative | Only the row moves |
| `B$2` | relative | locked | Only the column moves |
Use an absolute reference for a single value that many formulas share, such as a tax rate or an exchange rate.
=B2*$E$1Here E1 holds the rate. Filling the formula down gives =B3*$E$1, =B4*$E$1, and so on. Without the dollar signs, the second row would multiply by E2, which is probably empty. The result would be 0, with no error to warn you.
While editing a reference, press F4 (Fn+F4 on some keyboards) to cycle through B2, $B$2, B$2, and $B2.
Mixed references are useful in grids. If row 1 holds prices and column A holds quantities, then =$A2*B$1 in B2 can be copied across and down. The quantity stays in column A, and the price stays in row 1.
Copying a formula versus filling it
You can reuse a formula in three common ways:
- Copy and paste the cell. References adjust relative to the new position.
- Drag the fill handle, the small square at the corner of the selection, down or across.
- Double-click the fill handle. Sheets fills down as far as the adjacent column has data.
All three apply the relative-reference rule. Moving a cell is different. If you cut and paste a formula, its references do not change, because the formula is moved rather than copied. If you cut and paste a cell that other formulas refer to, those formulas follow it to its new location.
Common errors
Sheets displays an error code when a formula cannot produce a result. Hover over the cell to see a short explanation.
| Error | Usual cause |
|---|---|
| `#DIV/0!` | Dividing by zero or by an empty cell |
| `#NAME?` | A misspelled function name, or text without quotes |
| `#REF!` | A reference to a deleted row, column, or sheet |
| `#VALUE!` | The wrong type, such as adding a number to a word |
| `#N/A` | A lookup that found nothing |
Two other problems produce no error code. The first is a number stored as text. Text is left-aligned by default, and SUM ignores it. Retype it, or check the cell's format as covered in the data types chapter. The second is a circular reference, where a formula depends on its own cell, for example =A1+1 entered in A1. Sheets shows an error and stops calculating that cell.
Formatting does not change the value
A formula result can be shown as currency, a percentage, or a date. This changes only the display. =B2*$E$1 might show $12.35 but store 12.3456. Other formulas use the stored number. If you need the display to be the actual value, wrap it in ROUND, for example =ROUND(B2*$E$1,2).
Practice
Exercise
Cell E1 holds a discount rate of 0.1. Column B holds prices in B2:B20. You enter =B2*E1 in C2 and fill it down. C2 is correct, but C3 shows 0. What is wrong, and what is the fix?
Show answer
The reference E1 is relative, so in C3 it became E2, which is empty. Lock it with dollar signs: =B2*$E$1 in C2, then fill down again. Each row then multiplies its price by E1.
Exercise
Cell D5 contains =$A5*B$1. You copy it to F8. What formula does F8 contain?
Show answer
=$A8*D$1. The column of A is locked, so it stays A, and the row moves from 5 to 8. The row of B$1 is locked, so it stays 1, and the column moves two to the right, from B to D.