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 see what happens when a range has no numbers or contains an error value, and how to guard your formulas.
What happens when there is nothing to calculate
The five functions do not all react the same way to an empty data set. A range counts as empty here if it has only blanks or text.
| Formula | Result on a range with no numbers |
|---|---|
| `=SUM(B2:B6)` | 0 |
| `=COUNT(B2:B6)` | 0 |
| `=MIN(B2:B6)` | 0 |
| `=MAX(B2:B6)` | 0 |
| `=AVERAGE(B2:B6)` | #DIV/0! |
AVERAGE fails because it divides the sum by the count of numeric cells, and that count is 0. SUM, MIN, and MAX return 0, which looks like a real result. A MIN of 0 on an empty range can be mistaken for an actual minimum, so check the count first when it matters.
Guarding with COUNT
The safest guard tests whether any numbers exist before calculating.
=IF(COUNT(B2:B6)=0, "No data", AVERAGE(B2:B6))The same pattern protects MIN and MAX from returning a misleading 0.
=IF(COUNT(B2:B6)=0, "No data", MIN(B2:B6))This guard targets one specific problem. It does not hide other errors, which is usually what you want.
Errors inside the range
An error value in a cell is different from an empty range. SUM, AVERAGE, MIN, and MAX return the first error they meet, so one bad cell breaks the whole result. This is called error propagation.
COUNT is the exception. It ignores error cells, so =COUNT(B2:B6) still returns a number. COUNTA does count error cells, because they are not empty.
Find the source of the error before you hide it. Select the cell that shows the error, then use Formulas > Error Checking or Trace Precedents to follow it back.
Hiding errors with IFERROR
IFERROR returns a fallback value when its first argument produces any error.
=IFERROR(AVERAGE(B2:B6), "No data")This is short, but it is a blunt tool. It also hides a #REF! from a deleted column or a #NAME? from a typo, and you would see "No data" instead of a clue that the formula is broken. Prefer the COUNT test when your only concern is an empty range. Use IFERROR when you cannot predict which error will appear.
Ignoring errors with AGGREGATE
If the range contains errors you want to skip, use AGGREGATE. Its second argument is an option code, and 6 means "ignore error values".
=AGGREGATE(9, 6, B2:B6)
=AGGREGATE(1, 6, B2:B6)
=AGGREGATE(5, 6, B2:B6)
=AGGREGATE(4, 6, B2:B6)The first argument selects the function: 9 is SUM, 1 is AVERAGE, 5 is MIN, and 4 is MAX. The result covers only the valid numbers. Use this carefully, because skipping an error means the total no longer includes that row.
Quick checklist
- If AVERAGE shows #DIV/0!, check whether the range holds any numbers. Numbers stored as text are a common cause.
- If several summary cells show the same error, look for one bad cell in their shared range.
- If MIN or MAX shows 0 unexpectedly, test with COUNT.
- Choose the narrowest guard that solves the problem.
Exercise
B2:B6 contains 10, 20, #N/A, 40, and a blank cell. What do =SUM(B2:B6), =COUNT(B2:B6), and =AGGREGATE(9, 6, B2:B6) return?
Show answer
=SUM(B2:B6) returns #N/A because the error propagates. =COUNT(B2:B6) returns 3, counting only 10, 20, and 40. =AGGREGATE(9, 6, B2:B6) returns 70, because option 6 ignores the error value.
Exercise
Why is =IF(COUNT(B2:B6)=0, "No data", AVERAGE(B2:B6)) often better than wrapping AVERAGE in IFERROR?
Show answer
It handles only the empty-range case. IFERROR would also hide unrelated errors, such as #REF! or #NAME?, which signal a real problem in the worksheet.