IN THIS CHAPTER
It goes without saying that you want your Excel worksheets to produce accurate results. Unfortunately, it's not always easy to be certain that the results are correct, especially if you deal with large, complex worksheets. This chapter introduces the tools and techniques available to help identify, correct, and prevent errors.
Making a change in a worksheet — even a relatively minor change — may produce a ripple effect that introduces errors in other cells. For example, accidentally entering a value into a cell that previously held a formula is all too easy to do. This simple error can have a major impact on other formulas, and you may not discover the problem until long after you make the change — if you discover the problem at all.
Formula errors tend to fall into one of the following general categories:
Sum
formula may not include all the data that you want to sum.#NAME?
error.Syntax errors are usually the easiest to identify and correct. In most cases, you'll know when your formula contains a syntax error. For example, Excel won't permit you to enter a formula with mismatched parentheses. Other syntax errors also usually result in an error display in the cell.
The following sections describe common formula problems and offer advice on identifying and correcting them.
In a formula, every left parenthesis must have a corresponding right parenthesis. If your formula has mismatched parentheses, Excel usually won't permit you to enter it. An exception to this rule involves a simple formula that uses a function. For example, if you enter the following formula (which is missing a closing parenthesis), Excel accepts the formula and provides the missing parenthesis:
=SUM(A1:A500
A formula may have an equal number of left and right parentheses, but the parentheses may not match properly. For example, consider the following formula, which converts a text string such that the first character is uppercase and the remaining characters are lowercase. This formula has five pairs of parentheses, and they match properly:
=UPPER(LEFT(A1))&RIGHT(LOWER(A1),LEN(A1)-1)
The following formula also has five pairs of parentheses, but they're mismatched. The result displays a syntactically correct formula that simply returns the wrong result:
=UPPER(LEFT(A1)&RIGHT(LOWER(A1),LEN(A1)-1))
Often, parentheses that are in the wrong location will result in a syntax error, which is usually a message that tells you that you entered too many or too few arguments for a function.
A cell is filled with a series of hash marks (#) for one of two reasons:
Some Excel users have discovered that by pressing the spacebar, the contents of a cell seem to erase. Actually, pressing the spacebar inserts an invisible space character, which isn't the same as erasing the cell.
For example, the following formula returns the number of nonempty cells in range A1:A10. If you “erase” any of these cells by using the spacebar, these cells are included in the count, and the formula returns an incorrect result:
=COUNTA(A1:A10)
If your formula doesn't ignore blank cells the way that it should, check to make sure that the blank cells are really blank. Here's how to search for cells that contain only blank characters:
If you have formulas or use procedures that rely on comparing text, be careful that your text doesn't contain additional space characters. Adding an extra space character is particularly common when data has been imported from another source.
Excel automatically removes trailing spaces from values that you enter, but trailing spaces in text entries are not deleted. It's impossible to tell just by looking at a cell whether it contains one or more trailing space characters.
The TRIM
function removes leading spaces, trailing spaces, and multiple spaces within a text string. Figure 31.1 shows some text in column A. The formula in B1, which was copied down the column, is
=TRIM(A1)=A1
This formula returns FALSE
if the text in column A contains leading spaces, trailing spaces, or multiple spaces. In this case, the words Penguin and Chicken each contain a trailing space.
A formula may return any of the following error values:
#DIV/0!
#N/A
#NAME?
#NULL!
#NUM!
#REF!
#VALUE!
The following sections summarize possible problems that may cause these errors.
Division by zero is not a valid operation. If you create a formula that attempts to divide by zero, Excel displays its familiar #DIV/0!
error value.
Because Excel considers a blank cell to be zero, you also get this error if your formula divides by a missing value. This problem is common when you create formulas for data that you haven't entered yet, as shown in Figure 31.2. The formula in cell D4, which was copied to the cells below it, is
=C4/B4
This formula calculates the ratio of the values in columns C and B. Data isn't available for all days, so the formula returns a #DIV/0!
error.
To avoid the error display, you can use an IF
function to check for a blank cell in column B:
=IF(B4=0,"",C4/B4)
This formula displays an empty string if cell B4 is blank or contains 0
; otherwise, it displays the calculated value.
Another approach is to use an IFERROR
function to check for any error condition. The following formula, for example, displays an empty string if the formula results in any type of error:
=IFERROR(C4/B4,"")
The #N/A
error occurs if any cell referenced by a formula displays #N/A
.
The #N/A
error also occurs when a LOOKUP
function (HLOOKUP
, LOOKUP
, MATCH
, or VLOOKUP
) can't find a match.
If you would like to display an empty string instead of #N/A
, use the IFNA
function in a formula like this:
=IFNA(VLOOKUP(A1,C1:F50,4,FALSE),"")
The #NAME?
error occurs under these conditions:
#NAME?
error.A #NULL!
error occurs when a formula attempts to use an intersection of two ranges that don't actually intersect. Excel's intersection operator is a space. The following formula, for example, returns #NULL!
because the two ranges don't intersect:
=SUM(B5:B14 A16:F16)
The following formula doesn't return #NULL!
but displays the contents of cell B9, which represents the intersection of the two ranges:
=SUM(B5:B14 A9:F9)
You also see a #NULL!
error if you accidentally omit an operator in a formula. For example, this formula is missing the second operator:
= A1+A2 A3
A formula returns a #NUM!
error if any of the following occurs:
#NUM!
:
=SQRT(-12)
IRR
and RATE
.A #REF!
error occurs when a formula uses an invalid cell reference. This error can occur in the following situations:
#REF!
error if row 1, column A, or column B is deleted:
=A1/B1
#REF!
error if Sheet2 is deleted:
=Sheet2!A1
#REF!
because it attempts to refer to a nonexistent cell:
=A1-1
#REF!
.A #VALUE!
error is common and can occur under the following conditions:
#VALUE!
error.Array
formula.As I describe in Chapter 10, “Introducing Formulas and Functions,” a cell reference can be relative (for example, A1
), absolute (for example, $A$1
), or mixed (for example, $A1
or A$1
). The type of cell reference that you use in a formula is relevant only if the formula will be copied to other cells.
A common problem is using a relative reference when you should use an absolute reference. As shown in Figure 31.3, cell C1 contains a tax rate, which is used in the formulas in column C. The formula in cell C4 is
=B4+(B4*$C$1)
Notice that the reference to cell C1 is an absolute reference. When the formula is copied to other cells in column C, the formula continues to refer to cell C1. If the reference to cell C1 were a relative reference, the copied formulas would return an incorrect result.
As I note in Chapter 10, Excel has some straightforward rules about the order in which mathematical operations are performed. When in doubt (or when you simply need to clarify your intentions), you should use parentheses to ensure that operations are performed in the correct order. For example, the following formula multiplies A1 by A2 and then adds 1 to the result. The multiplication is performed first because it has a higher order of precedence:
=1+A1*A2
The following is a clearer version of this formula. The parentheses aren't necessary, but in this case, the order of operations is perfectly obvious:
=1+(A1*A2)
Notice that the negation operator symbol is the same as the subtraction operator symbol. This, as you may expect, can cause some confusion. Consider these two formulas:
=-3^2
=0-3^2
The first formula, as expected, returns 9
. The second formula, however, returns –9
. Squaring a number always produces a positive result, so how is it that Excel can return the –9
result?
In the first formula, the minus sign is a negation operator and has the highest precedence. However, in the second formula, the minus sign is a subtraction operator, which has a lower precedence than the exponentiation operator. Therefore, the value 3
is squared, and then the result is subtracted from 0
(zero), which produces a negative result.
Using parentheses, as shown in the following formula, causes Excel to interpret the operator as a minus sign rather than a negation operator. This formula returns –9
:
=-(3^2)
If you use custom worksheet functions written in VBA, you may find that formulas that use these functions fail to be recalculated and may display incorrect results. For example, assume that you wrote a VBA function that returns the number format of a referenced cell. If you change the number format, the function will continue to display the previous number format. That's because changing a number format doesn't trigger a recalculation.
To force a single formula to be recalculated, select the cell, press F2, and then press Enter. To force a recalculation of all formulas, press Ctrl+Alt+F9.
You may encounter a situation in which values in a range don't appear to add up properly. For example, Figure 31.4 shows a worksheet with the following formula entered into each cell in the range B2:B4:
=1/3
Cell B5 contains the following formula:
=SUM(B2:B4)
All the cells are formatted to display with two decimal places. As you can see, the formula in cell B5 appears to display an incorrect result. (You may expect it to display 0.99
.) The formula, of course, does return the correct result. The formula uses the actual values in the range B2:B4, not the displayed values.
You can instruct Excel to use the displayed values by selecting the Set Precision as Displayed check box of the Advanced section of the Excel Options dialog box. (Choose File Options to display this dialog box.)
Computers, by their nature, don't have infinite precision. Excel stores numbers in binary format by using 8 bytes, which can handle numbers with 15-digit accuracy. Some numbers can't be expressed precisely by using 8 bytes, so the number is stored as an approximation.
To demonstrate how this lack of precision may cause problems, enter the following formula into cell A1:
=(5.1-5.2)+1
The result should be 0.9
. However, if you format the cell to display 15 decimal places, you discover that Excel calculates the formula with a result of 0.899999999999999
. This result occurs because the operation in parentheses is performed first, and this intermediate result stores in binary format by using an approximation. The formula then adds 1 to this value, and the approximation error is propagated to the final result.
In many cases, this type of error doesn't present a problem. However, if you need to test the result of that formula by using a logical operator, it may present a problem. For example, the following formula (which assumes that the previous formula is in cell A1) returns FALSE
:
=A1=.9
One solution to this type of error is to use the ROUND
function. The following formula, for example, returns TRUE
because the comparison is made by using the value in A1 rounded to one decimal place:
=ROUND(A1,1)=0.9
Here's another example of a “precision” problem. Try entering the following formula:
=(1.333-1.233)-(1.334-1.234)
This formula should return 0
, but it actually returns –2.220446E-16
(a number very close to zero).
If that formula is in cell A1, the following formula returns Not Zero
:
=IF(A1=0,"Zero","Not Zero")
One way to handle these “very close to zero” rounding errors is to use a formula like this:
=IF(ABS(A1)<1E-6,"Zero","Not Zero")
This formula uses the less-than operator (<
) to compare the absolute value of the number with a very small number. This formula returns Zero
.
You may open a workbook and see a message like the one shown in Figure 31.5. This message sometimes appears even when a workbook contains no linked formulas. Often, these phantom links are created when you copy a worksheet that contains names.
First, try choosing File Info Edit Links to Files to display the Edit Links dialog box. Then select each link and click Break Link. If that doesn't solve the problem, this phantom link may be caused by an erroneous name. Choose Formulas Defined Names Name Manager and scroll through the list of names in the Name Manager dialog box. If you see a name that refers to #REF!
, delete the name. The Name Manager dialog box has a Filter button that lets you filter the names. For example, you can filter the lists to display only the names with errors.
Excel includes a number of tools that can help you track down formula errors. This section describes the auditing tools built in to Excel.
The Go to Special dialog box (shown in Figure 31.6) is a handy tool that enables you to locate cells of a particular type. To display this dialog box, choose Home Editing Find & Select Go to Special.
You can use the Go to Special dialog box to select cells of a certain type, which can often help you identify errors. For example, if you choose the Formulas option, Excel selects all the cells that contain a formula. If you zoom the worksheet out to a small size, you can get a good idea of the worksheet's organization (see Figure 31.7). To zoom a worksheet, use the zoom controls on the right side of the status bar or press Ctrl while you move the scroll wheel on your mouse.
You can become familiar with an unfamiliar workbook by displaying the formulas rather than the results of the formulas. To toggle the display of formulas, choose Formulas Formula Auditing Show Formulas. You may want to create a second window for the workbook before issuing this command. This way, you can see the formulas in one window and the results of the formula in the other window. Choose View Window New Window to open a new window.
Figure 31.8 shows an example of a worksheet displayed in two windows. The window on the top shows Normal view (formula results), and the window on the bottom displays the formulas. Choosing View Window View Side by Side, which allows synchronized scrolling, is also useful for viewing two windows.
To understand how to trace cell relationships, you need to familiarize yourself with the following two concepts:
For example, consider this simple formula entered into cell A4:
=SUM(A1:A3)
Cell A4 has three precedent cells (A1, A2, and A3), which are all direct precedents. Cells A1, A2, and A3 all have at least one dependent cell (cell A4).
Identifying cell precedents for a formula cell often sheds light on why the formula isn't working correctly. Conversely, knowing which formula cells depend on a particular cell is also helpful. For example, if you're about to delete a formula, you may want to check whether it has any dependents.
You can identify cells used by a formula in the active cell in a number of ways:
You can identify formula cells that use a particular cell in a number of ways:
If a formula displays an error value, Excel can help you identify the cell that is causing that error value. An error in one cell is often the result of an error in a precedent cell. Activate a cell that contains an error value and then choose Formulas Formula Auditing Error Checking Trace Error. Excel draws arrows to indicate the error source.
If you accidentally create a circular reference formula, Excel displays a warning message — Circular Reference
— with the cell address, in the status bar. It also draws arrows on the worksheet to help you identify the problem. If you can't figure out the source of the problem, choose Formulas Formula Auditing Error Checking Circular References. This command displays a list of all cells that are involved in the circular references. Start by selecting the first cell listed and then work your way down the list until you figure out the problem.
Some people may find it helpful to take advantage of the Excel automatic error-checking feature. This feature is enabled or disabled via the Enable Background Error Checking check box, found on the Formulas tab of the Excel Options dialog box, shown in Figure 31.10. In addition, you can use the check boxes in the Error Checking Rules section to specify which types of errors to check.
When error checking is turned on, Excel continually evaluates the formulas in your worksheet. If a potential error is identified, Excel places a small triangle in the upper-left corner of the cell. When the cell is activated, a drop-down control appears. Clicking this drop-down control provides you with options. Figure 31.11 shows the options that appear when you click the drop-down control in a cell that contains a #DIV/0!
error. The options vary, depending on the type of error.
In many cases, you'll choose to ignore an error by selecting the Ignore Error option. Selecting this option eliminates the cell from subsequent error checks. However, all previously ignored errors can be reset so that they appear again. (Use the Reset Ignored Errors button on the Formulas tab of the Excel Options dialog box.)
You can choose Formulas Formula Auditing Error Checking to display a dialog box that describes each potential error cell in sequence, much like using a spell-checking command. This command is available even if you disable background error checking. Figure 31.12 shows the Error Checking dialog box. This dialog box is modeless; that is, you can still access your worksheet when the Error Checking dialog box is displayed.
Formula Evaluator lets you see the various parts of a nested formula evaluated in the order in which the formula is calculated. To use Formula Evaluator, select the cell that contains the formula and then choose Formula Formula Auditing Evaluate Formula to display the Evaluate Formula dialog box (see Figure 31.13).
Click the Evaluate button to show the result of calculating the expressions within the formula. Each click of the button performs another calculation. This feature may seem a bit complicated at first, but if you spend some time working with it, you'll understand how it works and see the value.
Excel provides another way to evaluate a part of a formula:
The highlighted portion of the formula displays the calculated result. You can evaluate other parts of the formula or press Esc to cancel and return your formula to its previous state.
Excel has a powerful search-and-replace feature that makes it easy to locate information in a worksheet or across multiple worksheets in a workbook. As an option, you can also search for text and replace it with other text.
To access the Find and Replace dialog box, start by selecting the range that you want to search. If you select any single cell, Excel searches the entire sheet. Choose Home Editing Find & Select Find (or press Ctrl+F). You'll see the Find and Replace dialog box, shown in Figure 31.14. If you're simply looking for information in the worksheet, select the Find tab. If you want to replace existing text with new text, use the Replace tab. Also note that you can use the Options button to display (or hide) additional options. The dialog box shown in the figure displays these additional options.
Enter the information to search for in the Find What text box and then specify any of the following options:
Click Find Next to locate the matching cells one at a time or click Find All to locate all matches. If you use the Find All button, the Find and Replace dialog box expands to display the addresses of all matching cells in a list (see Figure 31.15). When you select an entry in this list, Excel scrolls the worksheet so that you can view it in context.
To replace text with other text, use the Replace tab in the Find and Replace dialog box. Enter the text to be replaced in the Find What field, and then enter the new text in the Replace With field. Specify other options as described in the previous section.
Click Find Next to locate the first matching item and then click Replace to do the replacement. When you click the Replace button, Excel locates the next matching item. To override the replacement, click Find Next. To replace all items without verification, click Replace All. If the replacement didn't occur as you planned, you can use the Undo button on the Quick Access toolbar (or press Ctrl+Z).
From the Find and Replace dialog box, you can also locate cells that contain a particular type of formatting. As an option, you can replace that formatting with another type of formatting. For example, assume that you want to locate all cells that are formatted as bold and then change that formatting to bold and italic. Follow these steps:
You can also find formatting based on a particular cell. In the Find Format dialog box, click the Choose Format from Cell button and then click the cell that contains the formatting you're looking for.
If you use a word-processing program, you probably take advantage of its spell-checker feature. Spelling mistakes can be just as embarrassing when they appear in a spreadsheet. Fortunately, Microsoft includes a spell checker with Excel.
To access the spell checker, choose Review Proofing Spelling, or press F7. To check the spelling in just a particular range, select the range before you activate the spell checker.
If the spell checker finds any words it doesn't recognize as correct, the Spelling dialog box, shown in Figure 31.17, appears.
The Spelling dialog box works similarly to other spell checkers with which you may be familiar. If Excel encounters a word that isn't in the current dictionary or that is misspelled, it offers a list of suggestions. You can respond by clicking one of these buttons:
AutoCorrect is a handy feature that automatically corrects common typing mistakes. You can also add words to the list that Excel corrects automatically. The AutoCorrect dialog box appears in Figure 31.18. To access this feature, choose File Options. In the Excel Options dialog box, select the Proofing tab and then click the AutoCorrect Options button.
This dialog box has several options:
Excel includes a long list of AutoCorrect entries for commonly misspelled words. In addition, it has AutoCorrect entries for some symbols. For example, (c) is replaced with ©, and (r) is replaced with®. You can also add your own AutoCorrect entries. For example, if you find that you frequently misspell the word January as Janruary, you can create an AutoCorrect entry so that it's changed automatically. To create a new AutoCorrect entry, enter the misspelled word in the Replace box and the correctly spelled word in the With field. You can also delete entries that you no longer need.
You can use the AutoFormat as You Type tab of the AutoCorrect dialog box to control a few other automatic settings in Excel.
The Actions tab enables what were formerly known as Smart Tags for certain types of data in your worksheets. The types of actions Excel recognizes vary depending on the types of software that are installed on your system. For example, if you enable the Financial Symbol action, you can right-click a cell that contains a financial symbol (such as MSFT, for Microsoft), choose Additional Cell Actions, and you'll be presented with a list of options. For example, you can insert a refreshable stock price in your worksheet.