Lesson 4.3: Using the VLOOKUP Function

advertisement
Lesson 4.3: Using the VLOOKUP Function
Excel 2003 provides two lookup functions that you can use to quickly retrieve information from
a table. The functions are called HLOOKUP (horizontal lookup) and VLOOKUP (vertical lookup).
They are similar to database functions in the sense that they retrieve values based on reference
data. They are, however, different from database functions in the way they operate.
The VLOOKUP function will look in the leftmost column of a table for a value you specify. When
it finds the value you specified, it will return a value that is located in the same row, a specified
number of columns into the table. It is called VLOOKUP because it looks vertically down a
column for a match, and then retrieves data from across the row. HLOOKUP is similar, but it will
look horizontally across the upper row of your table, and then retrieve data from down a
column.
In this lesson, you will learn how to use the VLOOKUP function to find data, how to limit the
VLOOKUP value to an exact match, and how to use the VLOOKUP function to find the closest
match. Finally, in the step by step exercise, you will practice using the VLOOKUP function
Step-By-Step
In this exercise you will practice using the VLOOKUP function.
1. To begin, start Excel 2007and open the file named PracticeExercise4.3 in your exercise files
folder.
2. When the workbook opens, it should look like the following.
Here you have an inventory table with item numbers and the corresponding price of each
item. Your goal in this exercise is to construct a VLOOKUP function so that the Item Price
will be automatically entered into column C (price per unit) when the item number is
entered into column A (Product). The Item Price can then be multiplied with the Quantity
ordered to get a total price for column D.
3. To begin, work out the details of the VLOOKUP function. The column that we want the
ticket prices to appear in is column C, so this is where we will place the function. The
values used to match the lookup table values will be in column A, and the item prices that
we are retrieving are in the 2nd column of the lookup table (Inventory).
The lookup table has the defined name Price, so your VLOOKUP function should look like
VLOOKUP(A2,PRICE,2), and be placed in cell C2.
4. If you activate cell C2 and enter =VLOOKUP(A2,PRICE,2) into the formula bar, your
worksheet should look like this.
Notice that C2 has a #N/A error because there is no value entered into cell A2 for the
function to use as a match. If you activate cell A2 and type 10202, you will see the item
price entered automatically in cell C2.
5. If you click on cell D2, you will see the formula =B2*C2 in the formula bar. This will
calculate the Total Price based on the price per item and the quantity of items sold.
Use your mouse to AutoFill cell D2 down nine cells and cell C2 down nine cells. Because
you relative references were used, the formula in D2 and the VLOOKUP function in C2 will
adjust accordingly for the cells they are filled into.
6. Your worksheet should look like this.
The #N/A errors are visible because there are no item numbers entered in column A for
those rows. When you enter an item number in column A, the VLOOKUP function will look
up the corresponding price from the Inventory table. When you enter a quantity in column
B, the total price will be calculated for the corresponding cell in column D.
7. Type 10204, 10207, 10209, and 10215 in cells A3:A6 respectively. Then enter 3, 3, 5, 1, and
10 in cells B2:B6 respectively. Your worksheet should now look like the following.
8. You may have noticed that you omitted the last argument in the VLOOKUP function when
you entered it. Because you left this argument out, the default argument of true is being
used. This is why VLOOKUP found a price for item number 10215 even though 10215 is not
in the inventory table. If the last argument in the function is true, VLOOKUP will find the
closest match. (True finds the most likely match; False will find only an exact match).
You can easily have lookup tables that are much larger than the inventory table used in
this exercise. If your lookup table has many columns, you just have to make sure you
specify the correct column number in your function arguments so that the appropriate
data will be retrieved.
In this exercise, the lookup table and the data table that uses the function are on the same
worksheet. The lookup table can just as easily be located on an entirely different
worksheet if that is more convenient.
9. Save your work and exit Excel.
Download