Sub topic 8
How Replacement Values Affect Totals and Other Formulas
What this chapter assumes
You can already write IFERROR and IFNA formulas and copy them down a column. You know how SUM, AVERAGE and COUNT work on a range. You also know the difference between an empty cell, the number 0, and text.
This chapter covers what happens after the handler returns its replacement value. The replacement sits in a cell, and other formulas read that cell. Each choice (0, an empty string, a message, or an error on purpose) changes those formulas' results in a different way.
The same error with four replacements
Start with a small unit-price calculation. Column B holds revenue, column C holds units, and column D divides one by the other. The second row has zero units, so the division fails.
| Row | B: Revenue | C: Units | D: Unit price (raw) |
|---|---|---|---|
| 2 | 100 | 10 | 10 |
| 3 | 200 | 0 | #DIV/0! |
| 4 | 300 | 20 | 15 |
| 5 | 400 | 8 | 50 |
Now wrap D2:D5 with four different handlers and look at what common summary formulas return.
| Formula in D3 | What D3 holds | `=SUM(D2:D5)` | `=AVERAGE(D2:D5)` | `=COUNT(D2:D5)` | `=MIN(D2:D5)` |
|---|---|---|---|---|---|
| `=IFERROR(B3/C3, 0)` | the number 0 | 75 | 18.75 | 4 | 0 |
| `=IFERROR(B3/C3, "")` | empty text | 75 | 25 | 3 | 10 |
| `=IFERROR(B3/C3, "n/a")` | text | 75 | 25 | 3 | 10 |
| `=B3/C3` (no handler) | #DIV/0! | #DIV/0! | #DIV/0! | 3 | #DIV/0! |
The SUM is the same in the first three rows. AVERAGE, COUNT and MIN differ. A total can look correct while the statistics next to it are wrong.
The rest of this chapter explains each difference.
Why SUM hides the difference
When SUM, AVERAGE, MIN, MAX and COUNT read a range, they use only the numbers in it. They skip text, including the empty string "", and they skip empty cells. A zero is a number, so they use it.
Adding 0 to a sum changes nothing, and skipping a cell changes nothing. That is why the three SUM results match. The outcomes split when a function depends on how many values it counted.
Errors behave differently. A single error in the range makes SUM, AVERAGE, MIN and MAX return that error. COUNT is the exception because it counts numbers and skips everything else, including errors.
Averages and counts
AVERAGE divides the sum by the number of numeric cells.
- With 0 in D3, the average is 75 / 4 = 18.75. The failed row counts as a real price of zero and drags the average down.
- With
""or a message, the average is 75 / 3 = 25. The failed row is left out.
Neither is automatically right. Ask what the failed row means. A product with no units sold has no unit price, so excluding it (25) is usually the honest answer. A missing sales figure that really means "we sold nothing" might correctly be 0.
The counting functions split the same way:
=COUNT(D2:D5)
=COUNTA(D2:D5)
=COUNTBLANK(D2:D5)With "" in D3, these return 3, 4 and 1.
COUNTcounts only numbers, so it returns 3.COUNTAcounts every non-empty cell. A formula that returns""is not empty, so it returns 4.COUNTBLANKcounts empty cells and also cells holding"", so it returns 1.
With 0 in D3, COUNT is 4, COUNTA is 4 and COUNTBLANK is 0. A report that says "3 products have a price" next to "4 rows" is a sign that "" is hiding a row.
Arithmetic on the handled cell
Totals and averages are forgiving about text. The arithmetic operators +, -, * and / are not. If a formula uses the cell directly, text in it causes a new error.
Say column E multiplies the unit price back by units to check the revenue:
=D3*C3- If D3 is 0, the result is 0. No error.
- If D3 is
""or"n/a", the result is #VALUE!.
You replaced one error with a text value, and a downstream formula turned it into another error. Often the second error appears far from the cause, so it is harder to trace than the first.
The same applies to comparisons and to functions that take number arguments, such as ROUND:
=ROUND(D3, 2)This also returns #VALUE! when D3 holds text.
Here are three ways to protect the downstream formula.
- Use a numeric replacement (0) when a zero is a valid meaning for the failure.
- Handle the empty case in the downstream formula, for example
=IF(D3="", "", D3*C3). - Use a function that ignores text.
SUMPRODUCTwith comma-separated arrays treats text as zero, whileSUMPRODUCT(D2:D5*C2:C5)with a multiplication fails on text:
=SUMPRODUCT(D2:D5, C2:C5)The comma form returns the sum of products and skips the "" row. The * form returns #VALUE!.
Comparisons and blank tests
A cell holding "" is not an empty cell, but a few tests treat them alike. These results are for a cell A1 that holds the formula ="", compared with a truly empty cell:
| Test | A1 holds `""` | A1 is truly empty |
|---|---|---|
| `=A1=""` | TRUE | TRUE |
| `=ISBLANK(A1)` | FALSE | TRUE |
| `=A1=0` | FALSE | TRUE |
| `=ISNUMBER(A1)` | FALSE | FALSE |
| `=LEN(A1)` | 0 | 0 |
If other formulas test for blanks with ISBLANK, a handler that returns "" will not count as blank. If they test =A1="", it will. Pick the test deliberately, and write =A1="" when you want both cases covered.
Sorting and filtering are also affected. In ascending order, numbers sort before text. A 0 therefore appears among the smallest values, while a "n/a" message goes to the bottom with the other text. Filters list 0 and "" as separate entries, which can surprise people who expect one "blank" group.
Replacement values in charts
Charts read cells differently from sums.
- A 0 is plotted as a point at zero. On a line chart, the line dives to the axis.
- Text, including
"", is also plotted as zero in most chart types. A blank-looking cell can still produce a dip. - An #N/A error is skipped. The line either bridges the gap or leaves a break, depending on the chart's setting for hidden and empty cells.
This is the one case where deliberately returning an error is useful. If a chart must show a gap, return NA():
=IFERROR(B3/C3, NA())The chart skips the point. The cost is that every SUM and AVERAGE pointing at that range now returns #N/A. When you need both a clean chart and clean totals, keep two columns: one with NA() for the chart, and one with 0 or "" for the calculations. Alternatively, use AGGREGATE, which can ignore errors:
=AGGREGATE(9, 6, D2:D5)Function number 9 is SUM, and option 6 means "ignore error values". The result is 75 even when D3 is #N/A.
How the choice flows through a workbook
A practical decision guide
Use these questions in order.
- Is a zero a true statement about this row? Examples are "no sales" and "no stock". Return 0. Downstream arithmetic works and the total is correct.
- Should the row be excluded from averages and counts? Examples are an unknown price and an unmatched customer. Return
""or a message, and guard any downstream arithmetic. - Will a person read this cell? A message such as
"Not found"explains the gap better than a blank. Keep it out of columns that feed arithmetic. - Is the value only for a chart? Return
NA()in a separate chart column.
Whatever you choose, apply it consistently across a column. A column mixing 0 for some failures and "" for others gives averages that depend on which failure happened.
Guarding the dependent formula
When the handled column must feed more calculations, make the dependent formula state its own rule instead of trusting the replacement value. For a revenue check:
=IF(ISNUMBER(D3), D3*C3, 0)This returns the product when D3 is a number, and 0 for any text. It does not depend on which replacement you chose. For an average that must ignore failed rows no matter how they were marked:
=AVERAGEIF(D2:D5, ">0")AVERAGEIF ignores text and empty cells in the range, and the ">0" criterion also excludes zeros. Use "<>0" instead if negative prices are valid. The result for this example is 25 with either a 0 or a "" in D3, so the formula holds up if someone changes the handler later.
Summary of behaviors
| Replacement | SUM | AVERAGE | COUNT | `+ - * /` | Chart |
|---|---|---|---|---|---|
| 0 | included, no effect | included, lowers it | counted | works | point at zero |
| `""` | skipped | skipped | not counted | #VALUE! | point at zero |
| Message text | skipped | skipped | not counted | #VALUE! | point at zero |
| `NA()` | #N/A | #N/A | not counted | #N/A | skipped |
Exercise
Cells D2:D5 hold 10, the result of =IFERROR(B3/C3, "") (which fails), 15, and 50. In another cell, =D3*C3 returns #VALUE!. Explain why, and give one formula for E3 that returns 0 for the failed row and D3*C3 otherwise.
Show answer
D3 holds the empty string, which is text, and the * operator cannot multiply text, so it returns #VALUE!. Use =IF(ISNUMBER(D3), D3*C3, 0). It multiplies only when D3 is a number and returns 0 for any text.
Exercise
A column of unit prices has one failed row. With the handler returning 0, =AVERAGE(D2:D5) gives 18.75. Which handler change gives 25, and which formula gives 25 without changing the handler?
Show answer
Change the replacement to "" (or a message). AVERAGE skips text, so it divides 75 by 3. Without changing the handler, use =AVERAGEIF(D2:D5, ">0"), which excludes the zero.
Exercise
You want a line chart to show a gap for a failed row, and you also need =SUM(D2:D5) to return a number. How can you do both?
Show answer
Keep the chart column separate and give it =IFERROR(B3/C3, NA()) so the chart skips the point. Point SUM at a second column that uses 0 or "". Alternatively, keep a single column with NA() and use =AGGREGATE(9, 6, D2:D5) for the total, which ignores errors.