Sub topic 11
Preventing errors such as division by zero
This chapter assumes you can write a basic IF formula, use comparison operators, copy formulas down a column with the fill handle, and use absolute references. It also assumes you know the empty-string result ("") from the chapter on blank cells. Here you will use IF to stop errors before they appear, and you will learn where that approach has limits.
Why errors appear in formulas
An error value is a result Excel shows when a formula cannot produce a valid answer. Each error starts with #. A single error in a cell can spread. Any formula that refers to that cell, including a SUM over a range that contains it, usually returns the same error.
The error you meet most often in IF work is #DIV/0!. It appears when a formula divides by zero or by an empty cell. Empty cells count as zero in arithmetic.
Here is a small worksheet that calculates revenue per unit:
| Row | A | B | C | D |
|---|---|---|---|---|
| 1 | Product | Revenue | Units | Revenue per unit |
| 2 | Pens | 500 | 100 | 5 |
| 3 | Pads | 300 | 0 | #DIV/0! |
| 4 | Clips | 200 | #DIV/0! |
The formula in D2 is =B2/C2. Row 3 has a zero in C3. Row 4 has an empty C4, which Excel reads as zero. Both rows return #DIV/0!.
The guard pattern
A guard is an IF test that checks for the bad condition first. If the condition is present, the formula returns a safe value. If it is not, the formula runs the calculation.
=IF(C2=0,"",B2/C2)Read it in three parts:
- The logical test is
C2=0. It asks whether the divisor is zero. - The value if true is
"", an empty string. The cell looks blank. - The value if false is
B2/C2. This part runs only when C2 is not zero.
Excel evaluates only the branch that the test selects. When C2 is zero, Excel never attempts the division, so no error occurs.
Copy the guarded formula down column D of the worksheet above and you get this:
| Row | A | B | C | D |
|---|---|---|---|---|
| 2 | Pens | 500 | 100 | 5 |
| 3 | Pads | 300 | 0 | (looks blank) |
| 4 | Clips | 200 | (looks blank) |
Row 4 works because an empty C4 equals 0 in the test C4=0. The comparison is TRUE for both a typed zero and a truly empty cell.
Choosing what the guard returns
An empty string is not the only choice. Pick the safe value based on how the result will be used.
| Safe value | Example | Good for | Risk |
|---|---|---|---|
| Empty string | `=IF(C2=0,"",B2/C2)` | Reports people read | Text in a numeric column can break later math |
| Zero | `=IF(C2=0,0,B2/C2)` | Columns you sum or chart | A zero looks like a real result |
| A message | `=IF(C2=0,"No units",B2/C2)` | Making the gap visible | Text, so math on it fails |
| `NA()` | `=IF(C2=0,NA(),B2/C2)` | Charts that should skip the point | Shows `#N/A` in the cell |
The risk column matters most. The empty string "" is text, not a number. If you later write =D2+10 and D2 holds "", Excel returns #VALUE!. You moved the error downstream instead of removing it.
SUM, AVERAGE, MIN, and MAX skip text in ranges, so they handle "" safely. The plus operator does not.
=SUM(D2:D4)This formula returns 5 for the worksheet above, even though D3 and D4 hold empty strings. By contrast, =D2+D3 returns #VALUE!.
Guarding a bigger calculation
The guard can protect an expression of any size. Suppose you calculate margin as profit divided by revenue, and profit is itself a calculation:
=IF(B2=0,"",(B2-C2)/B2)The test checks the divisor, B2. The false branch holds the whole calculation. Test the value that ends up in the denominator. If the denominator is a calculation, test the calculation:
=IF(B2-C2=0,"",D2/(B2-C2))Here the denominator is B2-C2, so the test uses the same expression. A common mistake is testing a nearby cell instead of the true divisor.
Guarding more than one input
Sometimes an error can come from several causes. Use OR to test them together.
=IF(OR(C2=0,B2=""),"",B2/C2)If either the divisor is zero or the numerator is blank, the formula returns an empty string. Order inside OR does not matter for the result here, because both tests are safe comparisons.
Use AND when you need every input to be valid before calculating:
=IF(AND(ISNUMBER(B2),ISNUMBER(C2),C2<>0),B2/C2,"Check inputs")ISNUMBER returns TRUE when a cell holds a number and FALSE for text or an empty cell. This formula calculates only when both cells are numbers and the divisor is not zero. Note that AND evaluates every argument. Excel does not skip the later tests once an earlier one is FALSE. This is safe here because C2<>0 produces no error when C2 holds text, since a comparison never fails. A calculation inside an AND argument would fail, so keep calculations in the false and true branches.
Other functions for the same job
Excel has functions built for error handling. They differ from the IF guard in an important way.
IFERROR
IFERROR runs a formula and, if it returns any error, returns a value you provide.
=IFERROR(B2/C2,"")This is shorter than the IF guard. It is also broader. IFERROR hides every error: #DIV/0!, #VALUE!, #REF!, #NAME?, and more. That behavior is the main drawback. A misspelled function name or a deleted column also gives a blank cell instead of a visible warning, so a real bug can hide for a long time.
IFNA
IFNA catches only #N/A, which lookup functions return when they find no match. It leaves other errors visible.
=IFNA(VLOOKUP(A2,F:G,2,FALSE),"Not found")Comparing the options
| Approach | Catches | Best when |
|---|---|---|
| `IF(C2=0,"",B2/C2)` | Only the condition you test | You know the one expected cause |
| `IFERROR(B2/C2,"")` | Every error type | You want a quick catch-all and have checked the formula |
| `IFNA(...)` | Only `#N/A` | A lookup may legitimately find nothing |
Prefer the IF guard for division. It documents the exact condition you expect, and it lets unexpected errors show up so you can fix them. Use IFERROR when many different inputs can fail and you cannot list every case.
Guarding against bad text inputs
Division by zero is not the only avoidable error. Arithmetic on text produces #VALUE!. If a cell holds the text "N/A" or a number stored as text, a calculation using it fails.
=IF(ISNUMBER(B2),B2*1.2,"")This formula multiplies B2 by 1.2 only when B2 holds a real number. Otherwise it returns an empty string.
A number stored as text often appears left-aligned with a small green triangle in the corner of the cell. ISNUMBER returns FALSE for it, so the guard returns "". If that surprises you, the guard has done its job: it revealed a data problem. Fix the source, for example with Data > Text to Columns or by multiplying the column by 1 with Paste Special.
Guarding errors that flow in from other cells
Suppose D2 already contains an error and E2 uses it. A guard on E2's own inputs cannot help, because the problem is upstream. ISERROR tests for any error:
=IF(ISERROR(D2),"",D2*2)ISERROR returns TRUE if D2 holds any error value. This formula works like IFERROR and has the same drawback. Prefer fixing D2 with a specific guard, and let E2 inherit the clean result.
Excel also has ISERR, which is TRUE for every error except #N/A, and ISNA, which is TRUE only for #N/A.
Common mistakes
- Testing the wrong cell. The guard tests C2 but the formula divides by D2. Make the test and the denominator match.
- Forgetting that blanks equal zero.
C2=0is TRUE for an empty cell. If you need to tell zero from blank, testC2=""separately, or useISBLANK(C2). - Returning
""and then doing math. Use zero, or test the guarded cell with ISNUMBER before the next calculation. - Using IFERROR everywhere. A catch-all hides typos and broken references.
- Writing the calculation outside the IF.
=B2/C2+IF(C2=0,0,1)still divides by zero. The division must sit inside the false branch. - Missing quotes.
=IF(C2=0,,B2/C2)returns 0, not an empty string, because an omitted argument becomes zero. Write""explicitly.
Worked example
You track a campaign. Column B holds clicks, column C holds impressions, and column D should show click-through rate (clicks divided by impressions) as a percentage.
| Row | A | B | C | D |
|---|---|---|---|---|
| 1 | Ad | Clicks | Impressions | Rate |
| 2 | Spring | 40 | 2000 | |
| 3 | Summer | 0 | 0 | |
| 4 | Autumn | 15 | ||
| 5 | Winter | n/a | 1500 |
Enter this in D2 and copy it down to D5:
=IF(AND(ISNUMBER(B2),ISNUMBER(C2),C2<>0),B2/C2,"Check data")Then format column D as Percentage. The results are:
| Ad | Result | Reason |
|---|---|---|
| Spring | 2.0% | Both inputs are numbers and C2 is not zero |
| Summer | Check data | Impressions are zero |
| Autumn | Check data | C4 is empty, so `ISNUMBER` returns FALSE |
| Winter | Check data | B5 holds text |
The formula separates three kinds of bad input and handles all of them with one visible message. A reader can filter column D for "Check data" and fix those rows.
Summary
- Excel treats an empty cell as zero in arithmetic, so a blank divisor produces
#DIV/0!. - A guard tests the problem condition first and puts the calculation in the false branch.
- Choose the safe value deliberately.
""looks clean but is text and can break later math. - Test the actual denominator, including when it is an expression.
- Prefer a specific IF guard over IFERROR so that unexpected errors stay visible.
- ISNUMBER is a reliable test that an input is usable before you calculate.
Exercise
Column B holds total cost and column C holds the number of items. Write a formula for D2 that returns the cost per item, shows the text "No items" when C2 is zero or empty, and does not hide other errors.
Show answer
=IF(C2=0,"No items",B2/C2). The test C2=0 is TRUE for both a zero and an empty cell, so the division runs only when C2 holds a nonzero value. A specific IF guard is used instead of IFERROR so that errors from other causes, such as text in B2, still appear.
Exercise
D2 contains =IF(C2=0,"",B2/C2). In E2 you enter =D2*100. For a row where C2 is zero, what does E2 return, and how can you change E2 so it returns an empty string instead?
Show answer
E2 returns #VALUE!, because D2 holds an empty string, which is text, and multiplication cannot use text. Change E2 to =IF(ISNUMBER(D2),D2*100,"") so it calculates only when D2 holds a number.
Exercise
A formula reads =IF(B2/C2>0.5,"High","Low"). It returns #DIV/0! when C2 is zero. Rewrite it so that it returns "n/a" in that case.
Show answer
=IF(C2=0,"n/a",IF(B2/C2>0.5,"High","Low")). The outer IF checks the divisor first. The original formula failed because the division sat inside the logical test, which Excel always evaluates. The guard must wrap the entire expression that contains the division.