Sub topic 2
Fixing #DIV/0!: dividing by zero or an empty cell
This chapter assumes you can enter and edit formulas, use cell references and ranges such as A2 and A2:A10, and use basic functions such as SUM and AVERAGE. It also assumes you know that Excel treats numbers and text in cells differently.
What #DIV/0! means
Excel returns #DIV/0! when a formula tries to divide a number by zero. Division by zero has no defined result in arithmetic, so Excel shows an error value instead of a number.
The error appears in more situations than a literal zero. All of these produce it:
- The divisor is a cell that contains 0.
- The divisor is a blank cell. Excel treats a blank cell as 0 in arithmetic.
- The divisor is a formula that evaluates to 0, such as
=A2/(B2-C2)when B2 and C2 are equal. - The formula calls a function that divides internally and has nothing to divide by, such as AVERAGE on a range with no numbers.
The most common cause in real worksheets is a blank cell that has not been filled in yet. A formula copied down a column of 500 rows will show #DIV/0! in every row where the input is still empty.
A worked example
Suppose a sales sheet has units sold in column A and number of orders in column B. Column C shows units per order.
| Row | A (units) | B (orders) | C formula | C result |
|---|---|---|---|---|
| 2 | 120 | 4 | `=A2/B2` | 30 |
| 3 | 80 | 0 | `=A2/B2` copied down | #DIV/0! |
| 4 | 50 | (blank) | `=A2/B2` copied down | #DIV/0! |
Row 3 fails because B3 holds 0. Row 4 fails because B4 is empty, and Excel reads the empty cell as 0. The formulas are correct. The data is incomplete, or the zero is a legitimate value that the formula does not account for.
Before you change anything, decide which case you are in. That decision determines the fix.
Find the cause first
Select the cell that shows the error and work through these checks in order.
- Click the cell and read the formula in the formula bar.
- Identify the divisor. It is the part after the
/operator, or the argument of a function such as AVERAGE. - Look at the divisor's value. If it is a reference, click that cell. If it is an expression, use Evaluate Formula on the Formulas tab to see what it computes.
- Decide whether a zero or blank is expected. A new row with no data yet is expected. A zero in a column that should never be zero points to a data problem.
> Note: this chapter covers the single-cell case. Later chapters cover Error Checking, Trace Precedents and Evaluate Formula in detail.
Fix 1: Prevent the division with IF
The most direct fix is to test the divisor before dividing. If it is zero, return something else, and otherwise divide.
=IF(B2=0,"",A2/B2)The condition B2=0 is true when B2 holds 0 and also when B2 is blank, because Excel compares a blank cell to 0 as equal. So this single test covers both cases. When the test is true, the formula returns an empty string and the cell looks empty. When it is false, the formula divides normally.
You can return other values instead of an empty string:
0if the result feeds later arithmetic and a zero is acceptable."n/a"or"No orders"if a reader should see why there is no number.NA()if you want charts to skip the point. Charts ignore #N/A values rather than plotting them as zero.
A caution about empty strings
Returning "" makes the cell look blank, but it holds text. Any formula that later does arithmetic on that cell, such as =C2*2, returns #VALUE!. If other formulas depend on the result, return a number or NA() and handle that downstream, or use functions that skip text, such as SUM and AVERAGE. This is the usual way a fix for one error creates another.
Fix 2: Handle AVERAGE on an empty range
AVERAGE adds the numbers in a range and divides by how many there are. If the range holds no numbers, the count is zero, and AVERAGE returns #DIV/0!. This happens when a range is empty, when it holds only text, or when a filter or earlier formula has removed all the values.
=IFERROR(AVERAGE(B2:B10),"No data")IFERROR evaluates its first argument. If that argument returns any error, IFERROR returns the second argument instead. Here, a range with no numbers gives "No data".
IFERROR is convenient, but it replaces every error type, not only #DIV/0!. If B2:B10 contained a #REF! error from a deleted column, this formula would also show "No data", and the real fault would be hidden. For a range average, a more precise alternative is to test the count first:
=IF(COUNT(B2:B10)=0,"No data",AVERAGE(B2:B10))COUNT returns how many cells in the range hold numbers. This version only intercepts the empty case. If B2:B10 contains an error value, COUNT ignores that cell and AVERAGE still returns the error, so you still see it.
Fix 3: Guard a calculated divisor
When the divisor is itself a calculation, test the calculation, not a single cell. Consider a growth rate:
=(C2-B2)/B2If B2 is the previous period's value and it is zero, the formula fails. Guard it the same way:
=IF(B2=0,"",(C2-B2)/B2)For a divisor that is a difference, put the whole expression in the test:
=IF(B2-C2=0,"",A2/(B2-C2))Be careful with decimals. Values such as 0.1+0.2 and 0.3 can differ by a tiny amount because of binary floating-point storage. A divisor that should be exactly zero may be 5.55E-17, which is not equal to 0, so the test passes and the result is an enormous, meaningless number. If your divisor is a difference of calculated decimals, round it first:
=IF(ROUND(B2-C2,6)=0,"",A2/(B2-C2))Fix 4: Fix the data, not the formula
Sometimes the right response is to leave the formula alone and correct the input. Ask these questions:
- Should the divisor ever be zero here? If a quantity of orders can never be zero, a zero is a data-entry mistake. Hiding the error means the mistake goes unnoticed.
- Is the cell blank because data has not arrived yet? Then a temporary "" is reasonable, and the error will clear itself.
- Did a lookup feed the divisor? If the lookup returned 0 because it found an empty cell in the source table, the problem is upstream.
A visible #DIV/0! is useful when it signals real missing data. Treat it as a flag to investigate before you suppress it.
Choosing between the approaches
| Situation | Best approach | Why |
|---|---|---|
| Divisor is a single cell that may be blank or 0 | `=IF(B2=0,"",A2/B2)` | Handles the one expected failure and nothing else |
| AVERAGE over a range that may hold no numbers | `=IF(COUNT(range)=0,"No data",AVERAGE(range))` | Does not hide unrelated errors in the range |
| Quick report where any failure should look clean | `=IFERROR(A2/B2,"")` | Short, but masks every error type |
| Divisor should never be zero | Fix the data | The error is doing useful work |
| Chart should skip missing points | `=IF(B2=0,NA(),A2/B2)` | Charts leave #N/A points unplotted |
Common mistakes
- Wrapping everything in IFERROR. A mistyped reference or a deleted column then looks the same as a harmless blank. Prefer a targeted IF test when you know which case you expect.
- Testing for blank with
=""on a numeric cell. For a truly empty cell,B2=""is true, but for a cell holding 0 it is false. UseB2=0to catch both blank and zero. - Returning "" and then doing math on it. The text result causes #VALUE! in the next formula.
- Fixing only the first row. After you edit one formula, copy it down the whole column, or convert the range to a table so new rows inherit the formula.
- Comparing with a tolerance never applied. Floating-point differences make a "zero" divisor slightly nonzero. Round when subtracting decimals.
Check your work
After you apply a fix, confirm it does what you intended.
- Test a normal row with a nonzero divisor, and check that the number is unchanged.
- Test a row with 0 in the divisor.
- Test a row with the divisor cell cleared.
- Type text such as
abcinto the divisor and confirm you see #VALUE!. A guard against zero should not hide a type problem.
If step 4 shows your fallback value instead of #VALUE!, the formula is masking more than you meant it to.
Exercise
Cell A2 holds 90 and cell B2 is empty. The formula in C2 is =A2/B2 and shows #DIV/0!. Write a formula for C2 that returns an empty-looking cell when B2 is blank or zero, and divides normally otherwise. Then explain why this is safer than =IFERROR(A2/B2,"").
Show answer
Use =IF(B2=0,"",A2/B2). The test B2=0 is true for both a blank cell and a zero. It is safer than IFERROR because it only intercepts the divide-by-zero case. If B2 held text, this formula would still return #VALUE!, and if A2 held a #REF! error, that error would still show. IFERROR would replace every one of those with "", so real mistakes would be hidden.
Exercise
The range D2:D8 currently contains only text labels, and =AVERAGE(D2:D8) shows #DIV/0!. Write a formula that shows "No data" in this case but still shows an error if one of the cells in D2:D8 contains an error value such as #REF!.
Show answer
Use =IF(COUNT(D2:D8)=0,"No data",AVERAGE(D2:D8)). COUNT returns 0 because no cell holds a number, so the formula returns "No data". If a cell in the range holds #REF!, COUNT skips it, but when numbers are also present AVERAGE returns the #REF! error, so the fault stays visible.