Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 86 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
86
Dung lượng
1,06 MB
Nội dung
Choosing among Multiple Lookup Tables You can, of course, have any number of lookup tables in a worksheet. In some cases, your formula may need to decide which lookup table to use. Figure 8-9 shows an example. Figure 8-9: This worksheet demonstrates the use of multiple lookup tables. This workbook calculates sales commission and contains two lookup tables: G3:H9 (named Table1) and J3:K8 (named Table2). The commission rate for a partic- ular sales representative depends on two factors: the sales rep’s years of service (column B) and the amount sold (column C). Column D contains formulas that look up the commission rate from the appropriate table. For example, the formula in cell D2 is: =VLOOKUP(C2,IF(B2<3,Table1,Table2),2) The second argument for the VLOOKUP function consists of an IF formula that uses the value in column B to determine which lookup table to use. The formula in column E simply multiplies the sales amount in column C by the commission rate in column D. The formula in cell E2, for example, is: =C2*D2 You can access the workbook shown in Figure 8-9 on the companion CD-ROM. Chapter 8: Lookups 223 4800-x Ch08.F 8/27/01 11:55 AM Page 223 Determining Letter Grades for Test Scores A common use of a lookup table is to assign letter grades for test scores. Figure 8-10 shows a worksheet with student test scores. The range E2:F6 (named GradeList) displays a lookup table used to assign a letter grade to a test score. Figure 8-10: Looking up letter grades for test scores The companion CD-ROM contains a workbook that demonstrates both for- mulas in this section. Column C contains formulas that use the VLOOKUP function and the lookup table to assign a grade based on the score in column B. The formula in C2, for example, is: =VLOOKUP(B2,GradeList,2) When the lookup table is small (as in the example shown in Figure 8-10), you can use a literal array in place of the lookup table. The formula that follows, for example, returns a letter grade without using a lookup table. Rather, the informa- tion in the lookup table is hard-coded into a literal array. See Chapter 14 for more information about literal arrays. =VLOOKUP(B2,{0,”F”;40,”D”;70,”C”;80,”B”;90,”A”},2) Another approach, which uses a more legible formula, is to use the LOOKUP function with two array arguments: =LOOKUP(B2,{0,40,70,80,90},{“F”,“D”,“C”,“B”,“A”}) 224 Part II: Using Functions in Your Formulas 4800-x Ch08.F 8/27/01 11:55 AM Page 224 Calculating a Grade Point Average A student’s grade point average (GPA) is a numerical measure of the average grade received for classes taken. This discussion assumes a letter grade system, in which each letter grade is assigned a numeric value (A=4, B=3, C=2, D=1, and F=0). The GPA comprises an average of the numeric grade values, weighted by the credit hours of the course. A one-hour course, for example, receives less weight than a three-hour course. The GPA ranges from 0 (all Fs) to 4.00 (all As). Figure 8-11 shows a worksheet with information for a student. This student took five courses, for a total of 13 credit hours. Range B2:B6 is named CreditHours. The grades for each course appear in column C (Range C2:C6 is named Grades). Column D uses a lookup formula to calculate the grade value for each course. The lookup formula in cell D2, for example, follows. This formula uses the lookup table in G2:H6 (named GradeTable). =VLOOKUP(C2,GradeTable,2,FALSE) Figure 8-11: Using multiple formulas to calculate a GPA Formulas in column E calculate the weighted values. The formula in E2 is: =D2*B2 Cell B8 computes the GPA using the following formula: =SUM(E2:E6)/SUM(B2:B6) The preceding formulas work fine, but you can streamline the GPA calculation quite a bit. In fact, you can use a single array formula to make this calculation and avoid using the lookup table and the formulas in columns D and E. This array for- mula does the job: {=SUM((MATCH(Grades,{“F”,”D”,”C”,”B”,”A”},0)-1)*CreditHours) /SUM(CreditHours)} Chapter 8: Lookups 225 4800-x Ch08.F 8/27/01 11:55 AM Page 225 You can access a workbook that demonstrates both the multiformula and the array formula techniques on the companion CD-ROM. Performing a Two-Way Lookup Figure 8-12 shows a worksheet with a table that displays product sales by month. To retrieve sales for a particular month and product, the user enters a month in cell B1 and a product name in cell B2. Figure 8-12: This table demonstrates a two-way lookup. The companion CD-ROM contains the workbook shown in Figure 8-12. To simplify things, the worksheet uses the following named ranges: Name Refers To Month B1 Product B2 Table D1:H14 MonthList D1:D14 ProductList D1:H1 226 Part II: Using Functions in Your Formulas 4800-x Ch08.F 8/27/01 11:55 AM Page 226 The following formula (in cell B4) uses the MATCH function to return the posi- tion of the Month within the MonthList range. For example, if the month is January, the formula returns 2 because January is the second item in the MonthList range (the first item is a blank cell, D1). =MATCH(Month,MonthList,0) The formula in cell B5 works similarly, but uses the ProductList range. =MATCH(Product,ProductList,0) The final formula, in cell B6, returns the corresponding sales amount. It uses the INDEX function with the results from cells B4 and B5. =INDEX(Table,B4,B5) You can, of course, combine these formulas into a single formula, as shown here: =INDEX(Table,MATCH(Month,MonthList,0),MATCH(Product,ProductList,0)) If you use Excel 97 or later, you can use the Lookup Wizard add-in to create this type of formula (see Figure 8-13). The Lookup Wizard add-in is distrib- uted with Excel. Figure 8-13: The Lookup Wizard add-in can create a formula that performs a two-way lookup. Chapter 8: Lookups 227 4800-x Ch08.F 8/27/01 11:55 AM Page 227 Another way to accomplish a two-way lookup is to provide a name for each row and column of the table. A quick way to do this is to select the table and use Insert → Name → Create. After creating the names, you can use a simple formula such as: = Sprockets July This formula, which uses the range intersection operator (a space), returns July sales for Sprockets. See Chapter 3 for details. Performing a Two-Column Lookup Some situations may require a lookup based on the values in two columns. Figure 8-14 shows an example. Figure 8-14: This workbook performs a lookup using information in two columns (D and E). The workbook shown in Figure 8-14 also appears on the companion CD-ROM. The lookup table contains automobile makes and models, and a corresponding code for each. The worksheet uses named ranges, as shown here: F2:F12 Code B1 Make B2 Model 228 Part II: Using Functions in Your Formulas 4800-x Ch08.F 8/27/01 11:55 AM Page 228 D2:D12 Range1 E2:E12 Range2 The following array formula displays the corresponding code for an automobile make and model: {=INDEX(Code,MATCH(Make&Model,Range1&Range2,0))} This formula works by concatenating the contents of Make and Model, and then searching for this text in an array consisting of the concatenated corresponding text in Range1 and Range2. Determining the Address of a Value within a Range Most of the time, you want your lookup formula to return a value. You may, how- ever, need to determine the cell address of a particular value within a range. For example, Figure 8-15 shows a worksheet with a range of numbers that occupy a single column (named Data). Cell B1, which contains the value to look up, is named Target. Figure 8-15: The formula in cell B2 returns the address in the Data range for the value in cell B1. The formula in cell B2, which follows, returns the address of the cell in the Data range that contains the Target value: =ADDRESS(ROW(Data)+MATCH(Target,Data,0)-1,COLUMN(Data)) Chapter 8: Lookups 229 4800-x Ch08.F 8/27/01 11:55 AM Page 229 If the Data range occupies a single row, use this formula to return the address of the Target value: =ADDRESS(ROW(Data),COLUMN(Data)+MATCH(Target,Data,0)-1) The companion CD-ROM contains the workbook shown in Figure 8-15. If the Data range contains more than one instance of the Target value, the address of the first occurrence is returned. If the Target value is not found in the Data range, the formula returns #N/A. Looking Up a Value Using the Closest Match The VLOOKUP and HLOOKUP functions are useful in the following situations: ◆ You need to identify an exact match for a target value. Use FALSE as the function’s fourth argument. ◆ You need to locate an approximate match. If the function’s fourth argu- ment is TRUE or omitted and an exact match is not found, the next largest value less than the lookup value is returned. But what if you need to look up a value based on the closest match? Neither VLOOKUP nor HLOOKUP can do the job. Figure 8-16 shows a worksheet with student names in column A and values in column B. Range B2:B20 is named Data. Cell E2, named Target, contains a value to search for in the Data range. Cell E3, named ColOffset, contains a value that repre- sents the column offset from the Data range. You can access the workbook shown in Figure 8-16 on the companion CD-ROM. The array formula that follows identifies the closest match to the Target value in the Data range, and returns the names of the corresponding student in column A (i.e., the column with an offset of –1). The formula returns Leslie (with a matching value of 8,000, which is the one closest to the Target value of 8,025). 230 Part II: Using Functions in Your Formulas 4800-x Ch08.F 8/27/01 11:55 AM Page 230 Figure 8-16: This workbook demonstrates how to perform a lookup using the closest match. {=INDIRECT(ADDRESS(ROW(Data)+MATCH(MIN(ABS(Target-Data)), ABS(Target-Data),0)-1,COLUMN(Data)+ColOffset))} If two values in the Data range are equidistant from the Target value, the for- mula uses the first one in the list. The value in ColOffset can be negative (for a column to the left of Data), positive (for a column to the right of Data), or 0 (for the actual closest match value in the Data range). To understand how this formula works, you need to understand the INDIRECT function. This function’s first argument is a text string in the form of a cell refer- ence (or a reference to a cell that contains a text string). In this example, the text string is created by the ADDRESS function, which accepts a row and column refer- ence and returns a cell address. Looking Up a Value Using Linear Interpolation Interpolation refers to the process of estimating a missing value by using existing values. To illustrate, refer to Figure 8-17. Column D contains a list of values (named x) and column E contains corresponding values (named y). The worksheet also contains a chart that depicts the relationship between the x range and the y range graphically. As you can see, there is an approximate linear relationship between the corresponding values in the x and y ranges: as x increases, so does y. Notice that the values in the x range are not strictly consecutive. For example, the x range doesn’t contain the following values: 3, 6, 7, 14, 17, 18, and 19. Chapter 8: Lookups 231 4800-x Ch08.F 8/27/01 11:55 AM Page 231 Figure 8-17: This workbook demonstrates a table lookup using linear interpolation. You can create a lookup formula that looks up a value in the x range and returns the corresponding value from the y range. But what if you want to estimate the y value for a missing x value? A normal lookup formula does not return a very good result because it simply returns an existing y value (not an estimated y value). For example, the following formula looks up the value 3, and returns 18.00 (the value that corresponds to 2 in the x range): =LOOKUP(3,x,y) In such a case, you probably want to interpolate. In other words, because the lookup value (3) is halfway between existing x values (2 and 4), you want the for- mula to return a y value of 21.000 — a value halfway between the corresponding y values 18.00 and 24.00. FORMULAS TO PERFORM A LINEAR INTERPOLATION Figure 8-18 shows a worksheet with formulas in column B. The value to be looked up is entered into cell B1. The final formula, in cell B16, returns the result. If the value in B3 is found in the x range, the corresponding y value is returned. If the value in B3 is not found, the formula in B16 returns an estimated y value, obtained using linear interpolation. The companion CD-ROM contains the workbook shown in Figure 8-18. 232 Part II: Using Functions in Your Formulas 4800-x Ch08.F 8/27/01 11:56 AM Page 232 [...]... =SUBTOTAL(9,C5:C400) Figure 9 -4 shows the result of these formulas when applied to a filtered list 48 00-x Ch09.F 8/27/01 11:56 AM Page 243 Chapter 9: Databases and Lists Figure 9 -4: The formulas in cells C1 and C2 use the SUBTOTAL function The SUBTOTAL function is the only function that recognizes data hidden by AutoFiltering If you have other formulas that refer to data in a filtered list, these formulas. .. select and delete the visible rows in the table, and the rows hidden by AutoFiltering will not be affected 243 48 00-x Ch09.F 244 8/27/01 11:56 AM Page 244 Part II: Using Functions in Your Formulas About the SUBTOTAL Function The SUBTOTAL function is very versatile It’s unique in that it is the only Excel function that ignores cells in hidden rows There is one caveat, however: The rows must be hidden as... only the columns for which you copied the labels 247 48 00-x Ch09.F 248 8/27/01 11:56 AM Page 248 Part II: Using Functions in Your Formulas Working with Data in a List Excel s Data ¡ Form command displays a dialog box to help you work with a list This dialog enables you to enter new data, delete rows, and search for rows that match certain criteria Excel s Data Form is handy, but by no means ideal If... Returns the y value that corresponds to the x value in B9 Continued 233 48 00-x Ch08.F 2 34 8/27/01 11:56 AM Page 2 34 Part II: Using Functions in Your Formulas TABLE 8-2 FORMULAS FOR A LOOKUP USING LINEAR INTERPOLATION (Continued) Cell Formula Description B13 =LOOKUP(B10,x,y) Returns the y value that corresponds to the x value in B10 B15 =IF(B4,0,(B1-B3)/ (B10-B9)) Calculates an adjustment factor based on the... Go To dialog box The hidden name does not appear in the list of names, so you need to enter it manually.Type _FilterDatabase in the Reference field and click OK 241 48 00-x Ch09.F 242 8/27/01 11:56 AM Page 242 Part II: Using Functions in Your Formulas Custom AutoFiltering is useful, but it definitely has limitations For example, if you want to filter a list to show only three values in a field (such as... 239 48 00-x Ch09.F 240 8/27/01 11:56 AM Page 240 Part II: Using Functions in Your Formulas Using AutoFiltering Filtering a list involves the process of hiding all rows in the list except those rows that meet some criteria that you specify For example, if you have a list of customers, you can filter the list to show only those who live in Oregon Filtering is a common (and very useful) technique Excel. .. in Excel have focused exclusively on AutoFiltering The use of a separate criteria range for advanced filtering originated with the original version of Lotus 1-2-3 Excel adapted this method, and it 48 00-x Ch09.F 8/27/01 11:56 AM Page 249 Chapter 9: Databases and Lists has never been changed, despite the fact that specifying advanced filtering criteria remains one of the most confusing aspects of Excel. .. Date Listed date is less or equal to March 31 253 48 00-x Ch09.F 2 54 8/27/01 11:56 AM Page 2 54 Part II: Using Functions in Your Formulas The criteria shown in Figure 9-9 may not work properly for systems that don’t use the U.S date formats.To ensure compatibility with different date systems, use the DATE function to define such criteria, as in the following formulas =”>=”&DATE(2001,3,1) =” . C: =SUBTOTAL(9,C5:C400) Figure 9 -4 shows the result of these formulas when applied to a filtered list. 242 Part II: Using Functions in Your Formulas 48 00-x Ch09.F 8/27/01 11:56 AM Page 242 . B1 Product B2 Table D1:H 14 MonthList D1:D 14 ProductList D1:H1 226 Part II: Using Functions in Your Formulas 48 00-x Ch08.F 8/27/01 11:55 AM Page 226 The following formula (in cell B4) uses the MATCH. two array arguments: =LOOKUP(B2,{0 ,40 ,70,80,90},{“F”,“D”,“C”,“B”,“A”}) 2 24 Part II: Using Functions in Your Formulas 48 00-x Ch08.F 8/27/01 11:55 AM Page 2 24 Calculating a Grade Point Average A