Specialized Lookup Formulas


You can use some additional types of lookup formulas to perform more specialized lookups. For instance, you can look up an exact value, search in another column besides the first in a lookup table, perform a case-sensitive lookup, return a value from among multiple lookup tables, and perform other specialized and complex lookups.

image from book
When a Blank Is Not a Zero

Excel's lookup functions treat empty cells in the result range as zeros. The worksheet in the accompanying figure contains a two-column lookup table, and this formula looks up the name in cell B1 and returns the corresponding amount:

 =VLOOKUP(B1,D2:E8,2) 

Note that the Amount cell for Charlie is blank, but the formula returns a 0.

image from book

If you need to distinguish zeros from blank cells, you must modify the lookup formula by adding an IF function to check whether the length of the returned value is 0. When the looked up value is blank, the length of the return value is 0. In all other cases, the length of the returned value is non-zero. The following formula displays an empty string (a blank) whenever the length of the looked-up value is zero, and the actual value whenever the length is anything but zero:

 =IF(LEN(VLOOKUP(B1,D2:E8,2))=0,"",(VLOOKUP(B1,D2:E8,2))) 

Alternatively, you can specifically check for a blank cell, as in the following formula:

 =IF(ISBLANK(VLOOKUP(B1,D2:E8,2)),"",(VLOOKUP(B1,D2:E8,2))) 
image from book

On the CD 

The examples in this section are available on the companion CD-ROM. The filename is image from book specialized lookup examples.xlsx.

Looking Up an Exact Value

As demonstrated in the previous examples, VLOOKUP and HLOOKUP don't necessarily require an exact match between the value to be looked up and the values in the lookup table. An example of an approximate match is looking up a tax rate in a tax table. In some cases, you may require a perfect match. For example, when looking up an employee number, you would probably require a perfect match for the number.

To look up an exact value only, use the VLOOKUP (or HLOOKUP) function with the optional fourth argument set to FALSE.

Figure 8-6 shows a worksheet with a lookup table that contains employee numbers (column C) and employee names (column D). The lookup table is named EmpList. The formula in cell B2, which follows, looks up the employee number entered in cell B1 and returns the corresponding employee name:

 =VLOOKUP(B1,EmpList,2,FALSE) 

image from book
Figure 8-6: This lookup table requires an exact match.

Because the last argument for the VLOOKUP function is FALSE, the function returns an employee name only if an exact match is found. If the employee number is not found, the formula returns #N/A. This, of course, is exactly what you want to happen because returning an approximate match for an employee number makes no sense. Also, notice that the employee numbers in column C are not in ascending order. If the last argument for VLOOKUP is FALSE, the values need not be in ascending order.

New 

If you prefer to see something other than #N/A when the employee number is not found, you can use the new IFERROR function to test for #NA. The syntax for IFERROR is

     IFERROR(value, value_if_error) 
  • value: Any expression, usually a formula that can result in an error

  • value_if_error: Any expression including numbers, text, cell references or formulas

IFERROR returns value if it's not an error and value_if_error if it is. The following formula displays the text Not Found rather than #N/A:

     =IFERROR(VLOOKUP(B1,EmpList,2,FALSE) "Not Found") 

Looking Up a Value to the Left

The VLOOKUP function always looks up a value in the first column of the lookup range. But what if you want to look up a value in a column other than the first column? It would be helpful if you could supply a negative value for the third argument for VLOOKUP-but you can't.

Figure 8-7 illustrates the problem. Suppose you want to look up the batting average (column B, in a range named Averages) of a player in column C (in a range named Players). The player you want data for appears in a cell named LookupValue. The VLOOKUP function won't work because the data is not arranged correctly. One option is to rearrange your data, but sometimes that's not possible.

image from book
Figure 8-7: The VLOOKUP function can't look up a value in column B, based on a value in column C.

Another solution is to use the LOOKUP function, which requires two range arguments. The following formula (in cell F3) returns the batting average from column B of the player name contained in the cell named LookupValue:

 =LOOKUP(LookupValue,Players,Averages) 

Using the LOOKUP function requires that the lookup range (in this case, the Players range) is in ascending order. In addition to this limitation, the formula suffers from a slight problem: If you enter a nonexistent player (in other words, the LookupValue cell contains a value not found in the Players range), the formula returns an erroneous result.

A better solution uses the INDEX and MATCH functions. The formula that follows works just like the previous one except that it returns #N/A if the player is not found. Another advantage to using this formula is that the player names need not be sorted.

 =INDEX(Averages,MATCH(LookupValue,Players,0)) 

Performing a Case-Sensitive Lookup

Excel's lookup functions (LOOKUP, VLOOKUP, and HLOOKUP) are not case sensitive. For example, if you write a lookup formula to look up the text budget, the formula considers any of the following a match: BUDGET, Budget, or BuDgEt.

Figure 8-8 shows a simple example. Range D2:D7 is named Range1, and range E2:E7 is named Range2. The word to be looked up appears in cell B1 (named Value).

image from book
Figure 8-8: Using an array formula to perform a case-sensitive lookup.

The array formula that follows is in cell B2. This formula does a case-sensitive lookup in Range1 and returns the corresponding value in Range2.

 {=INDEX(Range2,MATCH(TRUE,EXACT(Value,Range1),0))} 

The formula looks up the word DOG (uppercase) and returns 300. The following standard LOOKUP formula (which is not case sensitive) returns 400:

 =LOOKUP(Value,Range1,Range2) 
Note 

