Sub topic 5
Handling errors with IF, IFERROR and IFNA
This chapter assumes you can enter and edit formulas, use cell references and ranges such as A2 and A2:A10, and use SUM, AVERAGE and VLOOKUP. It also assumes you know that numbers and text are different kinds of values in a cell. Earlier chapters covered how to fix the cause of each error. This chapter covers what to do when an error is expected and you want the worksheet to handle it cleanly.
Excel has three tools for this. They differ in how much they catch, so choosing the right one decides whether your sheet stays trustworthy.
The three functions at a glance
| Function | Syntax | What it handles | Typical use |
|---|---|---|---|
| IF | `IF(test, value_if_true, value_if_false)` | Only the cases your test describes | Prevent a known error before it happens |
| IFERROR | `IFERROR(value, value_if_error)` | Every error type | Quick cleanup when any failure means "no result" |
| IFNA | `IFNA(value, value_if_na)` | Only `#N/A` | Lookups that may find nothing |
IFNA is available in Excel 2013 and later. IF and IFERROR work in all current versions.
IF: prevent the error by testing first
IF does not catch errors. It checks a condition and avoids the calculation that would fail.
=IF(B2=0,"",A2/B2)If B2 is zero, the formula returns an empty string. Otherwise it divides. A blank B2 also counts as zero in the comparison, so this formula covers blank divisors too.
This test has a gap. If B2 holds the text "n/a", then B2=0 is FALSE, and A2/B2 returns #VALUE!. To cover both problems, test that the divisor is a number and is not zero:
=IF(AND(ISNUMBER(B2),B2<>0),A2/B2,"")ISNUMBER returns FALSE for text and for blank cells, so only real, non-zero numbers reach the division.
IF gives you the most control, because you decide which conditions count as "safe". The cost is that you must anticipate each failure.
What the fallback value should be
The second result you return matters as much as the test. An empty string "" looks blank, but it is text. Any later arithmetic on that cell can fail:
=C2+1If C2 contains "", this returns #VALUE!. Functions such as SUM, AVERAGE and COUNT ignore text in cell references, so totals still work. Direct operators like + and * do not. You have three options:
- Return
""when the cell is for display only. - Return
0when a zero is a true and harmless answer. - Return
NA()when a chart should skip the point. Charts leave gaps for#N/Ainstead of plotting zero.
IFERROR: catch everything
IFERROR evaluates its first argument. If the result is any error, it returns the second argument. Otherwise it returns the result unchanged.
=IFERROR(AVERAGE(B2:B10),"No data")AVERAGE returns #DIV/0! when the range has no numbers, and the formula shows "No data" instead. IFERROR covers #N/A, #VALUE!, #REF!, #DIV/0!, #NUM!, #NAME?, #NULL! and #SPILL!.
That breadth is also its danger. Suppose you mistype the function name:
=IFERROR(AVRAGE(B2:B10),"No data")AVRAGE is not a function, so the inner formula returns #NAME?. IFERROR hides it, and the cell shows "No data" forever, even when B2:B10 is full of numbers. The cell now looks like a valid answer, and the green error triangle that would have warned you is gone.
IFNA: catch only missing matches
A lookup that finds nothing returns #N/A. That is often an expected result, such as an order ID not yet in the price list. IFNA replaces only that error:
=IFNA(VLOOKUP(E2,A2:B100,2,FALSE),"Not found")Compare it with the broken lookup from the #REF! chapter:
=IFNA(VLOOKUP(E2,A2:B100,3,FALSE),"Not found")The range A2:B100 has two columns, so column index 3 returns #REF!. IFNA lets that error through, so the cell shows #REF! and you know the formula itself is wrong. With IFERROR, the cell would say "Not found", and you would conclude that your data was missing.
This is why IFNA is the better default for lookups. It handles the one error that means "no match" and leaves design mistakes visible.
Choosing the right tool
A worked example
A sheet lists products in A2:A50 with unit prices in B2:B50. Column D holds order quantities and column E holds the product looked up from each order. You want revenue per order in F2 and an average price at the bottom.
- Look up the price and treat a missing product as a visible gap:
=IFNA(VLOOKUP(E2,$A$2:$B$50,2,FALSE),"Not found")- Compute revenue only when the lookup produced a number:
=IF(ISNUMBER(G2),G2*D2,"")Here G2 holds the price from step 1. The IF test avoids #VALUE! from multiplying the text "Not found".
- Summarize without failing on an empty column:
=IFERROR(AVERAGE(G2:G50),"No data")Each step uses the narrowest tool that fits. Step 1 uses IFNA because only a missing match is expected. Step 2 uses IF because you can name the condition. Step 3 uses IFERROR because an empty range is the only likely failure, and every failure there means the same thing.
Edge cases and gotchas
The fallback is not checked
IFERROR and IFNA return the fallback as-is. If the fallback is itself an error, the result is that error. The same applies to the value of an IF branch.
Nested lookups
IFNA can be chained to search a second table when the first search finds nothing:
=IFNA(VLOOKUP(E2,$A$2:$B$100,2,FALSE),IFNA(VLOOKUP(E2,$D$2:$E$100,2,FALSE),"Not found"))Each lookup runs only if the previous one returned #N/A.
Older IF(ISERROR()) patterns
You may see this in older workbooks:
=IF(ISERROR(VLOOKUP(E2,A2:B100,2,FALSE)),"Not found",VLOOKUP(E2,A2:B100,2,FALSE))It runs the lookup twice. IFERROR and IFNA run it once, so they are shorter and faster on large sheets. ISNA and ISERR remain useful when you need a TRUE or FALSE test rather than a replacement value. ISNA is TRUE only for #N/A. ISERR is TRUE for every error except #N/A.
Dynamic arrays
In Excel versions with dynamic arrays, IFERROR works element by element on an array result. Wrapping a spilling formula replaces only the failing items. It does not hide a #SPILL! caused by a blocked range, though, because that error describes the whole result. IFERROR will replace it with the fallback, so the blockage disappears from view. Check spilling formulas without the wrapper first.
Error Checking goes quiet
Excel's green-triangle indicator only flags problems it can see. After IFERROR converts an error into text, no triangle appears. Whatever protection the indicator gave you is gone, so you must test the formula yourself.
Practical rules
- Use IF when you can state the failing condition, and you want the cell to stay correct.
- Use IFNA for lookups.
- Use IFERROR for aggregates over possibly empty ranges, or when every error truly means "no result".
- Choose a fallback that downstream formulas can handle:
""for display,0when zero is true,NA()for charts. - Confirm the unwrapped formula works before adding any handler.
Exercise
Column B holds quantities, which may be zero, blank or text such as "n/a". Write a formula for C2 that returns A2/B2 only when B2 is a non-zero number, and returns an empty string otherwise. Why is =IF(B2=0,"",A2/B2) not enough?
Show answer
Use =IF(AND(ISNUMBER(B2),B2<>0),A2/B2,""). The simpler formula handles zero and blank, but when B2 holds "n/a" the test B2=0 is FALSE, so A2/B2 runs and returns #VALUE!. ISNUMBER rules out text and blanks before the division.
Exercise
A cell contains =IFERROR(VLOOKUP(E2,A2:B100,3,FALSE),"Not found") and always shows "Not found", even for IDs that are in the table. What is wrong, and what formula would reveal and fix the problem?
Show answer
The range A2:B100 has only two columns, so the column index 3 makes VLOOKUP return #REF!. IFERROR hides it behind "Not found". Using IFNA instead would let #REF! show, and changing the index to 2 fixes the formula: =IFNA(VLOOKUP(E2,A2:B100,2,FALSE),"Not found").