REFERENCE FUNCTIONS In the previous chapter it was shown how IF functions can be nested to decide between a set of possible outcomes. However, it quickly becomes apparent after two or three levels of nesting this method can be cumbersome and time consuming to implement. For example, in Figure 1 cells B1:C5 contain a grading scale. A student’s letter grade is determined by that scale. Percentages lower than 60% will receive an E, percentages of at least 60% but less than 70% will receive a D, percentages of at least 70% but less than 80% will receive a B, and percentages of 90% or more will receive an A. In cells A7:B10 is a list of students and their percentage grade. In cell C7, we want to write a formula to determine James Brown’s letter grade. This formula should be copied down the column to determine the letter grades for each student on the list. We could use the nested IF below to accomplish this to create this formula. Figure 1 =IF(B7>=B$5,C$5,IF(B7>=B$4,C$4,IF(B7>=B$3,C$3,IF(B7>=B$2,C$2,C$1)))) While this function works, it’s somewhat tedious to write and if more levels are added, i.e. A-, B+, B-, etc., the formula will be difficult to maintain. Furthermore, the IF function is limited to 64 nested IFs. A more efficient method of solving this problem is to use a reference function which is designed to look up values from a given list and return a corresponding value. In this class we will learn about the VLOOKUP and HLOOKUP functions. Page 1 THE VLOOKUP FUNCTION The VLOOKUP function searches for a value in the first column of a vertical table array and returns a value in the same row from another column in the table array. The syntax of the VLOOKUP function is: VLOOKUP (lookup_value,table_array,col_index_num,[range]) The arguments of the VLOOKUP Function are as follows: Lookup_value: The value to search in the first column of the table array (list). This value can be a number, text, logical value, or a name or reference that refers to a value. It can be thought of as the criteria you wish to match. Table Array: The range of cells that contains the data Col index num: The column number in the table_array that should be returned if a match is found. A col_index_num of 1 returns the value in the first column of the table array. A col_index_num of 2 returns the value in the second column of the table array, etc. Range: Specifies whether you want VLOOKUP to find an exact match or an approximate match. True: Finds an exact match in the table_array or finds the lowest value without going over o Leftmost column should contain the table range o Rightmost column should contain the value o Leftmost column must be in ascending order o Beginning value in table array must be the lowest value of a lookup value or the error message #N/A will be displayed False: Finds an exact match in your table array o Leftmost column should contain the table range o Rightmost column should contain the value o Finds the exact match in your table array o If no match is found, the error message #N/A is displayed Page 2 VLOOKUP with a range argument of True In cell C7 of figure 1 we wrote a formula using a nested IF to obtain the correct letter grade for each student. Now we will use a VLOOKUP function to obtain the letter grade. =VLOOKUP(B7,B$1:C$5,2,TRUE) The first argument in the function is the look up value, which in this case is 55%. The second argument is the table array or list of values to search.(Cells B1:C5) The third argument specifies the column to return if a “match” is found. Because the final argument is true, Excel will find an exact match or the lowest value without going over. The function will try to match the value “55%” to the entries in the table array B1:C5. Because we have True as the range argument if “55% does not appear in the table array it will find the “closest” match without going over. In this case the closest value is “0%”, so the corresponding value in column 2 of the table array (“E”) will be displayed. VLOOKUP with a range argument of False If we type in the above formula in cell C7, but change the range argument to false we would obtain different results because we are now looking for an exact match. Cell C7 would now display a #N/A. =VLOOKUP(B7,B$1:C$5,2,FALSE) Page 3 THE HLOOKUP FUNCTION The HLOOKUP function is used if the table array is in a horizontal format. In Figure 2 cells A1:E2 contain a tax schedule. Taxes are calculated on a progressive scale: incomes under $25,000 have no tax (0%), incomes of at least $25,000 but less than $50,000 are taxed at 10% of income, incomes of at least $50,000 but less than $100,000 are taxed at 15% of income, and incomes of $100,00 or more are taxed at the rate of 23%. In cell A5:C10 is a list of employees and their taxable income. In cell C6, write a formula to determine the income tax for the employee Jonas, such that it can be copied down the column. A 1 2 3 4 5 6 7 8 9 10 Income: Rate: Employee: Jonas Maria Zev Marta Alex B $ $ $ $ $ $ C Income Tax Rates: $ 25,000 $ 0% 10% Income 73,250 27,350 1,094 92,125 48,999 $ $ $ $ $ D 50,000 $ 15% E 100,000 23% Tax Due 10,987.50 2,735.00 13,818.75 4,899.90 =B6*HLOOKUP(B6,B$2:E$3,2,TRUE) This formula will multiply the value $73,250, the income in cell B6, by corresponding tax rate. To find the corresponding tax rate the LOOKUP function will match the value in cell B6, the first argument of the function, to the values in the range B2:E3, as specified in the second argument of the function. Once a match is found, the corresponding value in the 2nd row will be returned. Page 4 A MORE COMPLEX EXAMPLE: A B C 1 Price List: 2 Shipping $/lb. $ 0.50 3 4 Item No. $/each Weight (lbs) 5 124 $ 332 19 6 125 $ 55 5 7 126 $ 224 14 8 127 $ 483 16 9 128 $ 41 2 10 129 $ 83 20 11 130 $ 9 14 12 131 $ 268 2 13 132 $ 139 17 14 133 $ 52 11 15 134 $ 474 18 16 135 $ 294 20 17 136 $ 411 3 A 1 2 3 4 5 6 7 B C Customer Orders Customer Item# Qty Zael 134 3 Orwich 127 4 Wagma 104 2 PT 134 10 PT 128 1 D Total Price $ 1,449.00 $ 1,964.00 #N/A $ 4,830.00 $ 42.00 Orders Figure 2 Pricing A more complex example is given in Figure 3. This workbook example contains two worksheets: Sheet Pricing contains a list of items numbers for various items you have for sale on E-Bay, their corresponding cost per unit, and weight in pounds (lbs) per unit. Also given on this worksheet is the shipping cost per pound. Worksheet Orders contains a list of recent orders, including the item number being ordered and quantity. To complete the Orders worksheet column D requires a formula to total the cost of this customer’s order, including the cost of all items and shipping. Step 1 - Understand the Problem. Mathematically the customer’s order total is Quantity * (Price/Item + Shipping Cost/Item). Since just the shipping weight in pounds (lbs) and cost per pound ($/lb) are given, this formula can be further expanded to the following: Quantity * (Price/Item + Weight/Item * $/lb to ship) Step 2 – Translate to Excel Syntax. If this formula is written directly, just substituting the appropriate values for Zael’s order, then the cost per unit and weight would not necessary copy down correctly. Both of these values are variable, but change by item number and not by relative position. Thus a LOOKUP will be required. Can a single LOOKUP function return both the cost per item and shipping weight? No, the LOOKUP function can return only one value at a time. So each variable, price/item, and weight/item, will require a separate LOOKUP. Page 5 In pseudo code this formula will be: Quantity * (Lookup(item number, range of item numbers an prices, column number to return) + Lookup(item number, range of item numbers and weights, column number to return) * $/lb to ship). The cell references are as follows: o o o o o o The quantity is on the same worksheet as the formula (Orders) in cell C3. The item number to be looked up is on the same worksheet as the formula in cell B3. The range containing the item numbers to lookup is on sheet Pricing! in cells A5 through A17. The range containing the corresponding prices is on sheet Pricing! in cells B5 through B17. The range containing the corresponding weight is on sheet Pricing! in cells B5 through B17. The cost per pound to ship is on sheet Pricing in cell C2. Applying these cell references the formula is: =C3*(VLOOKUP(B3,Pricing!A5:B17,2,true) + VLOOKUP(B3,Pricing!A5:C17,3,true)*Pricing!C2) Step 3 – Copying. In addition to knowing that a LOOKUP function is needed so that the formula can be copied, some of the arguments of the LOOKUP and the other operands must be considered. The Quantity and Item Number will change relatively as the formula is copied down the column so thus C3 and B3 are relative references. However, the table array does not change when copied and therefore must contain an absolute row reference. Lastly, the $/lb for shipping in cell Pricing!C2 needs to be considered. Again this value does not change when the formula is copied and thus requires an absolute row reference. The final formula will be: =C3*(VLOOKUP(B3,Pricing!A$5:B$17,2,true) + VLOOKUP(B3,Pricing!A$5:C$17,3,true)*Pricing!C$2,true) One point worth noting is the value that results in cell Orders!D5 is #N/A. This is an error message indicating that there is no corresponding answer. The reason for this is the lookup_value 104 is below the smallest value in the table array list (124). This error can be corrected either by changing the item number on the order to an existing item number from the list, or by adding item 104 to the top of the price list. A more serious error would be using an item number that is between the values in the lookup range or greater than those values and a range of true. For example if the value 138 is used as an item number, Excel will return the greatest value on the list that does not exceed 138. Hence the pricing information for item 136 will be returned and not an error message. This could be remedied by changing the range argument to False. Page 6 PRACTICE PROBLEM 6.1 REFERENCE FUNCTIONS: PRICING COPIES A 1 2 3 4 5 6 7 8 B The Lookup Table for Sliding-Scale Prices: Number of copies Price per copy 1 10 100 200 500 1000 PRICING 0.07 0.06 0.05 0.04 0.03 0.02 9 10 11 Copies Made: Total Cost: 12 DELIVERY 3 10 200 1001 13 14 15 16 1. Write a formula in cell B13, which can be copied down the column, to determine the total cost of making copies. The number of copies to be made is listed in Column. A. 2. The company also offers delivery. Their delivery charges are as follows: For orders under $5, there is a $5 delivery fee. Orders at least $5 but smaller than $20 have a $7 delivery fee, Orders of $20 or more are delivered free of charge. Set up a reference table on the Delivery! worksheet so that you can write a reference function to calculate the delivery cost for each order. Organize the table in a horizontal format. 3. Write a formula that can be copied down for Delivery Cost. (cell PRICING!C13). Use the reference table you created in question 2. Page 7 PRACTICE PROBLEM 6.2 GRADE BOOK scores Grades1 Grades2 Page 8 1. Write the formula in cell scores!G2, which can be copied down, to determine the percent of total possible points Mickey Mouse earned. 2. Write a formula in cell scores!H2, which can be copied down the column, to determine Mr. Mouse’s final letter grade. Use a reference function and the grade listing on sheet Grades1 when solving this problem. 3. Write another formula in cell scores!H2, which can be copied down the column, to determine Mr. Mouse’s final letter grade. This time use a reference function and the grade listing on sheet Grades2 when solving this problem. 4. How could you solve this problem without using a reference function? Page 9 PRACTICE PROBLEM 6.3 SALES ORDER PRICING A 1 2 3 4 5 6 7 8 9 B Company ABC Inc. DEF Inc. GHI Corp. LLL Ind. NYPD Inc. PQ Ltd. RFG Inc. XYZ Inc. City Columbus Philadelphia Scranton Long Island Brooklyn Warren Columbus Cleveland Disc C D E Discount Discount A B State OH 5% 8% PA 5% 10% PA 7% 10% NY 7% 15% NY 5% 12% OH 5% 10% OH 7% 8% OH 6% 8% A B C D Total cost Customer Sales w/o Amount Ship Cost Discount 1 Name E F Total Cost Shipping Cost with Discount >$25 2 ABC Inc. 345 $ 34.50 $ 379.50 $ 349.14 TRUE 3 DEF Inc. 92 $ 18.40 $ 110.40 $ 104.88 FALSE 4 ABC Inc. $ 2,100.00 $ 1,932.00 TRUE 32 $ 6.40 $ 38.40 $ 36.48 FALSE 6 RFG Inc. 99 $ 19.80 $ 118.80 $ 110.48 FALSE 7 DEF Inc. 101 $ 10.10 $ 111.10 $ 105.55 FALSE $ 3,360.00 $ 3,091.20 TRUE 5 NYPD Inc. 8 ABC Inc. 2000 $ 100.00 3200 $ 160.00 sales A B 1 Shipping Table #1 sale percent shipping 2 amount 3 1000 5% 4 100 10% 5 1 20% A 1 Shipping Table #2 2 sales amount 3 percent shipping B C D 1 100 1000 20% 10% 5% Ship2 Ship1 Above are some worksheets you have prepared to keep track of customer sales. The worksheet sales! lists each sale made by your company. It will be your job to calculate the shipping costs and factor in the appropriate discounted sales price for each sale. Shipping charges from $1 to $99.99 are 20% of the total sale, from $100 to $999.99 shipping charges are 10% of the total sale and over $1000 are 5% of the total sale. Each company has negotiated different discount rates for discount types A and B. Discount A is applied to sales under $200. Discount B is applied to sales of $200 and above. Discounts are calculated based total cost w/o discount (sales plus shipping) – that you will calculate in column D. 1. If you were to use a reference function in cell sales!C2 to calculate the shipping cost of the $345 sale (cell sales!B2), would you want to use the data on sheet ship1! as the reference table or the data on sheet ship2! as the reference table and why? 2. Write the formula in cell sales!C2, which can be copied down the column, to calculate the shipping cost of the corresponding sale in that row. Assume you will be copying this formula down the column. Page 10 3. Without using a reference function, write another formula which could be written in cell sales!C2 to calculate the cost of the shipping. Assume you can copy this formula down the column. 4.Write a formula in cell sales!D2, which can be copied down the column, to calculate the total cost before discounts (list price plus shipping). 5. Write a formula in cell sales!E2, which can be copied down the column, to determine the discounted price of the order. Remember that discounts are based on a company’s negotiated discount rate, and the total cost of the order (discount A for <$200, discount B for >=$200). 6. Your competitor charges $25 flat rate per order. Write a formula (True/False) in cell sales!F2 to determine if your customer paid more than $25 for his/her shipping. Page 11 PRACTICE PROBLEM 6.4 The three spreadsheets on the following page are all worksheets in a single workbook. The worksheet named Prod! Lists product information (shipping time, cost, shipping cost) by product number. The worksheet named ship! is a table of shipping costs for the corresponding product costs. The worksheet named Order! is a list of customer orders (one item type per order), and the order quantity. 1. (1 point) Write a formula in cell Prod!C20 to automatically determine the item number of the product with the highest cost. __________________________________________________________________ 2. (1 point) Write a formula in cell Prod!D3, using a reference function, to calculate the shipping cost for product 0001. Write the formula so that it can be copied down the column. __________________________________________________________________ 3. (2 points) Write a formula in cell Order!D3 to calculate the total cost of this order (product costs and shipping costs). Write the formula so that it can be copied down the column. __________________________________________________________________ ________________________________________ 4. (2 points) Write a formula in cell order!G3 to determine (true/false) if the shipment was late. A shipment is late if it took more that the required number of shipping days to be received. Write the formula so that it can be copied down the column. __________________________________________________________________ 5. (2 points) Write a formula in cell Order!G20 to determine the customer name of the customer with the lowest value order. __________________________________________________________________ 6. (2 points) Write a formula in cell Prod!D3, using a nested if function, to calculate the shipping cost for product 0001. Write the formula so that it can be copied down the column. __________________________________________________________________ _________________________________________________________________ A 1 2 Prod 3 4 5 6 7 8 9 10 11 12 13 14 15 B Product Information Shipping Product Days ID Required 0001 0012 0023 0034 0045 0067 0089 0032 0043 0010 0011 0078 0013 Ship 1 4 7 3 5 1 7 3 4 2 7 6 3 A Product Cost 3 Shipping Cost B Product Costs $ 25 $ 65 $ 85 $ 48 $ 32 $ 64 $ 189 $ 76 $ 58 $ 15 $ 97 $ 64 $ 89 Shipping Cost per item $ 3 $ 5 $ 7 $ 3 $ 3 $ 5 $ 9 $ 7 $ 5 $ 3 $ 7 $ 5 $ 7 Product ID 0001 0012 0023 0034 0045 0067 0089 0032 0043 0010 0011 0078 0013 C C D D E F E 75 $ 7 $ 100 9 G H Customer Orders Product Number 3 Fraiser 0045 4 Johnson 0067 5 Jordon 0001 6 Chamberlain 0012 7 Reed 0023 8 Monroe 0034 9 Bird 0089 10 Barry 0032 11 Lucas 0043 12 Bradley 0010 13 DeBusher 0011 14 Rodman 0078 15 Malone 0013 2 E Shipping Costs 0 $ 50 $ $ 3 $ 5 $ 2 A D B 1 1 C Customer Order QTY 1 1 1 1 2 3 2 3 1 1 4 1 1 Total Order Ship Receipt Cost Date Date $ 35 2/8/98 2/12/98 $ 69 2/8/98 2/16/98 $ 28 2/8/98 2/9/98 $ 70 2/8/98 2/15/98 $ 184 2/8/98 2/9/98 $ 153 2/8/98 2/11/98 $ 396 2/8/98 3/1/98 $ 249 2/8/98 2/14/98 $ 63 2/8/98 2/13/98 $ 18 2/8/98 2/15/98 $ 416 2/8/98 2/10/98 $ 69 2/8/98 2/9/98 $ 96 2/8/98 2/21/98 Late? FALSE TRUE FALSE TRUE FALSE FALSE TRUE TRUE TRUE TRUE FALSE FALSE TRUE Customer Fraiser Johnson Jordon Chamberlain Reed Monroe Bird Barry Lucas Bradley DeBusher Rodman Malone