Lecture-18 Lookup Functions How do I write a formula to compute tax rates based on income? Given a product ID, how can I look up the product’s price? Suppose that a product’s price changes over time. I know the date the product was sold. How can I write a formula to compute the product’s price? Syntax of the Lookup Functions Lookup functions enable you to “look up” values from worksheet ranges. Microsoft Office Excel allows you to perform both vertical lookups (by using the VLOOKUP function) and horizontal lookups (by using the HLOOKUP function). In a vertical lookup, the lookup operation starts in the first column of a worksheet range. In a horizontal lookup, the operation starts in the first row of a worksheet range. Because the majority of formulas using lookup functions involve vertical lookups, we’ll concentrate on VLOOKUP functions. VLOOKUP Syntax The syntax of the VLOOKUP function is as follows. The brackets ([ ]) indicate optional arguments. VLOOKUP (lookup value, table range, column index, [range lookup]) Lookup value is the value that we want to look up in the first column of the table range. Table range is the range that contains the entire lookup table. The table range includes the first column, in which we try and match the lookup value, and any other columns in which we will look up formula results. Column index is the column number in the table range from which the value of the lookup function is obtained. Md. Abdullah Al Mahmud Senior Lecturer Manarat International University Range lookup is an optional argument. The point of range lookup is to allow you to specify an exact or approximate match. If the range lookup argument is True or omitted, the first column of the table range must be in ascending numerical order. If the range lookup argument is True or omitted and an exact match to the lookup value is found in the first column of the table range, Excel bases the lookup on the row of the table in which the exact match is found. If the range lookup argument is True or omitted and an exact match does not exist, Excel bases the lookup on the largest value in the first column that is less than the lookup value. If the range lookup argument is False and an exact match to the lookup value is found in the first column of the table range, Excel bases the lookup on the row of the table in which the exact match is found. If no exact match is obtained, Excel returns an #N/A response (Not Available). Note that a range lookup argument of 1 is equivalent to TRUE whereas a range lookup argument of 0 is equivalent to FALSE. HLOOKUP Syntax In an HLOOKUP function, Excel tries to locate the lookup value in the first row (not the first column) of the table range. For an HLOOKUP function, use the VLOOKUP syntax and change “column” to “row.” Let’s explore some interesting examples of lookup functions. How do I write a formula to compute tax rates based on income? The following example shows how a VLOOKUP function works when the first column of the table range consists of numbers in ascending order. Suppose that the tax rate depends on income, as shown in the following table. Income level Tax rate $0–$9,999 15% $10,000–$29,999 30% $30,000–$99,999 34% $100,000 and over 40% Md. Abdullah Al Mahmud Senior Lecturer Manarat International University I began by entering the relevant information (tax rates and break points) in cell range D6:E9. I named the table range D6:E9 lookup . I recommend that you always name the cells you’re using as the table range. If you do so, you need not remember the exact location of the table range, and when you copy any formula involving a lookup function, the lookup range will always be correct. To illustrate how the lookup function works, I entered some incomes in the range D13:D17. By copying from E13:E17 the formula VLOOKUP (D13, Lookup, 2, True), we computed the tax rate for the income levels listed in D13:D17. Let’s examine how the lookup function worked in cells E13:E17. Note that because the column index in the formula is 2, the answer always comes from the second column of the table range. In D 13, the income of –$ 1, 0 00 yields #N/A because –$1,000 is less than the lowest income level in the first column of the table range. If you wanted a tax rate of 15percent associated with an income of –$1,000, simply replace the 0 in D6 by a number that is –1,000 or smaller. In D14, the income of $30,000 exactly matches a value in the first column of the table range, so the function returns a tax rate of 34 percent. In D15, the income level of $29,000 does not exactly match a value in the first column of the table range, which means the lookup function stops at the largest number less than $29,000 in the first column of the range–$10,000 in this case. This function returns the tax rate in column 2 of the table range opposite $10,000, or 30 percent. In D16, the income level of $98,000 does not yield an exact match in the first column of the table range. The lookup function stops at the largest number less than $98,000 in the first column of the table range. This returns the tax rate in column 2 of the table range opposite $30,000–34 percent. In D17, the income level of $104,000 does not yield an exact match in the first column Md. Abdullah Al Mahmud Senior Lecturer Manarat International University of the table range. The lookup function stops at the largest number less than $104,000 in the first column of the table range, which returns the tax rate in column 2 of the table range opposite $100,000–40 percent. In F13:F17, we changed the value of the range lookup argument from True to False and copied from F13 to F14:F17 the formula VLOOKUP(D13,Lookup,2,False) . Cell F14 still yields a 34 percent tax rate because the first column of the table range contains an exact match to $30,000. The other entries in F13:F17 all display #N/A because none of the other incomes in D13:D17 have an exact match in the first column of the table range. Given a product ID, how can I look up the product’s price? Often, the first column of a table range does not consist of numbers in ascending order. For example, the first column of the table range might list product ID codes or employee names. In my experience, I’ve found that many people don’t know how to deal with lookup functions when the first column of the table range does not consist of numbers in ascending order. In these situations, you need to remember only one simple rule: use False as the value of the range lookup argument. Here’s an example. How do you write a formula that will take a product ID code and return the product price? . Md. Abdullah Al Mahmud Senior Lecturer Manarat International University Many people would enter the formula as I have in cell I18: VLOOKUP (H18, Lookup2,2) . However, note that when you omit the fourth argument (the range lookup argument), the value is assumed to be True. Because the product IDs in the table range Lookup2 (H11:I15) are not listed in alphabetical order, an incorrect price ($3.50) is returned. If we enter the formula VLOOKUP (H18, Lookup2, 2,False) in cell I18 , the correct price ($5.20) is returned. You would also use False in a formula designed to find an employee’s salary using the employee’s last name or ID number. Suppose that a product’s price changes over time. I know the date the product was sold. How can I write a formula to compute the product’s price? Suppose the price of a product depends on the date the product was sold. How can you use a lookup function in a formula that will pick up the correct product price? More specifically, suppose the price of a product is as shown in the following table. Date sold Price January-April 2005 $98 May-August 2005 $105 September-December 2005 $112 We’ll write a formula to determine the correct product price for any date on which the product is sold in the year 2005. For variety, we’ll use an HLOOKUP function. I’ve placed the dates when the price changes in the first row of the table range. Md. Abdullah Al Mahmud Senior Lecturer Manarat International University I copied from C8 to C9:C11 the formula HLOOKUP (B8, lookup, 2, TRUE). This formula tries to match the dates in column B with the first row of the range B2:D3. At any date between 1/1/05 and 4/30/05, the lookup function will stop at 1/1/05 and return the price in B3; for any date between 5/01/05 and 7/31/05, the lookup stops at 5/1/05 and returns the price in C3; and for any date later than 8/01/05, the lookup stops at 8/01/05 and returns the price in D3. Md. Abdullah Al Mahmud Senior Lecturer Manarat International University