Sub topic 12
Handling errors and empty data sets
This chapter assumes you can write SUM, AVERAGE, MIN, MAX, and COUNT formulas over a range. It also assumes you know how these functions treat blanks and text. Here you will learn what happens when a range has no numbers or contains an error value, and how to make formulas fail in a controlled way.
What an error value is
An error value is a result that starts with # and tells you a formula could not produce a normal answer. It is a value like any other, so other formulas can read it. That is why one error can spread through a worksheet.
| Error | Typical cause in summary formulas |
|---|---|
| `#DIV/0!` | AVERAGE over a range with no numbers, or your own division by zero or by a blank cell |
| `#VALUE!` | Arithmetic on text, such as `=B2+"abc"` |
| `#REF!` | A cell or range the formula used was deleted |
| `#NAME?` | A misspelled function name, such as `=SUMM(B2:B6)` |
| `#N/A` | A lookup found nothing, or someone entered `NA()` on purpose |
| `#NUM!` | An invalid numeric operation |
A cell full of #### is not an error value. It means the column is too narrow to show the number. Widen the column.
Empty data sets: what each function returns
A range with no numbers is a normal situation. A new month has no entries yet, or a filter leaves nothing to show. The five functions do not respond to it in the same way.
=SUM(B2:B6)SUM returns 0 when the range has no numbers. MIN and MAX also return 0. COUNT returns 0, which is correct.
=AVERAGE(B2:B6)AVERAGE returns #DIV/0!. It divides the sum by the count of numeric cells, and that count is zero.
The zeros from MIN and MAX are a quiet trap. A 0 from =MIN(B2:B6) can mean "the smallest value is 0" or "there are no values". Nothing in the cell tells you which. A blank range produces the same display as a range of real zeros.
Guarding with COUNT
The standard guard tests whether any numbers exist before computing.
=IF(COUNT(B2:B6)=0, "No data", AVERAGE(B2:B6))COUNT is the right test here because it counts exactly the cells AVERAGE would use. COUNTA would be wrong. A range holding only the text n/a has a COUNTA of 1, but AVERAGE still fails.
The guard works the same way for MIN and MAX, which fixes the ambiguous zero:
=IF(COUNT(B2:B6)=0, "No data", MAX(B2:B6))You can return an empty string instead of a message:
=IF(COUNT(B2:B6)=0, "", AVERAGE(B2:B6))If the result feeds a chart, return NA() instead of text. Charts skip #N/A points, while they plot text as zero.
=IF(COUNT(B2:B6)=0, NA(), AVERAGE(B2:B6))Guarding your own division
The same idea applies when you divide by a cell. A blank cell counts as 0 in arithmetic, so it causes #DIV/0!.
=IF(C2=0, "", B2/C2)For a percentage of a total, guard the denominator, not the numerator:
=IF(SUM($B$2:$B$6)=0, 0, B2/SUM($B$2:$B$6))The absolute references keep the denominator fixed when you fill the formula down the column.
How errors propagate
Inside a range, SUM, AVERAGE, MIN, and MAX do not skip errors. If any cell in the range holds an error value, the function returns that same error. This holds even when the other cells are fine.
Take these values in B2:B6: 120, #N/A, 80, a blank cell, and 100.
=SUM(B2:B6)The result is #N/A, not 300. AVERAGE, MIN, and MAX also return #N/A.
COUNT and COUNTA behave differently from the others:
=COUNT(B2:B6)
=COUNTA(B2:B6)
=COUNTBLANK(B2:B6)For the data above, these return 3, 4, and 1. COUNT ignores the error because it is not a number. COUNTA counts it because the cell is not empty. COUNTBLANK counts only the one empty cell.
This table summarizes the behavior.
| Function | Range with no numbers | Range containing an error value |
|---|---|---|
| SUM | `0` | Returns the error |
| AVERAGE | `#DIV/0!` | Returns the error |
| MIN | `0` | Returns the error |
| MAX | `0` | Returns the error |
| COUNT | `0` | Does not count the error cell |
| COUNTA | `0` if the range is empty | Counts the error cell |
An error also spreads through chains of formulas. If B3 is #N/A and a total in E2 sums B2:B6, then E2 is #N/A. A grand total in F2 that adds E2 to other cells is #N/A as well. The error you see at the end of the chain is rarely where the problem started.
Finding the source of an error
Work backwards from the visible error to the first cell that holds one.
- Select the cell showing the error and look for the small green triangle in its corner. The warning icon next to it has a menu with Show Calculation Steps.
- On the Formulas tab, use Trace Precedents to draw arrows to the cells the formula reads.
- Use Evaluate Formula to step through the calculation one part at a time.
- Check the cell at the start of the chain. It is usually a lookup, a division, or a deleted reference.
To see how many errors a range contains, count them:
=SUMPRODUCT(--ISERROR(B2:B6))ISERROR returns TRUE or FALSE for each cell. The double minus turns those into 1 and 0, and SUMPRODUCT adds them.
Summarizing a range that contains errors
Sometimes errors are expected, for example when some rows come from a lookup that finds nothing. You then want results that ignore them. You have two options.
The AGGREGATE function takes a function number and an option number. Option 6 means "ignore error values".
=AGGREGATE(9, 6, B2:B6)
=AGGREGATE(1, 6, B2:B6)
=AGGREGATE(4, 6, B2:B6)
=AGGREGATE(5, 6, B2:B6)
=AGGREGATE(2, 6, B2:B6)The function numbers are 9 for SUM, 1 for AVERAGE, 4 for MAX, 5 for MIN, and 2 for COUNT. With the earlier data, the first formula returns 300.
AGGREGATE does not protect you from the empty case. If every cell is an error, AGGREGATE(1, 6, B2:B6) still returns #DIV/0!, so combine it with a test:
=IF(COUNT(B2:B6)=0, "No data", AGGREGATE(1, 6, B2:B6))IFERROR and its risks
IFERROR returns a replacement value when its first argument produces any error.
=IFERROR(AVERAGE(B2:B6), "No data")This is short and it works. The danger is that it hides every kind of error, including ones you did not expect. Suppose you delete a column and the formula becomes #REF!. IFERROR shows "No data", and nobody notices the formula is broken.
Prefer these choices, in order:
- Test the condition that causes the error, as with
COUNT(...)=0. Real problems still show up as errors. - Use IFNA when you only expect
#N/A. It leaves#DIV/0!and#REF!visible. - Use IFERROR only when any failure really should produce the same fallback, and say so in a note next to the formula.
=IFNA(AVERAGE(B2:B6), "No data")Common mistakes
- Using COUNTA to decide whether AVERAGE is safe. Text and errors make COUNTA positive while COUNT is still zero.
- Reading a
0from MIN or MAX as data. Check COUNT first when "no values" is possible. - Wrapping every formula in IFERROR "to be safe". This turns errors into silent wrong answers.
- Returning
""from a guard and then using the cell in arithmetic. UseNA()or reference the cell inside a function like SUM. - Expecting a total to skip an error cell. Only AGGREGATE with option 6, or fixing the source, does that.
Check your understanding
Exercise
B2:B6 contains 50, the text "n/a", 70, a blank cell, and 90. In E2 you want the average, but show "Waiting" when the range has no numbers. Which formula is correct and what does it return? Then explain what changes if B3 is replaced by the error value #N/A.
Show answer
Use =IF(COUNT(B2:B6)=0, "Waiting", AVERAGE(B2:B6)). COUNT finds 3 numbers (50, 70, 90), so the test is false and AVERAGE returns 70, because it skips the text and the blank cell. If B3 becomes #N/A, COUNT still returns 3 because it ignores errors, so the guard passes through to AVERAGE. AVERAGE then returns #N/A, since it does not skip error values. To ignore the error, use =IF(COUNT(B2:B6)=0, "Waiting", AGGREGATE(1, 6, B2:B6)), which returns 70.
Exercise
A worksheet shows 0 in a cell with =MAX(C2:C20), and a colleague says the highest sale was zero. How can you check whether that is true, and how would you change the formula so the two cases look different?
Show answer
Check =COUNT(C2:C20). If it returns 0, the range holds no numbers and the 0 means "no data", not a highest sale of zero. To separate the cases, use =IF(COUNT(C2:C20)=0, "No data", MAX(C2:C20)).