CSE 2337 Chapter 5 Retrieving Data with Lookups 1 Objectives • VLOOKUP • HLOOKUP • LOOKUP • INDEX 2 Lookup Tables in Excel • Lookup Table – A data list that categorizes values that are needed elsewhere • Better than nested ifs – extensibility • Better than hard coding - redundancy 3 Vertical Lookups 4 Example Vertical Lookup Tables Unit Pricing Qty $/each 0 $ 2.45 60 $ 2.27 120 $ 2.12 240 $ 2.00 Goal: Given a quantity, what is the unit price?? Shipping Costs Method $/each Truck $ 0.25 Rail $ 0.20 Ship $ 0.15 Customer $ - Goal: Given a method, what is the cost per unit?? 5 VLOOKUP • VLOOKUP function – Most effective and flexible way to retrieve data organized in columns – Searches specified part of worksheet for data, starting with the first column – =VLOOKUP(lookup_value,table_array, col_index_num,range_lookup) – Must specify col_index_num 6 VLOOKUP Arguments 7 VLOOKUP Algorithm 8 Exact Match • If using lookup type of FALSE, VLOOKUP only looks for an exact match • Sorted ascending not required • VLOOKUP function displays #N/A in cell if exact match is not found 9 Exact Match 10 Horizontal Lookups 11 Horizontal Lookup Shipping Discount Total Price: 0 Shipping Discount 1 0% Shipping Discount 2 0% 300 15% 10% 1000 25% 15% 12 HLOOKUP • HLOOKUP function – Looks up a value by testing for a criterion across a row – HLOOKUP(lookup_value,table_array, row_index_num,range_lookup) – Must specify row_index_num • Similar to VLOOKUP function, except that values are tested across the row • First row of lookup table must be sorted in ascending order 13 14 HLOOKUP 15 Other Worksheets • Use VLOOKUP and HLOOK for accessing data on other worksheets – Data may be placed on separate sheet when it is a long table 16 Separate Sheets 17 LOOKUP • Use LOOKUP function – Looks up the greatest value that does not exceed a specified value anywhere in a table or range – Can retrieve data from a lookup table with a vertical or horizontal orientation – Uses only a TRUE lookup type; column or row containing lookup values must be in ascending order – LOOKUP(lookup_value,lookup_vector, result_vector) 18 19 20 Index Function • Use INDEX function with two- or threedimensional tables – Returns the value in a table based on row and column numbers that you specify – =INDEX(reference,row_num,column_num, area_num) – Has several guidelines – See p 305 of text… 21 22 Index 23