Sub topic 14
Handling #DIV/0! in AVERAGEIFS
This chapter assumes you can write an AVERAGEIFS formula with a range and criteria pairs, and that you know what an Excel error value is. The sample data is the sales layout from earlier chapters, with Region in column B and Amount in column D.
Why the error appears
AVERAGEIFS adds up the matching values and divides by how many rows matched. If no row meets every condition, the count is zero. Dividing by zero gives the #DIV/0! error.
=AVERAGEIFS(D2:D100, B2:B100, "South")The sample data has no South rows, so this formula returns #DIV/0!. The formula is written correctly. The error tells you that nothing matched.
SUMIFS and COUNTIFS behave differently here. With no matches, SUMIFS returns 0 and COUNTIFS returns 0. Only AVERAGEIFS has no sensible answer, because an average of nothing is undefined.
Replace the error with IFERROR
Wrap the formula in IFERROR. It returns your replacement value when the inner formula produces an error.
=IFERROR(AVERAGEIFS(D2:D100, B2:B100, "South"), "No sales")If rows match, you see the average. If none match, you see the text "No sales".
Choose the replacement value carefully
The replacement affects any formula that uses this cell later.
| Replacement | Result | Use it when |
|---|---|---|
| `"No sales"` | Text | The cell is only read by people. |
| `""` | Looks blank, but is text | You want a clean report. |
| `0` | A real number | A zero average is truthful for your data. |
| `NA()` | `#N/A` | Charts should skip the point. |
A zero is often misleading. A region with no sales did not average 0. It has no average. Text also causes trouble if another formula, such as =H2*2, reads the cell, because that formula then returns #VALUE!.
A narrower alternative
You can check the count first with COUNTIFS. The average is only calculated when at least one row matches.
=IF(COUNTIFS(B2:B100, "South") = 0, "No sales", AVERAGEIFS(D2:D100, B2:B100, "South"))This formula is longer and repeats the criteria. Its advantage is that it handles only the "no match" case, so other errors still show up. Use it when you want to keep real mistakes visible.
Other causes of #DIV/0!
Sometimes matching rows exist, but the values being averaged are text. AVERAGEIFS ignores text in the average range, so an Amount column stored as text can also give #DIV/0!. If you expect matches, count them with COUNTIFS. If the count is above zero and the error persists, check that the Amount cells are real numbers.
Exercise
A summary cell uses =AVERAGEIFS(D2:D100, B2:B100, "North") and shows #DIV/0!, although North rows exist in the data. Name two likely causes and one way to check each.
Show answer
First, the Amount values in column D may be stored as text, so AVERAGEIFS ignores them. Check with ISNUMBER on a D cell, or look for left-aligned numbers. Second, the Region text may not match exactly, for example "North " with a trailing space. Check by running COUNTIFS(B2:B100, "North"). If it returns 0, the criteria are not matching any cell.