Sub topic 11
Preventing errors such as division by zero
This chapter assumes you can write a basic IF formula, use comparison operators, and copy a formula down a column with the fill handle. Here you will use IF to stop a calculation from running when its inputs would produce an error.
Why errors appear
Excel shows an error value when a formula cannot produce a result. The most common one in everyday sheets is #DIV/0!. It appears when a formula divides by zero or by an empty cell. Excel treats an empty cell as 0 in arithmetic.
Suppose column B holds revenue and column C holds the number of units sold. The formula =B2/C2 gives the price per unit. If C2 is 0 or blank, the result is #DIV/0!.
A single error is also a problem for other formulas. A SUM or AVERAGE over a range that contains #DIV/0! returns the same error. One bad row can break your totals.
The guard pattern
The fix is to test the risky input first. Check whether the divisor is zero, and only divide when it is not.
=IF(C2=0,"",B2/C2)Excel reads this in three steps:
- It evaluates the test
C2=0. - If the test is TRUE, the formula returns an empty string, which looks like a blank cell.
- If the test is FALSE, the formula returns
B2/C2.
The division sits in the value-if-false argument. Excel only calculates the argument it returns, so the division never runs when C2 is 0.
Choosing what to show instead
An empty string is not the only choice. Pick the value that makes sense for your readers and for any formulas that use the result.
| Guard result | Formula | Good for |
|---|---|---|
| Empty string | `=IF(C2=0,"",B2/C2)` | Reports where a blank looks clean |
| Zero | `=IF(C2=0,0,B2/C2)` | Results that feed into other calculations |
| Message | `=IF(C2=0,"No units",B2/C2)` | Sheets where the reader needs to know why |
Empty strings are text
An empty string "" is text, not a number. This matters when other formulas use the guarded cells.
SUMandAVERAGEignore text in a range, so they still work.- A direct operation such as
=D2*2returns#VALUE!if D2 holds"".
If the guarded result feeds more arithmetic, return 0 instead of "". You can then hide the zeros with a number format if you want a clean look.
Guarding with more than one condition
Sometimes more than one input can break the formula. Combine the checks with OR, as you did in an earlier chapter.
=IF(OR(B2="",C2=0),"",B2/C2)The formula returns an empty string if the revenue is missing or the unit count is zero. Otherwise it divides.
You can also reuse the blank-cell check from the previous chapter. The test C2="" is TRUE for a truly empty cell, and C2=0 is also TRUE for an empty cell, because Excel treats the blank as 0 in a numeric comparison. For a divisor, C2=0 alone therefore covers both cases.
Guarding other kinds of errors
Dividing by zero is the most common case, but the same pattern works elsewhere. Test the condition that causes the error, and return a safe value when it holds.
=IF(A2="","",B2*C2)This stops a row from showing a result until the user has entered an item in A2.
Common mistakes
- Dividing outside the IF. Writing
=B2/C2anywhere the test does not protect it still returns the error. - Testing the wrong cell. Check the divisor (C2), not the numerator (B2).
- Forgetting that
""is text. Later arithmetic on a guarded cell can return#VALUE!. Use 0 when other formulas depend on the result. - Missing quotes. Write
""for an empty string. Writing nothing between the commas, as inIF(C2=0,,B2/C2), returns 0, not an empty string.
Practice
Use this layout to try the formulas. Enter the values, put the guard formula in D2, and fill it down.
| Row | A (Product) | B (Revenue) | C (Units) |
|---|---|---|---|
| Row 2 | Pens | 500 | 100 |
| Row 3 | Paper | 300 | 0 |
| Row 4 | Ink | 450 | 90 |
With =IF(C2=0,"",B2/C2), D2 shows 5, D3 shows nothing, and D4 shows 5. Without the guard, D3 shows #DIV/0!.
Exercise
Cell B2 holds a total cost and C2 holds a number of people. Write a formula that returns the cost per person, and returns the text "No attendees" when C2 is 0. Then say why this guard is preferable to IFERROR in this case.
Show answer
=IF(C2=0,"No attendees",B2/C2). The formula tests only the situation you expect, a divisor of zero. IFERROR would also hide unrelated errors, such as a #VALUE! caused by text in B2, so real data problems would go unnoticed.
Exercise
A guarded formula in D2 returns "" when C2 is 0. In E2 you write =D2*2. What does E2 show when C2 is 0, and how do you change D2 so E2 works?
Show answer
E2 shows #VALUE!, because "" is text and cannot be multiplied. Change D2 to return 0 instead: =IF(C2=0,0,B2/C2).