In this video, we'll look at a few ways to trap errors. Trapping errors can make your spreadsheets more professional by making them less cluttered and more friendly to use.
In this first worksheet, we have a simple table that shows test scores for 5 sections. The total questions in each section is in column C, and the total questions that were correct is in column D.
Column D contains a formula that calculates the percentage score in each section by dividing the correct answers by the total number of questions in the section.
Notice we get a divide by zero error in column E for the last 2 sections. That's because there is no data yet to work with in those sections.
If you want to trap and suppress this error, there are a few easy options.
Let's go over three options in Columns G H and I.
First, you could test for a blank value in column C using the IF function with with the ISBLANK function.
For the logical test use ISBLANK and C6 for the reference. For value if true, use empty double quotes, and for value if false use the original formula, D6/C6.
When I copy this formula down, we get the correct result for the first 3 sections and nothing is displayed for the next two sections.
This is a flexible way to trap errors since you are checking the source cells for any condition that you like. You can easily test for empty values, positive values, or values that match any other condition that makes sense.
Another way to trap this error is to use the IFERROR function which was introduced with Excel 2007.
IFERROR takes two arguments, value and value if error. For value, we supply the orignal formula, and for value if error, we use empty double quotes.
IF value is an error -- that is if our formula returns an error -- IFERROR will catch it and return the empty string. If there's no error, IFERROR will simply display the value...in this case, the result of the formula.
If you're using an older version of Excel you can also test for an error using the IF function and the ISERROR function, which will return TRUE with any error.
In this case, the logical test is the ISERROR with the original formula. THe value if true is empty double quotes, and the value if false is the original formula.
This works fine, but the structure is redundant and more error prone compared to IFERROR, so you'll want to use IFERROR if possible.
Finally, although we're using empty double quotes to display nothing when we detect an error condition, you could also display any message that you like. For example, you could display "not enough data" instead of an empty cell.
The IF function can perform a logical test and return one value for a TRUE result, and another for a FALSE result. For example, to "pass" scores above 70: =IF(A1>70,"Pass","Fail"). More than one condition can be tested by nesting IF functions....
The Excel IFERROR function returns a custom result when a formula generates an error, and a standard result when no error is detected. IFERROR is an elegant way to trap and manage errors without using more complicated nested IF statements.
The Excel ISERROR function returns TRUE for any error type excel generates, including #N/A, #VALUE!, #REF!, #DIV/0!, #NUM!, #NAME?, or #NULL! You can use ISERROR together with the IF function to test for errors and display a custom message, or...