When entering an array formula, remember to use Ctrl+Shift+Enter.

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.

image from book
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 particular 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 

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.

image from book
Figure 8-10: Looking up letter grades for test scores.

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 information in the lookup table is hard-coded into an array constant. See Chapter 14 for more information about array constants.

 =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"}) 

Finally, whenever you can easily convert your input, the number grade in this case, into the integers 1 to 29, the CHOOSE function becomes an option. The number grades are divided by 10, the decimal is stripped off, and 1 is added to it to produce the numbers 1 to 11. The remaining arguments define the return values for those 11 options.

 =CHOOSE(TRUNC(B2/10)+1,"F","F","F","F","D","D","D","C","B","A","A") 

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 onehour 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).

image from book
Figure 8-11: Using multiple formulas to calculate a GPA.

 =VLOOKUP(C2,GradeTable,2,FALSE) 

Formulas in column E calculate the weighted values. The formula in E2 is

 =D2*B2 

Cell B8 computes the GPA by 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 formula does the job:

 {=SUM((MATCH(Grades,{"F","D","C","B","A"},0)-1)*CreditHours) /SUM(CreditHours)} 

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.

image from book
Figure 8-12: This table demonstrates a two-way lookup.

To simplify things, the worksheet uses the following named ranges:

Open table as spreadsheet

Name

Refers To

Month

B1

Product

B2

Table

D1:H14

MonthList

D1:D14

ProductList

D1:H1

The following formula (in cell B4) uses the MATCH function to return the position 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)) 

Tip 

You can use the Lookup Wizard add-in to create this type of formula (see Figure 8-13). The Lookup Wizard add-in is distributed with Excel. After installing the add-in, access the Lookup Wizard by choosing Formulas image from book Solutions image from book Lookup.

image from book
Figure 8-13: The Lookup Wizard add-in can create a formula that performs a two-way lookup.

Tip 

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 Formulas image from book Defined Names image from book Create from Selection. After creating the names, you can use a simple formula to perform the two-way lookup, such as

 =Sprockets July 

This formula, which uses the range intersection operator (a space), returns July sales for Sprockets. To refer to the cells where the month and product are entered, use

 =INDIRECT(Month) INDIRECT(Product) 

This formula converts the values in the cells Month and Product into range references and finds the intersection. See Chapter 3 for details about the range intersection operator.

Performing a Two-Column Lookup

Some situations may require a lookup based on the values in two columns. Figure 8-14 shows an example.

image from book
Figure 8-14: This workbook performs a lookup by using information in two columns (D and E).

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

D2:D12

Range1

E2:E12

Range2

Open table as spreadsheet

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, however, 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.

image from book
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)) 

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) 

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 by 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 argument is TRUE or omitted and an exact match is not found, the next largest value that is 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 represents the column offset from the Data range.

image from book
Figure 8-16: This workbook demonstrates how to perform a lookup by using the closest match.

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 (that is, 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).

 {=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 formula 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 reference (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 reference 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. For an illustration of this concept, see Figure 8-17. Column D contains a list of values (named x) and column E contains corresponding values (named y).

image from book
Figure 8-17: This workbook demonstrates a table lookup using linear interpolation.

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.

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 formula to return a y value of 21.00-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.

image from book
Figure 8-18: Column B contains formulas that perform a lookup using linear interpolation.

It's critical that the values in the x range appear in ascending order. If B1 contains a value less than the lowest value in x or greater than the largest value in x, the formula returns an error value. Table 8-2 lists and describes these formulas.

Table 8-2: FORMULAS FOR A LOOKUP USING LINEAR INTERPOLATION
Open table as spreadsheet

Cell

Formula

Description

B3

=LOOKUP(B1,x,x)

Performs a standard lookup on the x range and returns the looked-up value.

B4

=B1=B3

Returns TRUE if the looked-up value equals the value to be looked up.

B6

=MATCH(B3,x,0)

Returns the row number of the x range that contains the matching value.

B7

=IF(B4,B6,B6+1)

Returns the same row as the formula in B6 if an exact match is found. Otherwise, it adds 1 to the result in B6.

B9

=INDEX(x,B6)

Returns the x value that corresponds to the row in B6.

B10

=INDEX(x,B7)

Returns the x value that corresponds to the row in B7.

B12

=LOOKUP(B9,x,y)

Returns the y value that corresponds to the x value in B9.

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 difference between the x values.

B16

=B12+((B13-B12)*B15)

Calculates the estimated y value using the adjustment factor in B15.

COMBINING THE LOOKUP AND TREND FUNCTIONS

Another slightly different approach, which you may find preferable to performing lookup using linear interpolation, uses the LOOKUP and TREND functions. One advantage is that it requires only one formula (see Figure 8-19).

image from book
Figure 8-19: This worksheet uses a formula that uses the LOOKUP function and the TREND function.

The formula in cell B3 follows. This formula uses an IF function to make a decision. If an exact match is found in the x range, the formula returns the corresponding y value (using the LOOKUP function). If an exact match is not found, the formula uses the TREND function to return the calculated "best-fit" y value. (It does not perform a linear interpolation.)

 =IF(B1=LOOKUP(B1,x,x),LOOKUP(INDEX(x,MATCH (LOOKUP(B1,x,x),x,0)),x,y),TREND(y,x,B1)) 




Excel 2007 Formulas
Excel 2007 Formulas (Mr. Spreadsheets Bookshelf)
ISBN: 0470044020
EAN: 2147483647
Year: 2007
Pages: 212

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net