- •About the Author
- •About the Technical Editor
- •Credits
- •Is This Book for You?
- •Software Versions
- •Conventions This Book Uses
- •What the Icons Mean
- •How This Book Is Organized
- •How to Use This Book
- •What’s on the Companion CD
- •What Is Excel Good For?
- •What’s New in Excel 2010?
- •Moving around a Worksheet
- •Introducing the Ribbon
- •Using Shortcut Menus
- •Customizing Your Quick Access Toolbar
- •Working with Dialog Boxes
- •Using the Task Pane
- •Creating Your First Excel Worksheet
- •Entering Text and Values into Your Worksheets
- •Entering Dates and Times into Your Worksheets
- •Modifying Cell Contents
- •Applying Number Formatting
- •Controlling the Worksheet View
- •Working with Rows and Columns
- •Understanding Cells and Ranges
- •Copying or Moving Ranges
- •Using Names to Work with Ranges
- •Adding Comments to Cells
- •What Is a Table?
- •Creating a Table
- •Changing the Look of a Table
- •Working with Tables
- •Getting to Know the Formatting Tools
- •Changing Text Alignment
- •Using Colors and Shading
- •Adding Borders and Lines
- •Adding a Background Image to a Worksheet
- •Using Named Styles for Easier Formatting
- •Understanding Document Themes
- •Creating a New Workbook
- •Opening an Existing Workbook
- •Saving a Workbook
- •Using AutoRecover
- •Specifying a Password
- •Organizing Your Files
- •Other Workbook Info Options
- •Closing Workbooks
- •Safeguarding Your Work
- •Excel File Compatibility
- •Exploring Excel Templates
- •Understanding Custom Excel Templates
- •Printing with One Click
- •Changing Your Page View
- •Adjusting Common Page Setup Settings
- •Adding a Header or Footer to Your Reports
- •Copying Page Setup Settings across Sheets
- •Preventing Certain Cells from Being Printed
- •Preventing Objects from Being Printed
- •Creating Custom Views of Your Worksheet
- •Understanding Formula Basics
- •Entering Formulas into Your Worksheets
- •Editing Formulas
- •Using Cell References in Formulas
- •Using Formulas in Tables
- •Correcting Common Formula Errors
- •Using Advanced Naming Techniques
- •Tips for Working with Formulas
- •A Few Words about Text
- •Text Functions
- •Advanced Text Formulas
- •Date-Related Worksheet Functions
- •Time-Related Functions
- •Basic Counting Formulas
- •Advanced Counting Formulas
- •Summing Formulas
- •Conditional Sums Using a Single Criterion
- •Conditional Sums Using Multiple Criteria
- •Introducing Lookup Formulas
- •Functions Relevant to Lookups
- •Basic Lookup Formulas
- •Specialized Lookup Formulas
- •The Time Value of Money
- •Loan Calculations
- •Investment Calculations
- •Depreciation Calculations
- •Understanding Array Formulas
- •Understanding the Dimensions of an Array
- •Naming Array Constants
- •Working with Array Formulas
- •Using Multicell Array Formulas
- •Using Single-Cell Array Formulas
- •Working with Multicell Array Formulas
- •What Is a Chart?
- •Understanding How Excel Handles Charts
- •Creating a Chart
- •Working with Charts
- •Understanding Chart Types
- •Learning More
- •Selecting Chart Elements
- •User Interface Choices for Modifying Chart Elements
- •Modifying the Chart Area
- •Modifying the Plot Area
- •Working with Chart Titles
- •Working with a Legend
- •Working with Gridlines
- •Modifying the Axes
- •Working with Data Series
- •Creating Chart Templates
- •Learning Some Chart-Making Tricks
- •About Conditional Formatting
- •Specifying Conditional Formatting
- •Conditional Formats That Use Graphics
- •Creating Formula-Based Rules
- •Working with Conditional Formats
- •Sparkline Types
- •Creating Sparklines
- •Customizing Sparklines
- •Specifying a Date Axis
- •Auto-Updating Sparklines
- •Displaying a Sparkline for a Dynamic Range
- •Using Shapes
- •Using SmartArt
- •Using WordArt
- •Working with Other Graphic Types
- •Using the Equation Editor
- •Customizing the Ribbon
- •About Number Formatting
- •Creating a Custom Number Format
- •Custom Number Format Examples
- •About Data Validation
- •Specifying Validation Criteria
- •Types of Validation Criteria You Can Apply
- •Creating a Drop-Down List
- •Using Formulas for Data Validation Rules
- •Understanding Cell References
- •Data Validation Formula Examples
- •Introducing Worksheet Outlines
- •Creating an Outline
- •Working with Outlines
- •Linking Workbooks
- •Creating External Reference Formulas
- •Working with External Reference Formulas
- •Consolidating Worksheets
- •Understanding the Different Web Formats
- •Opening an HTML File
- •Working with Hyperlinks
- •Using Web Queries
- •Other Internet-Related Features
- •Copying and Pasting
- •Copying from Excel to Word
- •Embedding Objects in a Worksheet
- •Using Excel on a Network
- •Understanding File Reservations
- •Sharing Workbooks
- •Tracking Workbook Changes
- •Types of Protection
- •Protecting a Worksheet
- •Protecting a Workbook
- •VB Project Protection
- •Related Topics
- •Using Excel Auditing Tools
- •Searching and Replacing
- •Spell Checking Your Worksheets
- •Using AutoCorrect
- •Understanding External Database Files
- •Importing Access Tables
- •Retrieving Data with Query: An Example
- •Working with Data Returned by Query
- •Using Query without the Wizard
- •Learning More about Query
- •About Pivot Tables
- •Creating a Pivot Table
- •More Pivot Table Examples
- •Learning More
- •Working with Non-Numeric Data
- •Grouping Pivot Table Items
- •Creating a Frequency Distribution
- •Filtering Pivot Tables with Slicers
- •Referencing Cells within a Pivot Table
- •Creating Pivot Charts
- •Another Pivot Table Example
- •Producing a Report with a Pivot Table
- •A What-If Example
- •Types of What-If Analyses
- •Manual What-If Analysis
- •Creating Data Tables
- •Using Scenario Manager
- •What-If Analysis, in Reverse
- •Single-Cell Goal Seeking
- •Introducing Solver
- •Solver Examples
- •Installing the Analysis ToolPak Add-in
- •Using the Analysis Tools
- •Introducing the Analysis ToolPak Tools
- •Introducing VBA Macros
- •Displaying the Developer Tab
- •About Macro Security
- •Saving Workbooks That Contain Macros
- •Two Types of VBA Macros
- •Creating VBA Macros
- •Learning More
- •Overview of VBA Functions
- •An Introductory Example
- •About Function Procedures
- •Executing Function Procedures
- •Function Procedure Arguments
- •Debugging Custom Functions
- •Inserting Custom Functions
- •Learning More
- •Why Create UserForms?
- •UserForm Alternatives
- •Creating UserForms: An Overview
- •A UserForm Example
- •Another UserForm Example
- •More on Creating UserForms
- •Learning More
- •Why Use Controls on a Worksheet?
- •Using Controls
- •Reviewing the Available ActiveX Controls
- •Understanding Events
- •Entering Event-Handler VBA Code
- •Using Workbook-Level Events
- •Working with Worksheet Events
- •Using Non-Object Events
- •Working with Ranges
- •Working with Workbooks
- •Working with Charts
- •VBA Speed Tips
- •What Is an Add-In?
- •Working with Add-Ins
- •Why Create Add-Ins?
- •Creating Add-Ins
- •An Add-In Example
- •System Requirements
- •Using the CD
- •What’s on the CD
- •Troubleshooting
- •The Excel Help System
- •Microsoft Technical Support
- •Internet Newsgroups
- •Internet Web sites
- •End-User License Agreement
Chapter 31: Protecting Your Work
6.Enter a password (twice).
7.Click OK and then save your file. When the file is closed and then re-opened, a password will be required to view or modify the VBA code.
FIGURE 31.8
Protecting a VB Project with a password.
Cross-Reference
Part VI discusses VBA macros. n
Related Topics
This section covers additional topics related to protecting and distributing your work.
Saving a worksheet as a PDF file
The PDF (Portable Document Format) file format is widely used as a way to present information in a read-only manner, with precise control over the layout. Software to display PDF files is available from a number of sources. Excel can create PDF files, but it cannot open them.
XPS is another “electronic paper” format, developed by Microsoft as an alternative to the PDF format. At this time, there is very little third-party support for the XPS format.
Save a worksheet in PDF or XPS format by choosing File Save and Send Create PDF/XPS Document Create a PDF/XPS. Excel displays its Publish as PDF Or XPS dialog box, in which you can specify a filename and location and set some other options.
645
Part IV: Using Advanced Excel Features
Note
Only the active sheet is saved. If your workbook has multiple sheets, you must save each sheet to a separate PDF or XPS file. n
Marking a workbook final
Excel lets you mark a document as “final.” This action makes two changes to the workbook:
•It makes the workbook read-only so that the file can’t be saved using the same name.
•It makes the workbook view-only so that nothing may be changed. When you open a finalized document, you see a message below the Ribbon. You can override its “final” status by clicking the Edit Anyway button.
To finalize a workbook, choose File Info Protect Workbook Mark as Final. Excel displays a dialog box in which you can confirm your choice.
Caution
Marking a document as final is not a security measure. Anyone who opens the workbook can cancel the mark as final designation. Therefore, this method doesn’t guarantee that others will not change the workbook. n
Inspecting a workbook
If you plan to distribute a workbook to others, you may want to have Excel check the file for hidden data and personal information. This tool can locate hidden information about you, your organization, or about the workbook. In some situations, you may not want to share this information with others.
To inspect a workbook, choose File Info Check for Issues Inspect Document. The Document Inspector dialog box opens, as shown in Figure 31.9. Click Inspect, and Excel displays the results of the inspection and gives you the opportunity to remove the items it finds.
Caution
If Excel identifies items in the Document Inspector, it doesn’t necessarily mean that they should be removed. In other words, you should not blindly use the Remove All buttons to remove the items that Excel locates. For example, you may have a hidden sheet that serves a critical purpose. Excel will identify that hidden sheet and make it very easy for you to delete it. To be on the safe side, always make a backup copy of your workbook before running the Document Inspector. n
Using a digital signature
Excel lets you add a digital signature to a workbook. Using a digital signature is somewhat analogous to signing a paper document. A digital signature helps to assure the authenticity of the workbook and also ensures that the content hasn’t been modified since it was signed.
646
Chapter 31: Protecting Your Work
FIGURE 31.9
The Document Inspector dialog box identifies hidden and personal information in a workbook.
After you sign a workbook, the signature is valid until you make changes and resave the file.
Getting a digital ID
To digitally sign a workbook, you must obtain a certificate from a certified authority who is able to verify the authenticity of your signature. Prices vary, depending on the certificate granting company.
Another option is to create your own digital ID, but others will not be able to verify the authenticity. Creating your own digital ID is useful if you want to ensure that no one tampers with one of your signed workbooks.
Signing a workbook
Excel supports two types of digital signatures: a visible signature and an invisible signature.
To add a visible digital signature, choose Insert Text Signature Line Microsoft Office Signature Line. Excel displays its Signature Setup dialog box, and you’re prompted for the information for the signature. After you add the signature box, double-click it to display the Sign dialog box, where you actually sign the document either by typing your name or uploading a scanned image of your signature.
Figure 31.10 shows a document with a visible digital signature.
647
Part IV: Using Advanced Excel Features
FIGURE 31.10
This document has a digital signature.
To add an invisible digital signature, choose File Info Protect Add a Digital Signature. If the signed workbook is changed in any way, the digital signature is invalidated.
648
CHAPTER
Making Your
Worksheets Error-Free
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 work-
sheets. This chapter introduces the tools and techniques available to help identify, correct, and prevent errors.
Finding and Correcting
Formula Errors
IN THIS CHAPTER
How to identify and correct common formula errors
Using Excel auditing tools
Using formula AutoCorrect
Tracing cell relationships
Checking spelling and related features
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 — or you may never discover the problem.
Formula errors tend to fall into one of the following general categories:
•Syntax errors: You have a problem with the syntax of a formula. For example, a formula may have mismatched parentheses, or a function may not have the correct number of arguments.
•Logical errors: A formula doesn’t return an error, but it contains a logical flaw that causes it to return an incorrect result.
•Incorrect reference errors: The logic of the formula is correct, but the formula uses an incorrect cell reference. As a simple example, the range reference in a Sum formula may not include all the data that you want to sum.
649
Part IV: Using Advanced Excel Features
•Semantic errors: An example is a function name that is spelled incorrectly. Excel will attempt to interpret it as a name and will display the #NAME? error.
•Circular references: A circular reference occurs when a formula refers to its own cell, either directly or indirectly. Circular references are useful in a few cases, but most of the time, a circular reference indicates a problem.
•Array formula entry error: When entering (or editing) an Array formula, you must press Ctrl+Shift+Enter to enter the formula. If you fail to do so, Excel doesn’t recognize the formula as an Array formula, and you may get an error or incorrect results.
•Incomplete calculation errors: The formulas simply aren’t calculated fully. Microsoft has acknowledged some problems with Excel’s calculation engine in some versions of Excel. To ensure that your formulas are fully calculated, press Ctrl+Alt+Shift+F9.
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 offers advice on identifying and correcting them.
Mismatched parentheses
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 are 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.
650
Chapter 32: Making Your Worksheets Error-Free
Using Formula AutoCorrect
When you enter a formula that has a syntax error, Excel attempts to determine the problem and offers a suggested correction. The accompanying figure shows an example of a proposed correction.
Be careful when accepting corrections for your formulas from Excel because it doesn’t always guess correctly. For example, I entered the following formula (which has mismatched parentheses):
=AVERAGE(SUM(A1:A12,SUM(B1:B12))
Excel then proposed the following correction to the formula:
=AVERAGE(SUM(A1:A12,SUM(B1:B12)))
You may be tempted to accept the suggestion without even thinking. In this case, the proposed formula is syntactically correct — but not what I intended. The correct formula is
=AVERAGE(SUM(A1:A12),SUM(B1:B12))
Tip
Excel can help you out with mismatched parentheses. When you’re editing a formula and you move the cursor over a parenthesis, Excel displays it (and its matching parenthesis) in bold for about one-half second. In addition, Excel color codes pairs of nested parentheses while you are editing a formula. n
Cells are filled with hash marks
A cell is filled with a series of hash marks (#) for one of two reasons:
•The column is not wide enough to accommodate the formatted numeric value. To correct it, you can make the column wider or use a different number format (see Chapter 24).
•The cell contains a formula that returns an invalid date or time. For example, Excel doesn’t support dates prior to 1900 or the use of negative time values. A formula that returns either of these values results in a cell filled with hash marks. Widening the column won’t fix it.
Blank cells are not blank
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.
651
Part IV: Using Advanced Excel Features
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 cells. Here’s how to search for cells that contain only blank characters:
1.Press Ctrl+F to display the Find and Replace dialog box.
2.Click the Options button to expand the dialog box so it displays additional options.
3.In the Find What box, enter * *. That’s an asterisk, followed by a space, and followed by another asterisk.
4.Make sure the Match Entire Cell Contents check box is selected.
5.Click Find All. If any cells that contain only space characters are found, Excel will list the cell address at the bottom of the Find and Replace dialog box.
Extra space 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 32.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 word Dog in cell A3 contains a trailing space.
FIGURE 32.1
Using a formula to identify cells that contain extra space characters.
652
Chapter 32: Making Your Worksheets Error-Free
Tracing Error Values
Often, an error in one cell is the result of an error in a precedent cell. For help in identifying the cell causing an error value to appear, activate the cell that contains the error and then choose Formulas Formula Auditing Error Checking Trace Error. Excel draws arrows to indicate which cell is the source of the error. After you identify the error, choose Formulas Formula Auditing Remove Arrows to get rid of the arrow display.
Formulas returning an error
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.
Tip
Excel allows you to choose how error values are printed. To access this feature, display the Page Setup dialog box and click the Sheet tab. You can choose to print error values as displayed (the default), or as blank cells, dashes, or #N/A. To display the Page Setup dialog box, click the dialog box launcher of the Page Layout Page Setup group. n
#DIV/0! 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 32.2. The formula in cell D2, which was copied to the cells below it, is
=(C2-B2)/C2
653
Part IV: Using Advanced Excel Features
FIGURE 32.2
#DIV/0! errors occur when the data in column C is missing.
This formula calculates the percent change between the values in columns B and C. Data isn’t available for months beyond May, 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 C:
= IF(C2=0,””,(C2-B2)/C2)
This formula displays an empty string if cell C2 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((C2-B2)/C2,””)
Note
The IFERROR function was introduced in Excel 2007. For compatibility with previous versions, use this formula:
=IF(ISERROR((C2-B2)/C2),””,(C2-B2)/C2)
#N/A errors
The #N/A error occurs if any cell referenced by a formula displays #N/A.
Note
Some users like to use =NA() or #N/A explicitly for missing data. This method makes it perfectly clear that the data is not available and hasn’t been deleted accidentally. n
654
Chapter 32: Making Your Worksheets Error-Free
The #N/A error also occurs when a LOOKUP function (HLOOKUP, LOOKUP, MATCH, or VLOOKUP) can’t find a match.
#NAME? errors
The #NAME? error occurs under these conditions:
•The formula contains an undefined range or cell name.
•The formula contains text that Excel interprets as an undefined name. A misspelled function name, for example, generates a #NAME? error.
•The formula uses a worksheet function that’s defined in an add-in, and the add-in is not installed.
Caution
Excel has a bit of a problem with range names. If you delete a name for a cell or range and the name is used in a formula, the formula continues to use the name, even though it’s no longer defined. As a result, the formula displays #NAME?. You may expect Excel to automatically convert the names to their corresponding cell references, but this doesn’t happen. n
#NULL! errors
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)
#NUM! errors
A formula returns a #NUM! error if any of the following occurs:
•You pass a non-numeric argument to a function when a numeric argument is expected.
•You pass an invalid argument to a function. For example, this formula returns #NUM!:
=SQRT(-12).
•A function that uses iteration can’t calculate a result. Examples of functions that use iteration are IRR and RATE.
•A formula returns a value that is too large or too small. Excel supports values between
–1E-307 and 1E+307.
655
Part IV: Using Advanced Excel Features
#REF! errors
A #REF! error occurs when a formula uses an invalid cell reference. This error can occur in the following situations:
•You delete the row column of a cell that is referenced by the formula. For example, the following formula displays a #REF! error if row 1, column A, or column B is deleted:
=A1/B1
•You delete the worksheet of a cell that is reference by the formula. For example, the following formula displays a #REF! error if Sheet2 is deleted:
=Sheet2!A1
•You copy a formula to a location that invalidates the relative cell references. For example, if you copy the following formula from cell A2 to cell A1, the formula returns #REF! because it attempts to refer to a nonexistent cell.
=A1-1
•You cut a cell (choose Home Clipboard Cut) and then paste it to a cell that’s referenced by a formula. The formula will display #REF!.
#VALUE! errors
A #VALUE! error is very common and can occur under the following conditions:
•An argument for a function is of an incorrect data type, or the formula attempts to perform an operation using incorrect data. For example, a formula that adds a value to a text string returns the #VALUE! error.
•A function’s argument is a range when it should be a single value.
•A custom worksheet function is not calculated. You can press Ctrl+Alt+F9 to force a recalculation.
•A custom worksheet function attempts to perform an operation that is not valid. For example, custom functions can’t modify the Excel environment or make changes to other cells.
•You forget to press Ctrl+Shift+Enter when entering an Array formula.
Pay Attention to the Colors
When you edit a cell that contains a formula, Excel color-codes the cell and range references in the formula. Excel also outlines the cells and ranges used in the formula by using corresponding colors. Therefore, you can see at a glance the cells that are used in the formula.
You also can manipulate the colored outline to change the cell or range reference. To change the references used in a formula, drag the outline’s border or fill handle (at the lower right of the outline). This technique is often easier than editing the formula.
656
Chapter 32: Making Your Worksheets Error-Free
Absolute/relative reference problems
As I describe in Chapter 10, 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 32.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)
FIGURE 32.3
Formulas in the range C4:C7 use an absolute reference to cell C1.
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.
Operator precedence problems
As I describe 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)
657
Part IV: Using Advanced Excel Features
Notice that the negation operator symbol is exactly 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)
Formulas are not calculated
If you use custom worksheet functions written in VBA, you may find that formulas that use these functions fail to get recalculated and may display incorrect results. 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.
Actual versus displayed values
You may encounter a situation in which values in a range don’t appear to add up properly. For example, Figure 32.4 shows a worksheet with the following formula entered into each cell in the range B2:B4:
=1/3
FIGURE 32.4
A simple demonstration of numbers that appear to add up incorrectly.
658
Chapter 32: Making Your Worksheets Error-Free
Cell B5 contains the following formula:
=SUM(B2:B4)
All the cells are formatted to display with three decimal places. As you can see, the formula in cell B5 appears to display an incorrect result. (You may expect it to display 0.999.) 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 Excel Options to display this dialog box.)
Caution
Be very careful with the Set Precision as Displayed option. This option also affects normal values (nonformulas) that have been entered into cells. For example, if a cell contains the value 4.68 and is displayed with no decimal places (that is, 5), selecting the Precision as Displayed check box converts 4.68 to 5.00. This change is permanent, and you can’t restore the original value if you later clear the Set Precision as Displayed check box. A better approach is to use the ROUND function to round off the values to the desired number of decimal places. n
Floating point number errors
Computers, by their very nature, don’t have infinite precision. Excel stores numbers in binary format by using eight bytes, which can handle numbers with 15-digit accuracy. Some numbers can’t be expressed precisely by using eight bytes, so the number stores 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
659
Part IV: Using Advanced Excel Features
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.
“Phantom link” errors
You may open a workbook and see a message like the one shown in Figure 32.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.
FIGURE 32.5
Excel’s way of asking whether you want to update links in a workbook.
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. 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.
660