Sub topic 5
Copying an IF formula down a column
This chapter assumes you can write a basic IF formula, as covered in the previous chapters: a test, a value if true, and a value if false. It also assumes you know what a cell reference such as B2 means. Here you learn what happens to the references inside an IF formula when you copy it to other rows, and how to control that.
Why copying changes a formula
You usually write an IF formula once, in the first row of your data, and then reuse it for every other row. Sheets does not copy the formula's text unchanged. It copies the formula's relationship to the cells around it.
Suppose cell C2 contains this formula:
=IF(B2>=50, "Pass", "Fail")Sheets stores the reference B2 as "the cell one column to the left, in the same row." When you copy the formula to C3, the reference keeps that relationship and becomes B3. In C4 it becomes B4, and so on. This is called a relative reference. It is the reason one formula can grade a whole column of scores.
You can see the shift by clicking a copied cell and reading the formula bar.
| Cell | Formula after copying |
|---|---|
| C2 | `=IF(B2>=50, "Pass", "Fail")` |
| C3 | `=IF(B3>=50, "Pass", "Fail")` |
| C4 | `=IF(B4>=50, "Pass", "Fail")` |
In this example, the only part of the formula that changes is the row number of B. The number 50 and the text values are constants, so they stay as they are.
Ways to copy a formula down
Sheets gives you several ways to fill a column. They produce the same result, so choose the one that suits your hands.
- Drag the fill handle. Select the cell with the formula. A small square appears at its bottom-right corner. Drag it down over the rows you want to fill.
- Double-click the fill handle. Sheets fills down as far as the data in the neighboring column goes. It stops at the first blank cell in that neighboring column (see the warning below).
- Fill a selected range with Ctrl+D. Select the formula cell and the cells below it, then press Ctrl+D (Cmd+D on a Mac). The top cell's formula fills the rest of the selection.
- Copy and paste. Copy the formula cell, select the target range, and paste. The formula adjusts for each cell in the range.
Cutting is different from copying. If you cut C2 and paste it into E2, Sheets moves the formula and keeps its references exactly as written, so it still points at B2. Cutting is for moving a formula. Copying is for reusing it.
When a reference must not move
Relative references are what you want for the value being tested, such as each student's score. They are wrong for a value that every row should share, such as the pass mark.
Imagine you put the pass mark in F1 and write this in C2:
=IF(B2>=F1, "Pass", "Fail")It works in row 2. Copy it down, and in C3 the formula becomes =IF(B3>=F2, "Pass", "Fail"). F2 is empty. Now each row compares its score against a different cell, and the results are wrong.
The failure is hard to notice because it produces no error message. An empty cell is treated as 0 in a numeric comparison, so B3>=F2 is true for any score that is 0 or higher. Nearly every row shows "Pass", and the formula looks fine at a glance.
The fix is an absolute reference, which uses dollar signs to lock the reference:
=IF(B2>=$F$1, "Pass", "Fail")The $ before the column letter locks the column. The $ before the row number locks the row. No matter where you copy this formula, $F$1 always points at F1. The reference to B2 has no dollar signs, so it still shifts row by row.
A practical benefit follows. When the pass mark changes from 50 to 55, you edit F1 once and every row updates. If you had typed 50 into each formula, you would have to edit every row. Putting thresholds in cells and pointing at them is a habit worth keeping for any IF formula that uses a rule value.
Typing the dollar signs
You can type the $ characters by hand. You can also click inside a reference while editing the formula and press F4 on Windows. Each press cycles through the four forms: F1, $F$1, F$1, and $F1. On a Mac, the shortcut can differ by keyboard settings, so typing the dollar signs is the reliable option.
Mixed references: lock only one direction
The third and fourth forms above are mixed references. They lock either the row or the column, but not both.
| Form | Column | Row | Behavior when copied |
|---|---|---|---|
| `F1` | moves | moves | Both shift |
| `$F$1` | locked | locked | Neither shifts |
| `F$1` | moves | locked | Only the column shifts when copied sideways |
| `$F1` | locked | moves | Only the row shifts when copied down |
Mixed references matter when you copy a formula in two directions. Say you have scores in B2:B20, and row 1 of columns C, D, and E holds three different pass marks: 40, 50, and 60. You want one formula that shows, for each score, whether it passes each mark. Put this in C2:
=IF($B2>=C$1, "Pass", "Fail")Read it piece by piece:
$B2locks the column, so every copy still reads scores from column B. The row is free, so it follows each student down the sheet.C$1locks the row, so every copy still reads the pass mark from row 1. The column is free, so copying to D givesD$1and copying to E givesE$1.
Copy C2 across to E2, then fill down. One formula now fills a whole grid. With only absolute or only relative references, you would need to edit it for each column.
A quick rule for choosing a form: ask what should stay put when the formula moves down, and what should stay put when it moves right. Lock only those parts.
Blank rows and filled-down formulas
Once a formula is copied down, it runs on every row, including rows with no data. For a row where B is empty, B5>=50 compares 0 with 50, which is false, so the cell shows "Fail". A student who has not been entered looks like a student who failed.
The earlier chapter on blank checks covers this in detail. The version that fits a fill-down workflow wraps the grading test in a check on a required column:
=IF(A2="", "", IF(B2>=$F$1, "Pass", "Fail"))The outer IF returns an empty string when the name in A2 is missing. The inner IF only runs for rows that have a name. This lets you fill the formula far below your current data. New rows grade themselves as you type names and scores into them.
Checking your result after copying
After filling down, do three quick checks.
- Click the first, a middle, and the last formula cell. Read the formula bar. The relative references should have shifted by the right number of rows. Locked references should be identical in every cell.
- Look for suspicious uniformity. If every row says "Pass", or every row says "Fail", a reference is probably pointing at the wrong place. This is the usual sign of a missing
$. - Change the input once. Edit the pass mark in F1 or one score, and confirm that the affected rows change. A formula that ignores a change you made is reading the wrong cell.
For a more visual check, press Ctrl+` (the backtick key). That switches the sheet to show formulas instead of results, so you can scan a whole column of formulas at once. Press it again to switch back.
What changes when rows or columns are inserted
Absolute references adjust when you insert or delete rows and columns, because they point to a cell and not to a position. If you insert a new row above row 1, the pass mark moves from F1 to F2, and $F$1 in your formulas is rewritten as $F$2. The formulas keep pointing at the same pass mark.
Deleting the cell a reference points to is different. If you delete the row that holds the pass mark, the reference becomes #REF! and every dependent formula shows that error. Keep rule cells such as thresholds in a stable spot, away from rows you might delete. A separate area at the top or side of the sheet works well.
A short word on ARRAYFORMULA
Copying a formula down is the standard approach, and it has one drawback: the formula must be copied again whenever the data grows past the filled area. A later chapter covers ARRAYFORMULA, which lets one formula in the first cell fill the whole column. For now, treat copy-down as the baseline technique. It is also the one you will meet most often in other people's sheets.
Common problems when filling down
- Every row shows the same result. A reference that should shift is locked with
$, or a reference that should be locked is missing its$. Check the first row's formula against the rows below it. - The results are right in row 2 and wrong from row 3 on. The formula in row 2 used a plain reference to a rule cell. Add
$signs to the rule cell's reference and fill down again. - The fill stopped early. Double-click fill hit a blank in the neighboring column. Fill with Ctrl+D over an explicit selection instead.
- Some cells are formulas and some are typed values. Someone typed over a formula. Select the whole column and fill down again from the top to restore consistency.
- Pasting brings in formatting you did not want. Use Edit > Paste special > Formula only, or Ctrl+Shift+V for values only, when you need to control what is pasted.
Exercises
Exercise
Column B holds scores in rows 2 to 20, and the pass mark is in cell E1. You write =IF(B2>=E1, "Pass", "Fail") in C2 and fill it down. Row 2 is correct, but nearly every row below shows "Pass". Why, and how do you fix it?
Show answer
The reference E1 is relative, so in C3 it becomes E2, in C4 it becomes E3, and so on. Those cells are empty and count as 0 in the comparison, so B3>=E2 is true for almost any score. Lock the reference with dollar signs: =IF(B2>=$E$1, "Pass", "Fail"), then fill down again.
Exercise
Scores are in B2:B20. Cells C1, D1, and E1 hold the pass marks 40, 50, and 60. Write one formula for C2 that you can copy across to E2 and down to row 20, returning "Pass" or "Fail" against the mark at the top of each column.
Show answer
=IF($B2>=C$1, "Pass", "Fail"). The $B locks the score column when copying across. The $1 locks the row of pass marks when copying down. The row of B and the column of C are left free to shift.