You are on page 1of 20

VLookup and HLookup

in Microsoft Excel
VLookup
 Searches for a Collingsworth 1
value in the first Comanche 1
column of a table Dallas 16
array and returns a Denton 6
value in the same El Paso 4
row from another Ellis 2
column in the Fannin 1
table array. Fort Bend 1
Galveston 4
HLookup
 Searches for a value in the top row of a table or an
array of values, and then returns a value in the same
column from a row you specify in the table or array.

El Paso Ellis Fannin Fort Bend Galveston


4 2 1 1 4
Syntax
 VLOOKUP
(lookup_value, What you’re looking for
table_array, Where you’re going to look
col_index_num, What you want to know
range_lookup) Is ‘close’ good enough?

HLookup has the same syntax, with “row_index_num”


instead of “col_index_num”. We’ll talk about VLookup
today.
lookup_value
 The value to search for in the first
column of the table array.
 If lookup_value is smaller than the
smallest value in the first column of
the table array, VLookup returns the
#N/A error value.
table_array
 Data in columns.
 If the table is not sorted in ascending order
on the first column, the function can give
unpredictable results.
 Use ‘absolute’ references. (Put a $ before
the letters and numbers in the references, i.e.
$A$2:$B$255)
 Do not include column headers
col_index_num
 The column number in the table array
from which the matching value must
be returned.
 A col_index_num of 1 returns the
value in the first column in the table
array; col_index_num of 2 returns the
value in the second column, and so on.
range_lookup
 A logical (True/False) value that
specifies whether you want VLookup
to find an approximate match or an
exact match.
 Defaults to “True”.

 You will almost always want it to be


“False”.
What would happen if Collingsworth 1
VLookup were looking for Comanche 1
Dallam County in this
table and returning the Dallas 16
second column? Denton 6
El Paso 4
With range_lookup left as “True”, the value
for Comanche County will be returned. Ellis 2
Fannin 1
With range_lookup set to “False” an error
message will show that Dallam County was
Fort Bend 1
not found. Galveston 4
We can deal with error messages!!
SO WHAT ?!?!?!
Why would we submit ourselves
to such a complicated function?
Combine data tables
Case Population
Count by by County
County

Incidence
Rate by
County
Example

Two worksheets:
“Case Count” has a list of case counts by county.
“County Population” has a list of all counties
with their populations.
County

=VLOOKUP(A2,'Case Count'!
$A$1:$B$58,2,FALSE)
This formula will return an error for any county
that has no cases.
What we really want is 0

This is where it gets complicated…


Handling Errors
 This is where I lose people…
 If the original formula gives an error, we
want a zero. Otherwise we want the result
of the original formula.
 The Excel function IfError() will let us
choose between the value of a function
(vlookup) or another value if the function
returns an error.
 =IfError([original formula], 0)
 =IFERROR(VLOOKUP(A2,'Case
Count'!$A$1:$B$58,2,FALSE),0)
Where might we use True
for the range_lookup
parameter?
To group numeric values into ranges.
What would happen if
VLookup were looking for 00 - 4
55 - 9
24 in this table and
1010 - 17
returning the second 1818 - 24
column? 2525 - 34
With range_lookup set to “False” an error
3535 - 44
message will show that 24 was not found.
4545 - 54
5555 - 64
We don’t want an error message, or a 6565+
default value. We want an ANSWER!
What would happen if
VLookup were looking for 0 0-4
5 5-9
24 in this table and
10 10 - 17
returning the second 18 18 - 24
column?
With range_lookup set to “True”, 25 25 - 34
Vlookup will start at the top of the table, 35 35 - 44
work its way down to the last value in 45 45 - 54
column 1 that is earlier in the sort than
the lookup_value.
55 55 - 64
65 65+
The table MUST be sorted by the
first column!! You will get wrong
answers, and it may not be obvious
that they are wrong.

In this version of Vlookup, there are


no error messages for missing values.
Investigation ID2 Age Reported Birth Time Event Date Age Age Group
CAS482442134TX01 49 7/8/1966 11/21/2015 49 45 - 54
CAS482247044TX01 53 7/18/1961 2/14/2015 53 45 - 54
CAS482247047TX01 59 7/22/1955 3/5/2015 59 55 - 64
CAS482247055TX01 8 6/18/2014 3/3/2015 0 0-4
CAS482247057TX01 47 5/19/1967 3/5/2015 47 45 - 54
CAS482247060TX01 1 8/14/2013 3/3/2015 1 0-4

You might also like