1. Trang chủ
  2. » Công Nghệ Thông Tin

Excel 2002 Formulas phần 8 pdf

86 319 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Cấu trúc

  • Excel 2002 Formulas

    • Part V: Miscellaneous Formula Techniques

      • Chapter 21: Tools and Methods for Debugging Formulas

        • Excel’s Auditing Tools

        • Third-Party Auditing Tools

        • Summary

    • Part VI: Developing Custom Worksheet Functions

      • Chapter 22: Introducing VBA

        • About VBA

        • Introducing the Visual Basic Editor

        • Summary

      • Chapter 23: Function Procedure Basics

        • Why Create Custom Functions?

        • An Introductory VBA Function Example

        • About Function Procedures

        • Using the Insert Function Dialog Box

        • Testing and Debugging Your Functions

        • Creating Add-Ins

        • Summary

      • Chapter 24: VBA Programming Concepts

        • An Introductory Example Function Procedure

        • Using Comments in Your Code

        • Using Variables, Data Types, and Constants

        • Using Assignment Expressions

        • Using Arrays

        • Using VBA’s Built-in Functions

        • Controlling Execution

        • Using Ranges

        • Summary

Nội dung

#VALUE! ERRORS The #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 for- mula 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. With some versions of Excel, inserting or moving a sheet may cause this error. You can use Ctrl+Alt+F9 to force a recalculation. ◆ A custom worksheet function attempts to perform an operation that is not valid. For example, custom functions cannot modify the Excel environ- ment or make changes to other cells. ◆ You forget to press Ctrl+Shift+Enter when entering an array formula. Absolute/Relative Reference Problems As described in Chapter 2, 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 to use a relative reference when you should use an absolute reference. As shown in Figure 21-2, 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) Chapter 21: Tools and Methods for Debugging Formulas 567 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 that are used, drag the outline’s border or fill handle (at the lower-right corner of the outline). 4800-x Ch21.F 8/27/01 11:59 AM Page 567 Figure 21-2: Formulas in the range C4:C6 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 Excel has some straightforward rules about the order in which mathematical oper- ations are performed. In Table 21-1, operations with a lower precedence are per- formed before operations with a higher precedence. This table, for example, shows that multiplication has a higher precedence than addition. Therefore, multiplication is performed first. TABLE 21-1 OPERATOR PRECEDENCE IN EXCEL FORMULAS Symbol Operator Precedence - Negation 1 % Percent 2 ^ Exponentiation 3 * and / Multiplication and division 4 + and - Addition and subtraction 5 & Text concatenation 6 =, <, >, and <> Comparison 7 568 Part V: Miscellaneous Formula Techniques 4800-x Ch21.F 8/27/01 11:59 AM Page 568 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 neces- sary, but, in this case, the order of operations is perfectly obvious. =1+(A1*A2) 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 opera- tor, which has a lower precedence than the exponentiation operator. Therefore, the value 3 is squared and the result is subtracted from zero, which produces a negative result. Excel is a bit unusual in interpreting the negation operator. Other spread- sheet products (for example, 1-2-3 and Quattro Pro) return -9 for both for- mulas.In addition,Excel’s VBA language also returns -9 for these expressions. 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. =0+(-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 recalculation of all formulas, press Ctrl+Alt+F9. Chapter 21: Tools and Methods for Debugging Formulas 569 4800-x Ch21.F 8/27/01 11:59 AM Page 569 Prior to Excel 2000, this key combination was not documented. 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 21-3 shows a worksheet with the following formula entered into each cell in the range B2:B4: =1/3 Figure 21-3: A simple demonstration of numbers that appear to add up incorrectly Cell B5 contains the following formula: =SUM(B2:B4) All of 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 checking the Precision as displayed checkbox on the Calculation tab of the Options dialog box (choose Tools → Options to display this dialog box). Checking the Precision as displayed checkbox also affects normal values (non-formulas) that have been entered into cells. For example, if a cell con- tains the value 4.68 and is displayed with no decimal places (that is, 5), then checking the Precision as displayed checkbox converts 4.68 to 5.00. This change is permanent and you can’t restore the original value if you later uncheck the Precision as displayed checkbox. 570 Part V: Miscellaneous Formula Techniques 4800-x Ch21.F 8/27/01 11:59 AM Page 570 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 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’ll discover that Excel calculates the formula with a result of 0.899999999999999. This occurs because the operation in parentheses is performed first, and this intermediate result stores in binary format by using an approxima- tion. The formula then adds 1 to this value, and the approximation error is propa- gated to the final result. In many cases, this type of error does not 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 for- mula is in cell A1) returns FALSE: =A1=.9 One solution to this type of error is to use Excel’s ROUND function. The follow- ing 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.225-1.333-1.225 If you use Excel 97 or a later version, the formula returns 0. Previous versions return 2.22044604925031E-16 (a number very close to zero). Beginning with Excel 97, if an addition or subtraction operation results in a value at or very close to zero, the calculation engine compensates for any error introduced as a result of converting an operand to and from binary. When you perform the previous example in Excel 97 (and later versions), it correctly displays 0. Chapter 21: Tools and Methods for Debugging Formulas 571 4800-x Ch21.F 8/27/01 11:59 AM Page 571 “Phantom Link” Errors You may open a workbook, and see a message like the one shown in Figure 21-4. This message sometimes appears even when a workbook contains no linked formulas. Figure 21-4: Excel’s way of asking you if you want to update links in a workbook In the vast majority of cases, this phantom link problem occurs because of an erroneous name. Select Insert → Name → Define and scroll through the list of names. If you see a name that refers to #REF!, delete the name. These phantom links may be created when you copy a worksheet that con- tains names. See Chapter 3 for more information about names. Circular Reference Errors A circular reference is a formula that contains a reference to the cell that contains the formula. The reference may be direct or indirect. For help tracking down a cir- cular reference, see “Excel’s Auditing Tools,” later in this chapter. As described in Chapter 14, you may encounter some situations in which you create an intentional circular reference. Excel’s Auditing Tools Excel includes a number of tools that can help you track down formula errors. This section describes the auditing tools built into Excel. 572 Part V: Miscellaneous Formula Techniques 4800-x Ch21.F 8/27/01 11:59 AM Page 572 Identifying Cells of a Particular Type The Go To Special dialog box enables you to specify the type of cells that you want Excel to select. To display this dialog box, choose Edit → Go To (or press F5 or Ctrl+G). The Go To dialog box appears. Click the Special button, which displays the Go To Special dialog box, as shown in Figure 21-5. Figure 21-5: The Go To Special dialog box If you select a multicell range before choosing Edit → Go To, the command operates only within the selected cells. If a single cell is selected, the com- mand operates on the entire worksheet. You can use the Go To Special dialog box to select cells of a certain type, which can often help in identifying errors. For example, if you choose the Formulas option, Excel selects all of the cells that contain a formula. If you zoom the work- sheet out to a small size, you can get a good idea of the worksheet’s organization (see Figure 21-6). Selecting the formula cells may also help you to spot a common error — a for- mula that has been replaced accidentally with a value. If you find a cell that’s not selected amid a group of selected formula cells, chances are good that the cell pre- viously contained a formula that has been replaced by a value. Viewing Formulas You can become familiar with an unfamiliar workbook by displaying the formulas rather than the results of the formulas. Select Tools → Options, and check the check- box labeled “Formulas” on the View tab. You may want to create a new 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. Use the Window → New Window command to open a new window. Chapter 21: Tools and Methods for Debugging Formulas 573 4800-x Ch21.F 8/27/01 11:59 AM Page 573 Figure 21-6: Zooming out and selecting all formula cells can give you a good overview of how the worksheet is designed. You can use Ctrl+` to toggle between Formula view and Normal view. In Excel 2002, you can also use the Tools → Formula Auditing → Formula Auditing Mode command to toggle formula view on and off. When formula view is in effect, the Formula Auditing Toolbar is also displayed. Figure 21-7 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. When Formula view is in effect, Excel highlights the cells that are used by the formula in the active cell. In Figure 21-7, for example, the active cell is C11. The cells used by this formula are highlighted in both windows. 574 Part V: Miscellaneous Formula Techniques 4800-x Ch21.F 8/27/01 11:59 AM Page 574 Figure 21-7: Displaying formulas (bottom window) and their results (top window) If you need a printed list of your formulas, you’ll soon find that Excel’s Formula view is not really designed for printing.My Power Utility Pak add-in (on the companion CD-ROM) includes a utility that will generate a handy, printable list of all formulas in a worksheet or workbook. Tracing Cell Relationships To understand how to trace cell relationships, you need to familiarize yourself with the following two concepts: ◆ Cell precedents: Applicable only to cells that contain a formula, a formula cell’s precedents are all the cells that contribute to the formula’s result. A direct precedent is a cell that you use directly in the formula. An indirect precedent is a cell that is not used directly in the formula, but is used by a cell that you refer to in the formula. ◆ Cell dependents: These are formula cells that depend on a particular cell. A cell’s dependents consist of all formula cells that use the cell. Again, the formula cell can be a direct dependent or an indirect dependent. Chapter 21: Tools and Methods for Debugging Formulas 575 4800-x Ch21.F 8/27/01 11:59 AM Page 575 Identifying cell precedents for a formula cell often sheds light on why the for- mula is not 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. IDENTIFYING PRECEDENTS You can identify cells used by a formula in the active cell in a number of ways. ◆ Press F2. The cells that are used directly by the formula are outlined in color, and the color corresponds to the cell reference in the formula. This technique is limited to identifying cells on the same sheet as the formula. ◆ Select Edit → Go To (or press F5) to display the Go To dialog box. Then click the Special button to display the Go To Special dialog box. Select the Precedents option, and then select either Direct only (for direct precedents only) or All levels (for direct and indirect precedents). Click OK and Excel highlights the precedent cells for the formula. This technique is limited to identifying cells on the same sheet as the formula. ◆ Press Ctrl+[ to select all direct precedent cells on the active sheet. ◆ Press Ctrl+Shift+[ to select all precedent cells (direct and indirect) on the active sheet. ◆ Display the Formula Auditing toolbar by selecting Tools → Formula Auditing → Show Formula Auditing Toolbar. Click the Trace Precedents button to draw arrows to indicate a cell’s precedents. Click this button multiple times to see additional levels of precedents. Figure 21-8 shows a worksheet with precedent arrows drawn to indicate the precedents for the formula in cell C13. IDENTIFYING DEPENDENTS You can identify formula cells that use a particular cell in a number of ways. ◆ Select Edit → Go To (or press F5) to display the Go To dialog box. Then click the Special button to display the Go To Special dialog box. Select the Dependents option, and then select either Direct only (for direct depen- dents only) or All levels (for direct and indirect dependents). Click OK; Excel highlights the cells that depend on the active cell. This technique is limited to identifying cells on the active sheet only. ◆ Press Ctrl+] to select all direct dependent cells on the active sheet. ◆ Press Ctrl+Shift+] to select all dependent cells (direct and indirect) on the active sheet. 576 Part V: Miscellaneous Formula Techniques 4800-x Ch21.F 8/27/01 11:59 AM Page 576 [...]... Circular Reference toolbar 577 480 0-x Ch21.F 5 78 8/27/01 11:59 AM Page 5 78 Part V: Miscellaneous Formula Techniques Using Excel 2002 s Background Error Checking Feature If you use Excel 2002, you may find it helpful to take advantage of the new automatic error checking feature The information in this section applies only to Excel 2002 The Options dialog box in Excel 2002 contains several new tabs,... with a value 579 480 0-x Ch21.F 580 8/ 27/01 11:59 AM Page 580 Part V: Miscellaneous Formula Techniques Using Excel 2002 s Formula Evaluator Excel 2002 includes a new feature called the “Formula Evaluator,” which lets you see the various parts of a nested formula evaluated in the order that the formula is calculated The information in this section applies only to Excel 2002 To use the Formula Evaluator,... custom functions that you can use in your work- sheet formulas Excel also includes another way of creating custom functions by using the XLM macro language XLM is pretty much obsolete, but it is still supported for compatibility purposes This book completely ignores the XLM language and focuses on VBA 587 480 0-x Ch22.F 588 8/ 27/01 11:59 AM Page 588 Part VI: Developing Custom Worksheet Functions VBA... correcting them I also described the auditing tools that are built into Excel, plus three thirdparty auditing tools that you may find helpful The next chapter is the first of four chapters to provide information about creating custom worksheet functions by using VBA 583 480 0-x Ch21.F 8/ 27/01 11:59 AM Page 584 480 0-x PO6.F 8/ 27/01 11:59 AM Page 585 Part VI Developing Custom Worksheet Functions CHAPTER 22 Introducing... Function Examples 480 0-x PO6.F 8/ 27/01 11:59 AM Page 586 480 0-x Ch22.F 8/ 27/01 11:59 AM Page 587 Chapter 22 Introducing VBA IN THIS CHAPTER N An introduction to Visual Basic for Applications — Excel s programming language N How to use the Visual Basic Editor N How to work in the code windows of the Visual Basic Editor THIS CHAPTER INTRODUCES YOU to Visual Basic for Applications (VBA) VBA is Excel s programming... workbook Unlike Excel s Define Name dialog box, this utility also lists hidden names N Formula Report: Generates a useful (and printable) listing of all formulas in a worksheet (see Figure 21-15) This is much more useful than Excel s formula view N VBA Project Summary Report: Generates a report that describes the VBA procedures contained in a workbook 581 480 0-x Ch21.F 582 8/ 27/01 11:59 AM Page 582 Part V:... http://www.uq.net.au/detective/ Excel Auditor The Excel Auditor is available from Byg Software, based in the United Kingdom This product includes many tools to help you identify and correct spreadsheet errors For complete information, visit the following URL: http://www.bygsoftware.com 480 0-x Ch21.F 8/ 27/01 11:59 AM Page 583 Chapter 21: Tools and Methods for Debugging Formulas Summary This chapter discussed... can specify which types of errors to check for by using the checkboxes in the Rules section Figure 21-10: Excel 2002 can check your formulas for potential errors When error checking is turned on, Excel continually evaluates your worksheet, including its formulas 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 Smart Tag... Tools A few third-party auditing tools for Excel are available — namely the Power Utility Pak, the Spreadsheet Detective, and the Excel Auditor I describe them in the following sections 480 0-x Ch21.F 8/ 27/01 11:59 AM Page 581 Chapter 21: Tools and Methods for Debugging Formulas Power Utility Pak My Power Utility Pak includes a number of useful utilities relevant to auditing a worksheet These utilities... available VB Editor toolbars work just like those in Excel: You can customize toolbars, move them around, display other toolbars, and so forth 589 480 0-x Ch22.F 590 8/ 27/01 11:59 AM Page 590 Part VI: Developing Custom Worksheet Functions PROJECT WINDOW The Project window displays a tree diagram that consists of every workbook that’s currently open in Excel (including add-ins and hidden workbooks) In the . using VBA. Chapter 21: Tools and Methods for Debugging Formulas 583 480 0-x Ch21.F 8/ 27/01 11:59 AM Page 583 480 0-x Ch21.F 8/ 27/01 11:59 AM Page 584 Developing Custom Worksheet Functions CHAPTER 22 Introducing. value. Chapter 21: Tools and Methods for Debugging Formulas 579 480 0-x Ch21.F 8/ 27/01 11:59 AM Page 579 Using Excel 2002 s Formula Evaluator Excel 2002 includes a new feature called the “Formula. 21: Tools and Methods for Debugging Formulas 577 480 0-x Ch21.F 8/ 27/01 11:59 AM Page 577 Using Excel 2002 s Background Error Checking Feature If you use Excel 2002, you may find it helpful to take

Ngày đăng: 14/08/2014, 06:22

TỪ KHÓA LIÊN QUAN