Sub topic 10
Common mistakes and how to debug them
This chapter assumes you can enter and edit formulas, select ranges, and fill formulas with the fill handle. It also assumes you know the three reference types: relative (B2), absolute ($B$2), and mixed ($A2 or A$1). Here you will learn the errors that wrong dollar signs cause, how to spot them, and how to fix them.
Why reference mistakes are hard to see
A wrong reference rarely produces an error message. The formula runs, returns a number, and the number is wrong. The first row often looks correct, because the first formula you type is usually right. The mistake only shows up in the rows you filled below it.
That makes the debugging habit simple: never trust a filled formula until you have inspected a row other than the first.
Mistake 1: a missing dollar sign
You multiply each price by a tax rate stored in E1 and write this in D2:
D2: =B2*E1D2 is correct. Fill it down and D3 becomes =B3*E2, D4 becomes =B4*E3, and so on. E2 and E3 are empty, so Excel treats them as 0 and the results are 0.
The fix is to lock the rate cell:
D2: =B2*$E$1Symptom: the first row is right and the rows below are 0, or are wrong in a pattern. Cause: a reference that should stay fixed was relative.
Mistake 2: a dollar sign where you did not want one
The opposite error is a locked reference that should move. Suppose you write this in C2:
C2: =$A$2*B2Every row now multiplies by A2, so C3 is =$A$2*B3 instead of =A3*B3. The results look plausible but all use the first row's quantity.
Symptom: the same input appears to drive every row. Cause: an absolute reference where a relative one was needed.
Mistake 3: locking the wrong part of a mixed reference
Mixed references are the most common source of subtle errors, because there are two ways to get them wrong. For a multiplication table with row headings in A2:A10 and column headings in B1:J1, the correct formula is:
B2: =$A2*B$1The column is locked on A and the row is locked on 1. Compare the wrong versions:
| Formula in B2 | What goes wrong |
|---|---|
| `=A2*B1` | Both headings shift as you fill, so the table drifts away from the headings. |
| `=$A$2*$B$1` | Every cell shows the same product. |
| `=A$2*$B1` | The locks are swapped. Filling right moves the row heading, and filling down moves the column heading. |
A reliable check is to ask two questions. When I fill right, which part must stay put? When I fill down, which part must stay put? Whatever must stay put on a given axis gets a $ on that axis.
Mistake 4: anchoring the wrong end of a range
Percent-of-total and running-total formulas both use a range, and the anchor goes on different ends.
C2: =B2/SUM($B$2:$B$10)Here the whole range is absolute, because every row divides by the same total. If you write SUM(B2:B10) without dollar signs, C3 sums B3:B11 and the shares are wrong.
C2: =SUM($B$2:B2)
C3: =SUM($B$2:B3)Here only the start is locked, so the range grows by one row per fill. Locking both ends gives the same total on every row. Locking neither gives a sum that slides and covers only one cell.
Mistake 5: references that break when cells move
Copying and cutting treat references differently. Copying a formula adjusts its relative references. Cutting and pasting leaves them pointing at the same cells. Inserting a row inside a referenced range stretches the range, and deleting a referenced cell turns the formula into #REF!.
If a result changes after a layout edit, check which of these you did. A formula that was copied may point at new cells. A formula that was cut still points at the old ones.
A debugging routine
Use these steps in order when a filled formula gives wrong results.
- Click a wrong cell and read its formula in the formula bar. Compare it with the first row.
- Press
Ctrl+[to jump to the cells the formula refers to. Check that they are the cells you intended. - Use Formulas > Show Formulas (or
Ctrl+``) to display every formula at once. Look for the row where the pattern breaks. - Use Formulas > Evaluate Formula to step through the calculation and see each reference resolve to a value.
- Decide for each reference whether it should move with the fill, stay fixed, or stay fixed on one axis. Add or remove
$accordingly, then pressF4to cycle the types while the cursor is in the reference. - Fill the corrected formula again over the whole range, not just the first cell.
Make mistakes less likely
- Put fixed inputs such as a tax rate in one labeled cell, and refer to that cell, instead of typing the number into every formula.
- Consider a named range such as
TaxRate, which behaves as an absolute reference and reads clearly. - Test a new formula by filling it and then checking the last row, not only the first.
Exercise
A sheet has prices in B2:B10 and a discount rate in F1. Someone writes =B2*(1-F1) in C2 and fills down. C2 is correct, but C3 and below look like the full price. What happened, and what is the corrected formula?
Show answer
After filling, C3 holds =B3*(1-F2), and F2 onward are empty, so the discount is treated as 0 and the result equals the full price. The discount reference must be absolute. The corrected formula is =B2*(1-$F$1), filled down again.
Exercise
You want a running total in C2:C10 of the values in B2:B10, but your formula =SUM(B2:B2) in C2, filled down, shows each row's own value instead of a cumulative total. Which formula fixes it, and why?
Show answer
Use =SUM($B$2:B2). Locking the start of the range at B2 keeps the range beginning at the top, while the relative end moves down with each row, so the range grows by one cell per row.