5 / 6 · Intro
Copying formulas: relative references
Copy =B4*C4 from D4 to D5 and it arrives as =B5*C5. Microsoft uses this exact example to explain relative references.
Jump to the 10 practice questionsYou will learn
- Predict how a relative reference changes when copied
- Fill a formula down or across
- Recognise #REF! from a reference that falls off the edge
Before this: Adding up with SUM
1Explain
References remember a direction
Write a formula once, then copy it down a column, and it works on every row. This is possible because an ordinary reference is relative. Microsoft explains it with A2 seen from C2: the reference really means "the cell two columns to the left, in the same row". Copy the formula somewhere else and it still means two columns to the left of wherever it now sits. So =B4*C4 in D4, which means "the two cells to my left", becomes =B5*C5 when copied to D5.
2Problem
What a relative reference stores
What does a relative reference really store?
Hint
A direction from the formula.
Solution
Answer: A position relative to the formula's cell
For example, "two columns left, same row".
3Explain
Arithmetic on the address
The rule is simple arithmetic on the address. Move the formula down by some rows and every row number in it grows by the same amount. Move it right by some columns and every column letter moves the same number of letters. Microsoft's table copies =A1 two cells down and two cells to the right and gets =C3. Copying =A2+1 from B2 up to B1 gives =A1+1. Nothing about the formula's meaning changes, only where it looks.
Formulas shown instead of results. One formula in D2, filled down to D6, changes its row numbers on every row.
4Explore
Moving a reference
Copying adds the same number of rows and columns to every relative reference. Going past row 1 or column A gives #REF!.
const colNum = (s) => [...s].reduce((n, ch) => n * 26 + ch.charCodeAt(0) - 64, 0);
const colName = (n) => (n > 26 ? colName(Math.floor((n - 1) / 26)) : "") + String.fromCharCode(65 + ((n - 1) % 26));
const ref = "A1", down = 2, right = 2;
const col = colNum(ref.match(/[A-Z]+/)[0]) + right;
const row = Number(ref.match(/\d+/)[0]) + down;
console.log(col < 1 || row < 1 ? "#REF!" : colName(col) + row);C35Problem
D4 to D5
Hint
One row down.
Solution
Answer: =B5*C5
One row down: both row numbers grow by 1.
6Problem
Two down, two right
Hint
A → C, 1 → 3.
Solution
Answer: =C3
Moving two columns right changes A to C, and moving two rows down changes 1 to 3, giving C3.
7Problem
B2 to B1
Hint
One row up.
Solution
Answer: =A1+1
One row up: the row number goes down by 1.
8Problem
D2 to E2
Hint
One column right: B → C, C → D.
Solution
Answer: =C2*D2
One column right: B becomes C and C becomes D.
9Problem
SUM across
Hint
The range moves one column too.
Solution
Answer: =SUM(C2:C4)
Both ends of the range move one column right.
10Explain
Three ways to copy
You can copy and paste the cell as usual. You can drag the small square at the bottom-right corner of the selected cell, called the fill handle, down or across. Or you can select the formula together with the empty cells below it and press Ctrl+D to fill down, or select it with the cells to its right and press Ctrl+R to fill right. Google's shortcut list gives Ctrl+D and Ctrl+R for Sheets (⌘+D and ⌘+R on a Mac), and Excel on Windows uses the same keys.
11Problem
Ctrl+D
What does Ctrl+D do in Google Sheets?
Hint
D for down.
Solution
Answer: Fill down
Google's shortcut list: Ctrl+D fill down, Ctrl+R fill right.
12Explain
Show the formulas
To see what actually happened, show the formulas instead of the results. Google Sheets does this with Ctrl+~ according to its shortcut list, and Excel has a Show Formulas button on the Formulas tab. Reading a column of formulas side by side is the quickest way to spot the one row that does not follow the pattern, for example a cell where someone typed a number over the formula.
13Problem
Showing formulas
Which shortcut shows all formulas in Google Sheets?
Hint
The key with the wavy line.
Solution
Answer: Ctrl+~
"Show all formulas: Ctrl + ~" in Google's list. Excel has a Show Formulas button on the Formulas tab.
14Explain
Values do not move
Copying only moves the references, not the values. A formula may refer to a cell that is meant to stay put, such as a tax rate in one cell at the top. A relative reference will slide away from it as you fill down. The results will be wrong without any error showing. That problem has its own fix, the absolute reference, which opens the Formulas course.
15Explain
Falling off the edge
A relative reference can be pushed somewhere that does not exist. Copy =A1+1 from B1 one column left into A1 and the reference would have to point left of column A. There is nothing there, so the program writes #REF! into the formula itself, and the cell shows the #REF! error. The same thing happens when you copy a formula that looks one row up into row 1. When a copied formula suddenly shows #REF!, show formulas and check which reference ran off the sheet.
16Problem
Left of column A
=A1+1 in B1 is copied into A1. What happens?
Hint
There is nothing there.
Solution
Answer: The reference becomes #REF!
It would need a column left of A, which does not exist.
17Explore
Practice in the grid
Drag to turn. Scroll or pinch to zoom.
18Problem
D5 after filling
After filling =B2*C2 from D2 to D6, B5 is 4.25 and C5 is 2. What is in D5?
Hint
4.25 × 2.
Solution
Answer: 8.5
D5 holds =B5*C5 = 4.25 × 2 = 8.5.
19Practice
Write the shift function
colNum and colName are given. Finish shift(ref, down, right): return the moved address, or "#REF!" when it would fall above row 1 or left of column A.
C3
B5
C2
A1
#REF!const colNum = (s) => [...s].reduce((n, ch) => n * 26 + ch.charCodeAt(0) - 64, 0);
const colName = (n) => (n > 26 ? colName(Math.floor((n - 1) / 26)) : "") + String.fromCharCode(65 + ((n - 1) % 26));
function shift(ref, down, right) {
const col = colNum(ref.match(/[A-Z]+/)[0]) + right;
const row = Number(ref.match(/\d+/)[0]) + down;
return ref;
}
console.log(shift("A1", 2, 2));
console.log(shift("B4", 1, 0));
console.log(shift("B2", 0, 1));
console.log(shift("A2", -1, 0));
console.log(shift("A1", 0, -1));
Hint
Column 1 is A and row 1 is the first row; anything below 1 does not exist.
A solution
const colNum = (s) => [...s].reduce((n, ch) => n * 26 + ch.charCodeAt(0) - 64, 0);
const colName = (n) => (n > 26 ? colName(Math.floor((n - 1) / 26)) : "") + String.fromCharCode(65 + ((n - 1) % 26));
function shift(ref, down, right) {
const col = colNum(ref.match(/[A-Z]+/)[0]) + right;
const row = Number(ref.match(/\d+/)[0]) + down;
if (col < 1 || row < 1) return "#REF!";
return colName(col) + row;
}
console.log(shift("A1", 2, 2));
console.log(shift("B4", 1, 0));
console.log(shift("B2", 0, 1));
console.log(shift("A2", -1, 0));
console.log(shift("A1", 0, -1));20
Summary
- Ordinary references are relative: they shift when the formula is copied.
- Down n rows: row numbers + n. Right n columns: letters move n.
- Fill with the fill handle, or Ctrl+D (down) and Ctrl+R (right).
- A reference pushed off the sheet becomes #REF!.