In This Chapter
This chapter discusses various techniques that you can use to look up a value in a table. Microsoft Excel has three functions (LOOKUP, VLOOKUP, and HLOOKUP) designed for this task, but you may find that these functions don’t quite cut it. This chapter provides many lookup examples, including alternative techniques that go well beyond Excel’s normal lookup capabilities.
A lookup formula essentially returns a value from a table (in a range) by looking up another related value. A common telephone directory (remember those?) provides a good analogy: if you want to find a person’s telephone number, you first locate the name (look it up) and then retrieve the corresponding number.
Figure 8.1 shows a simple worksheet that uses several lookup formulas. This worksheet contains a table of employee data (named EmpData). When you enter a last name into cell C2, lookup formulas in D2:G2 retrieve the matching information from the table. The lookup formulas in the following table use the VLOOKUP function.
Cell | Formula |
D2 | =VLOOKUP(C2,EmpData,2,FALSE) |
E2 | =VLOOKUP(C2,EmpData,3,FALSE) |
F2 | =VLOOKUP(C2,EmpData,4,FALSE) |
G2 | =VLOOKUP(C2,EmpData,5,FALSE) |
This particular example uses four formulas to return information from the EmpData range. In many cases, you’ll want only a single value from the table, so use only one formula.
Several Excel functions are useful when writing formulas to look up information in a table. Table 8.1 lists and describes each of these functions.
Table 8.1 Functions Used in Lookup Formulas
Function | Description |
CHOOSE | Returns a specific value from a list of values supplied as arguments. |
HLOOKUP | Horizontal lookup. Searches for a value in the top row of a table and returns a value in the same column from a row you specify in the table. |
IF | Returns one value if a condition you specify is TRUE, and returns another value if the condition is FALSE. |
IFERROR* | If the first argument returns an error, the second argument is evaluated and returned. If the first argument does not return an error, then it is evaluated and returned. |
INDEX | Returns a value (or the reference to a value) from within a table or range. |
LOOKUP | Returns a value either from a one-row or one-column range. Another form of the LOOKUP function works like VLOOKUP (or HLOOKUP) but is restricted to returning a value from the last column (or row) of a range. |
MATCH | Returns the relative position of an item in a range that matches a specified value. |
OFFSET | Returns a reference to a range that is a specified number of rows and columns from a cell or range of cells. |
VLOOKUP | Vertical lookup. Searches for a value in the first column of a table and returns a value in the same row from a column you specify in the table. |
* Introduced in Excel 2007.
The examples in this chapter use the functions listed in Table 8.1.
You can use Excel’s basic lookup functions to search a column or row for a lookup value to return another value as a result. Excel provides three basic lookup functions: HLOOKUP, VLOOKUP, and LOOKUP. The MATCH and INDEX functions are often used together to return a cell or relative cell reference for a lookup value.
The VLOOKUP function looks up the value in the first column of the lookup table and returns the corresponding value in a specified table column. The lookup table is arranged vertically. The syntax for the VLOOKUP function is
VLOOKUP(lookup_value,table_array,col_index_num,range_lookup)
The VLOOKUP function’s arguments are as follows:
The classic example of a lookup formula involves an income tax rate schedule (see Figure 8.2). The tax rate schedule shows the income tax rates for various income levels. The following formula (in cell B3) returns the tax rate for the income in cell B2:
=VLOOKUP(B2,D2:F7,3)
The lookup table resides in a range that consists of three columns (D2:F7). Because the third argument for the VLOOKUP function is 3, the formula returns the corresponding value in the third column of the lookup table.
Note that an exact match is not required. If an exact match is not found in the first column of the lookup table, the VLOOKUP function uses the next largest value that is less than the lookup value. In other words, the function uses the row in which the value you want to look up is greater than or equal to the row value but less than the value in the next row. In the case of a tax table, this is exactly what you want to happen.
The HLOOKUP function works just like the VLOOKUP function except that the lookup table is arranged horizontally instead of vertically. The HLOOKUP function looks up the value in the first row of the lookup table and returns the corresponding value in a specified table row.
The syntax for the HLOOKUP function is
HLOOKUP(lookup_value,table_array,row_index_num,range_lookup)
The HLOOKUP function’s arguments are as follows:
Figure 8.3 shows the tax rate example with a horizontal lookup table (in the range E1:J3). The formula in cell B3 is
=HLOOKUP(B2,E1:J3,3)
The LOOKUP function has the following syntax:
LOOKUP(lookup_value,lookup_vector,result_vector)
The function’s arguments are as follows:
The LOOKUP function looks in a one-row or one-column range (lookup_vector) for a value (lookup_value) and returns a value from the same position in a second one-row or one-column range (result_vector).
Figure 8.4 shows the tax table again. This time, the formula in cell B3 uses the LOOKUP function to return the corresponding tax rate. The formula in B3 is
=LOOKUP(B2,D2:D7,F2:F7)
Note that LOOKUP (as opposed to VLOOKUP) can return a value that’s in a different row than the matched value. If your lookup_vector and your result_vector are not part of the same table, LOOKUP can be a useful function. If, however, they are part of the same table, VLOOKUP is usually a better choice if for no other reason than that LOOKUP will not work on unsorted data.
The MATCH and INDEX functions are often used together to perform lookups. The MATCH function returns the relative position of a cell in a range that matches a specified value. The syntax for MATCH is
MATCH(lookup_value,lookup_array,match_type)
The MATCH function’s arguments are as follows:
The INDEX function returns a cell from a range. The syntax for the INDEX function is
INDEX(array,row_num,column_num)
The INDEX function’s arguments are as follows:
Figure 8.5 shows a worksheet with dates, day names, and amounts in columns D, E, and F. When you enter a date in cell B1, the following formula (in cell B2) searches the dates in column D and returns the corresponding amount from column F. The formula in B2 is
=INDEX(F2:F21,MATCH(B1,D2:D21,0))
To understand how this formula works, start with the MATCH function. This function searches the range D2:D21 for the date in cell B1. It returns the relative row number where the date is found. This value is then used as the second argument for the INDEX function. The result is the corresponding value in F2:F21.
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.
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 A) and employee names (column B). The lookup table is named EmpList. The formula in cell B2, which follows, looks up the employee number entered into cell B1 and returns the corresponding employee name:
=VLOOKUP(B1,EmpList,2,FALSE)
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 A are not in ascending order. If the last argument for VLOOKUP is FALSE, the values need not be in ascending order.
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 Average) of a player in column C (in a range named Player). 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.
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 the lookup range (in this case, the Players range) to be in ascending order. In addition to this limitation, the formula suffers from a slight problem: if you enter a nonexistentplayer—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 don’t need to be sorted:
=INDEX(Averages,MATCH(LookupValue,Players,0))
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:D9 is named Range1, and range E2:E9 is named Range2. The word to be looked up appears in cell B1 (named Value).
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 700. The following standard LOOKUP formula (which is not case sensitive) returns 300:
=LOOKUP(Value,Range1,Range2)
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.
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 function 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
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.
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. Instead, the information in the lookup table is hard-coded into an array constant. See Chapter 14, “Introducing Arrays,” 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"})
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 Credit Hrs. The grades for each course appear in column C (Range C2:C6 is named Grade). 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)
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)}
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.
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 |
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 combine these formulas into a single formula, as shown here:
=INDEX(Table,MATCH(Month,MonthList,0),MATCH(Product,ProductList,0))
Some situations may require a lookup based on the values in two columns. Figure 8.13 shows an example.
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 | Makes |
E2:E12 | Models |
The following array formula displays the corresponding code for an automobile make and model:
{=INDEX(Code,MATCH(Make&Model,Makes&Models,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 Makes and Models.
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.14 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.
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.
The VLOOKUP and HLOOKUP functions are useful in the following situations:
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.15 shows a worksheet with student names in column A and data values in column B. Range B2:B20 is named Data. Cell E2, named Target Value, contains a value to search for in the Data range. Cell E3, named Column Offset, contains a value that represents the column offset from the Data range.
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 Paul (with a corresponding value of 6,800, which is the one closest to the Target Value of 7,200).
{=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 Column Offset 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.
Interpolation refers to the process of estimating a missing value by using existing values. For an illustration of this concept, see Figure 8.16. 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, and it includes a linear trendline. As you can see, an approximate linear relationship exists 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.
Figure 8.17 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.
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
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. |
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.18).
The formula in cell B2 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))
18.119.19.23