Sub topic 5
Handling errors with IF, IFERROR and IFNA
This chapter assumes you can enter formulas, use cell references and ranges, and know SUM, AVERAGE and VLOOKUP. It also assumes you know a number from text in a cell. You will learn three functions that control what a formula shows when it would otherwise return an error.
Why handle errors at all
Some errors are expected. A lookup may not find a key. A division may meet an empty cell because data has not been entered yet. In a report, a column of #DIV/0! is noisy, and it also breaks any formula that reads those cells, because most functions pass an error along.
Excel gives you three tools. Each one catches a different set of errors.
| Function | Catches | Use it when |
|---|---|---|
| IF | Whatever condition you test | You can predict the bad input and check it first |
| IFERROR | Every error type | You accept any failure in this formula and want one fallback |
| IFNA | Only `#N/A` | A missing lookup match is expected, and other errors must stay visible |
IF: prevent the error
IF tests a condition before the risky calculation runs. The formula never produces the error, so there is nothing to hide.
=IF(B2=0,"",A2/B2)If B2 is zero or blank, the formula returns an empty string. Otherwise it divides. The same idea works for #NUM!, where a square root of a negative number fails:
=IF(A2>=0,SQRT(A2),"")IF only guards against the problem you thought of. If A2 holds text, the first formula still returns #VALUE!.
IFERROR: catch any error
IFERROR takes a value and a fallback. If the value is an error of any kind, you get the fallback.
=IFERROR(AVERAGE(B2:B10),"No data")AVERAGE returns #DIV/0! when the range has no numbers. IFERROR replaces that with a readable message.
The strength of IFERROR is also its risk. It catches #REF!, #NAME? and #VALUE! too. A misspelled function name would show "No data" and look like normal output.
=IFERROR(SUMM(A1:A5),0)This formula returns 0 forever. SUMM is not a function, so the real result is #NAME?, and the 0 hides it.
IFNA: catch only a missing match
IFNA replaces #N/A and nothing else. A lookup that finds no match returns #N/A, so this is the natural fit.
=IFNA(VLOOKUP(E2,A2:B100,2,FALSE),"Not found")If you later delete a column and the lookup returns #REF!, that error still shows. You are told about a real break instead of seeing "Not found".
Common mistakes
- Wrapping everything in IFERROR. Add it last, after the formula works, and only where an error is expected.
- Using "" as the fallback in numeric columns. An empty string is text. Later arithmetic such as
=C2*2then returns#VALUE!. Use 0, or a fallback your downstream formulas can handle. - Treating a blank as zero.
=IF(B2=0,"",A2/B2)also hides a true zero. If zero is a valid divisor you want to report, testB2=""instead. - Using IFERROR on a lookup. IFNA is safer because it leaves other errors visible.
Exercise
A sheet looks up prices with =VLOOKUP(E2,A2:B100,2,FALSE). Missing products should show "Not found", but you want to notice if someone deletes column B. Which function do you wrap around the VLOOKUP, and why?
Show answer
Use IFNA: =IFNA(VLOOKUP(E2,A2:B100,2,FALSE),"Not found"). It replaces only #N/A, which is what a missing product returns. Deleting column B produces #REF!, which IFNA leaves visible. IFERROR would replace both and hide the break.
Exercise
What does =IFERROR(AVERAGE(B2:B10),"No data") return if B2:B10 contains only text, and why might this be a problem in a numeric column?
Show answer
It returns "No data", because AVERAGE finds no numbers and returns #DIV/0!. The result is text, so a formula such as =C2*2 that uses this cell would return #VALUE!. A numeric fallback such as 0 avoids that, if 0 is meaningful for your report.