Using HLOOKUP and VLOOKUP Functions

Transcription

Using HLOOKUP and VLOOKUP Functions
28
Microsoft Excel Version 2002 Step by Step Courseware Instructor Guide
Using HLOOKUP and VLOOKUP Functions
Ex2002e-4-2
Excel provides two lookup functions that can be used to retrieve information stored in a table: VLOOKUP and HLOOKUP. A lookup function is a
good way to search for and insert a value in a cell when the desired value
is stored elsewhere in the worksheet. A lookup function searches rows or
columns in a specified range (called a lookup table in Excel). Both functions use the same arguments (with one exception, as explained below).
An argument refers to the values that a function uses to perform operations or calculations. The type of argument a function uses is specific to
the function.
The VLOOKUP and HLOOKUP functions locate a specific value in a
table (any row-and-column range or a named range). This value might be
the name of a person, for example. These functions retrieve corresponding
information from the table that relates to this value (such as the person’s
birth date) and place the data into the active cell. You indicate how many
rows or columns that the desired value is from the lookup value.
For example, the recreation director at Adventure Works keeps a table of
names, room numbers, and birthdays for the children of guests at the resort. Each day, she uses the VLOOKUP function to search for that day’s
date in the table and display the name and room number of each child
whose birthday is that day. She then uses this information to send birthday
gifts to the appropriate rooms.
tip
The LOOKUP function uses specific criteria to search for a value in a single
column or row of a table, and then it displays a value from the corresponding
position in a second table. For example, suppose you had a worksheet in
which one column contains a room number and the second column contains the room type. You could perform a lookup for room 102 in the first
column. Excel would then return the room type stored in the corresponding column. For example, a lookup for room 102, located in the third row
of the first column, would return Single Room/Queen Bed from the third
row of the second column. The chief advantage of the LOOKUP function
is that you do not need to know the row or column position where the
desired lookup information is stored.
The HLOOKUP function looks in rows (a horizontal lookup), and the
VLOOKUP function looks in columns (a vertical lookup). Each function
can use up to four arguments. All of the arguments below are required
except for range_lookup:
HLOOKUP(lookup_value,table_array,row_index_num,range_lookup)
VLOOKUP(lookup_value,table_array,col_index_num,range_lookup)
Lesson 2 Working with Data and Named Ranges 29
Argument
Description
lookup_value
The value to be found in the row or the column.
The lookup value can be a constant value, a text
contact enclosed in quotation marks, or the address or name of a cell that contains a numeric or
text constant.
The table of information in which data is looked
up. This can be the row-and-column coordinates
of the table or the name of a range.
The numeric position of the row that is to be
searched (used only for HLOOKUP).
The numeric position of the column that is to be
searched (used only for VLOOKUP).
If the function is to return the nearest value, even
if there is no match, this value should be set to
TRUE. If an exact match is required, this value
should be set to FALSE.
table_array
row_index_num
col_index_num
range_lookup
To better understand the use of VLOOKUP and HLOOKUP, briefly review
the following lookup table that the sales manager of Adventure Works created to formulate end-of-year bonuses for the sales staff. Years is column 1
in the lookup table, Standard Bonus is column 2 in the lookup table, and
Excellence Award is column 3. “Years, Standard Bonus, Excellence
Award” is row 1 of the lookup table:
Row
Row
Row
Row
Row
Row
Row
Row
Row
1
2
3
4
5
6
7
8
9
Column1
Column2
Column 3
Years
1
3
5
7
9
11
13
15
Bonus Schedule
Standard Bonus
$50.00
$75.00
$100.00
$250.00
$500.00
$750.00
$1,000.00
$1,500.00
Excellence Award
$200.00
$300.00
$400.00
$1,000.00
$2,000.00
$3,000.00
$4,000.00
$6,000.00
Now review the following lookups and their results:
●
=VLOOKUP(D7,Bonustable,2) Result: $1,000
VLOOKUP looks in cell D7 of the above worksheet and finds the
value 13 stored there. The function then looks in the Bonustable
range for a matching value and finds it in the second-to-last row.
The function then retrieves the value ($1,000.00) in column 2 of
the same row and places the value in the active cell.
●
=VLOOKUP(12,Bonustable,2,TRUE) Result: $750.00
VLOOKUP looks for the value 12 in the Bonustable range and
does not find it. However, because the Range_lookup argument is
set to TRUE, the function finds the closest (next lower) value, 11
30
Microsoft Excel Version 2002 Step by Step Courseware Instructor Guide
in this case. The function then returns the value ($750.00) in
column 2 of the same row and places the value in the active cell.
Show slides 02 Ranges 12–13,
Using the HLOOKUP and
VLOOKUP Functions.
Be careful not to insert any spaces
when you type this formula.
Bonustable has already been defined
as a range name for cells A20:C28.
You can verify this by clicking the
Name down arrow and clicking
Bonustable. Arguments used in a
VLOOKUP or HLOOKUP function are
not case-sensitive, so you can type
them in either uppercase, lowercase,
or any combination of uppercase and
lowercase characters. Also the
VLOOKUP and HLOOKUP function
names are not case-sensitive.
●
=VLOOKUP(14,Bonustable,2,FALSE) Result: #N/A
VLOOKUP looks for the value 14 in the Bonustable range and
does not find it. Because the Range_lookup argument is set to
FALSE (which means an exact match is required), the function
places the #N/A error value in the active cell to indicate that no
match existed.
●
=HLOOKUP(“StandardBonus”,Bonustable,4,FALSE) Result:
$100.00
HLOOKUP looks for the value “Standard Bonus” in the
Bonustable range, finds the value in column 2, and then returns
the value ($100.00) in row 4 of the column.
In this exercise, you use the VLOOKUP function to calculate the yearly
bonus, based on years of service, for an Adventure Works employee
named Benson. Then you use the fill handle to copy the VLOOKUP function to cells for other employees. You also use the HLOOKUP function to
return the “Excellence Award” value in a specified row of a lookup table.
1
Open the Employee History Bonus workbook.
2
Click cell E7, and type =vlookup(d7,bonustable,2).
Cell D7 contains the number of years worked, which is the lookup
value. VLOOKUP looks for a match to the D7 value in the bonustable
range (A20:C28), and then it looks in the second column, where it
finds the match.
3
Press Enter.
The result of the function ($1000.00) appears in cell E7.
4
Click cell E7, and drag the fill handle down to cell E15.
The bonuses for the other employees are calculated.
You can also access the VLOOKUP
and HLOOKUP functions from the
Function Arguments dialog box,
which you open by clicking Function
on the Insert menu or by clicking the
Insert Function button next to the
Formula Bar.
5
Click cell F7.
Lesson 2 Working with Data and Named Ranges 31
Explain that if range_lookup is
not set to false, Excel uses the
default value true for the
range_lookup argument.
However, a range_lookup
argument of true works only if
the values in the first row of
the table (Years, Bonus, and
Excellence Award) are in
ascending alphabetical order,
which they are not. The
range_lookup argument must
be set to false for the lookup in
step 6 to work properly.
6
Type =hlookup(“excellence award”,bonustable,8,false), and press
Enter.
Excel searches for the text string “excellence award” in the first row
of the bonustable range and locates it in column C. Then Excel retrieves the value ($4,000.00) in cell C8 and places it in the active cell.
7
Save and close the workbook.
tip
If you have trouble remembering the syntax for an HLOOKUP or VLOOKUP
function, you can use the Function Arguments dialog box to help you
enter the lookup information. On the Insert menu, click Function or click the
Insert Function button next to the Formula bar to display a list of categories and function names. In the categories list, click Lookup & Reference. In
the function list, click VLOOKUP or HLOOKUP, and click OK. Excel will then
prompt you for the information required to perform the lookup.