You are on page 1of 10

COMPUTER DATA MANAGEMENT – BSMLS1E

LESSON 5
LOOKUP FUNCTION
WHAT IS VLOOKUP? 4. The fourth argument tells VLOOKUP whether to look for
• VLOOKUP lets you search for specific information approximate matches, and it can be either TRUE or FALSE. If it
in your spreadsheet. For example, if you have a list of is TRUE, it will look for approximate matches. Generally, this is
products with prices, you could search for the price of only useful if the first column has numerical values that have
a specific item. been sorted. Because we're only looking for exact matches, the
HOW TO USE VLOOKUP FUNCTION? fourth argument should be FALSE. This is our last argument, so
• We're going to use VLOOKUP to find the price of the go ahead and close the parenthesis:
Photo frame. You can probably already see that the =VLOOKUP("Photo frame", A2:B16, 2, FALSE)
price is $9.99, but that's because this is a simple
example. Once you learn how to use VLOOKUP, you'll ❖ That's it! When you press Enter, it should give you the
be able to use it with larger, more complex answer, which is 9.99.
spreadsheets, and that's when it will become truly
useful.

HOW IT WORKS
➢ Let's take a look at how this formula works. It first
searches vertically down the first column (VLOOKUP
is short for vertical lookup). When it finds "Photo frame",
it moves to the second column to find the price.

• We'll add our formula to cell F2, but you can add it to
any blank cell. As with any formula, you'll start with an
equals sign (=). Then type the formula name. Our
arguments will need to be in parentheses, so type an
open parenthesis. So far, it should look like this:
=VLOOKUP(
ADDING ARGUMENTS
Now, we'll add our arguments. The arguments will tell
VLOOKUP what to search for and where to search.
1. The first argument is the name of the item you're searching for, • If we want to find the price of a different item, we can
which in this case is Photo frame. Because the argument is text, just change the first argument:
we'll need to put it in double quotes: =VLOOKUP("T-shirt", A2:B16, 2, FALSE) or
=VLOOKUP("Photo frame" =VLOOKUP("Gift basket", A2:B16, 2, FALSE)
2.The second argument is the cell range that contains the data. EDITING VLOOKUP FORMULA USING
In this example, our data is in A2:B16. As with any function, you'll CELL REFERENCE
need to use a comma to separate each argument: EXAMPLE
=VLOOKUP("Photo frame", A2:B16 • In the previous example, we typed the item name
directly into the VLOOKUP formula. But in the real
It's important to know that VLOOKUP will always search the first world, you'll usually use a cell reference instead. In
column in this range. In this example, it will search column A for this example, we'll type the item name in cell E2, and
"Photo frame". The value that it returns (in this case, the price) will our VLOOKUP formula can then use a cell reference
always need to be to the right of that column. to find information about that product. Then, we can
simply type a new item name into E2 to find any
3.The third argument is the column index number. It's simpler than product we want.
it sounds: The first column in the range is 1, the second column is • We've also added a third column that has the category
2, etc. In this case, we are trying to find the price of the item, and for each item. This will give us the option of finding the
the prices are contained in the second column. This means our third price or category. Here's what the spreadsheet looks
argument will be 2: like so far:
=VLOOKUP("Photo frame", A2:B16, 2

1
COMPUTER DATA MANAGEMENT – BSMLS 1

OUR SPREADSHEET

• Our formula will be similar to The previous example, • If the subtotal for an order is at least $100, the
but we'll need to change the first three arguments. shipping should be free. If not, it should cost $5.99.
Let's start by changing the first argument to a cell We could use the IF function to solve this problem. As
reference (make sure to remove the quotation marks): you may remember from our lesson on Using the IF
=VLOOKUP(E2, A2:B16, 2, FALSE) Function, we can use it to test a condition and then
• To find the category, we'll need to change the second write something in a cell depending on the result. In
and third arguments. First, we'll change the range to this example, the IF function can tell us whether the
A2:C16 so it includes the third column. Next, we'll subtotal is at least $100 and then set the shipping
change the column index number to 3 because our cost depending on the result.
categories are in the third column:
=VLOOKUP(E2, A2:C16, 3, FALSE) WRITING THE FUNCTION
1. The first argument sets up the condition the IF
• When you press Enter, you'll see that the Gift basket is in function will test: In this example, if the value in cell D6
the Gifts category. is greater than or equal to 100. So our first argument
will be D6>=100.
2. The second argument tells the function what to write
if the condition is true. In this example, if the value is
greater than or equal to 100, we want it to write "0" in
the cell. So our second argument will be 0.
3. The third argument tells the function what to write if
the conditionis false: In this example, if the value is
less than 100, we want it to use the value from cell G3
(5.99). So our third argument will be $G$3 (we'll use
• If we want to find the category of a different item, we can an absolute reference here just in case we move this
simply change the item name in E2: formula in the future).
• Here's our function: =IF(D6>=100,0,$G$3). We'll enter
this function into cell D7.

PART 1: VLOOKUP
FREE SHIPPING FORMULA

• If you entered the function correctly, the shipping cost


should change from $5.99 to $0.00. That's because
the Order Subtotal is more than $100.

2
COMPUTER DATA MANAGEMENT – BSMLS 1E

• If you want to test the function, change the value in ARGUMENTS TO USE:
cell C2 from 2 to 1. Because the Order Subtotal is now 1. The first argument tells VLOOKUP what to search
less than $100, the Shipping cost should change from for. In our example, we're searching for the product
$0.00 to $5.99. ID number, which is in cell A2, so our first argument is
A2.
2. The second argument is a cell range that tells
VLOOKUP where to look for the value from our first
argument. In our example, we want it to search for this
value in cell range A2:C13 on the Products
worksheet, so our second argument is
Products!$A$2:$C$13. Note that we're using
absolute references so this range won't change
when we copy the formula to other cells.
3. The third argument is the column index number,
which is simpler than it sounds: The first column in the
cell range from the previous argument is 1, the second
column is 2, and so on. In our example, we're looking
for the Product Name. The names are stored in the
PART 2: VLOOKUP second column of the cell range from the previous
USING PRODUCT ID NUMBER TO FIND PRODUCT argument, so our third argument is 2.
NAME AND PRICE 4. The fourth argument tells VLOOKUP if it should look
• VLOOKUP is a bit more advanced than some for approximate or exact matches. If it is TRUE, it
functions, so you'll need to be familiar with functions will look for approximate matches. If it is FALSE, it
and cell references before you begin. You can review will look for exact matches. In this example, we're only
our lessons on Functions and Relative and looking for exact matches, so our fourth argument is
Absolute Cell References to learn more. FALSE.
HOW IT WORKS
• Before we start using VLOOKUP, it will be helpful to • It's important to know that VLOOKUP will always
know what it does. In our example, it will search for search the leftmost column in the cell range. Since
the Product ID number on the Products worksheet. It our cell range is A2:C13, it will search column A.
first searches vertically down the first column
(VLOOKUP is short for "vertical lookup"). When it finds WRITING THE FUNCTION
the desired product ID, it moves to the right to find Now that we have our arguments, we'll write our function in cell
the product name and product price. B2.

We'll start by typing an equals sign (=), followed by the function


name and an open parenthesis:
=VLOOKUP(

Next, we'll add each of the four arguments, separate them with
commas, then close the parentheses:
=VLOOKUP(A2, Products!$A$2:$C$13, 2, FALSE)

Þ If you entered the function correctly, the product name


should appear: Measuring cups. If you want to test
your function, change the Product ID number in cell
A2 from MEA7879 to CHE7888. The product name
should change from Measuring cups to
Cheesecloth.

3
COMPUTER DATA MANAGEMENT – BSMLS 1E

ADDING THE PRODUCT PRICE


• Next, we also want the Product ID to pull in the
product price, so we'll use the VLOOKUP function
again. Since we're using the same data, this function
will be very similar to the one we just added. In fact, all
we have to do is change the third argument to 3.
This will tell VLOOKUP to pull in the data from the
third column, where the product price is stored:
=VLOOKUP(A2, Products!$A$2:$C$13, 3, FALSE)
Let’s enter our new formula in cell C2:

• But if we look at the function in cell B3, we can see


that the VLOOKUP function is only searching for
information in cell range A2:C13.
We've got our formulas working, so we can just select cells B2
and C2 and. then drag the fill handle down to copy the
formulas to the other rows in the invoice. Now, each row is
using VLOOKUP to find the Product Name and Product Price.

Þ As you can see, the VLOOKUP function isn't


searching the full list of products; it's only
searching down to row 13. So it has no trouble finding
the Ramekin, but it won't be able to find the Zester,
PART 3: VLOOKUP Grater, or Wire whisk.
FIXING BROKEN FORMULA
WHAT ARE WE TRYING TO FIX? Þ To fix this, we'll need to change the second
• Let's see if we can figure out what went wrong. Our argument in VLOOKUP so it searches the entire
coworker added some new products to the Products product list. There are two ways to do this:
worksheet, but they don't work with the existing • Update the cell ranges
VLOOKUP functions. If we look at the Products • Use a table (Microsoft Excel only)
worksheet, we can see that there are a few new
products listed here. The data spans cell range UPDATE CELL RANGES
A2:C16.
• One way to fix this problem is to simply update the cell
ranges in our VLOOKUP functions. Let's try editing the
function in cell B3, where it's stopped working.

4
COMPUTER DATA MANAGEMENT – BSMLS 1E

• We'll edit the function to include all of the cells in the


Products table. And just in case more products are
added later, we'll increase the number of rows in the
cell range to 100. This means we're including a lot of
empty rows, but that's OK—the VLOOKUP function
will still work the same way. So here's our new
function:
=VLOOKUP(A3, Products!$A$2:$C$100, 2, FALSE)

The function will recalculate, and the correct product name


should appear: Zester. We'll also make the same correction to
the remaining VLOOKUP functions in the spreadsheet:

And that's it! Now our spreadsheet's working correctly, and it'll
be harder to break these functions in the future.

USE A TABLE
• Above, we fixed our VLOOKUP function by extending For EXCEL 2003 and EARLIER:
the cell range to row 100. However, if we continue
adding more products, we will eventually break the • Go to Data > List > Create List (tables were known
VLOOKUP function again, so this is not a perfect as lists in Excel 2003 and earlier).
solution. If you're using Microsoft Excel, a much better Next, we'll name the table. In this example, we'll give it the
solution is to use a table. This will ensure that any name: ProductsList. Note that table names cannot contain
new data will be included automatically. spaces.

First, we'll select our product information and then convert it to


a table. Note that this will work a bit differently depending on
which version of Excel you're using:

For EXCEL 2007-2019:

Go to Insert > Table.

5
COMPUTER DATA MANAGEMENT – BSMLS 1E

Now let's go back to our VLOOKUP function. Rather than Type the formula into cell E7 of the Invoice and press Enter to
telling VLOOKUP to search in a specific range on the Products see the result.
worksheet (Products!$A$2:$C$100), we can simply type the
name of the table (ProductsList). So here's our new function:
=VLOOKUP(A3, ProductsList, 2, FALSE)

• If anyone ever adds information to the table, the


VLOOKUP function will include it automatically—no
need to worry about updating the cell range later on.
Again, you'll need to update all of the VLOOKUP
functions in the spreadsheet to refer to the table rather
than the cell range.

PART 5: VLOOKUP
USING SHIPPING OPTION TO. AUTOMATICALLY
PULL SHIPPING COST
We'll need to use the Shipping Option (in cell E6) to automatically
pull in the Shipping Cost for cell E7. We can use a VLOOKUP
function to do this automatically.
WRITING THE FUNTION
This VLOOKUP function will actually be similar to the
VLOOKUP function we created in Part 2 of 5: VLOOKUP. We'll
start by typing the equals sign (=), followed by the function
name and an open parenthesis: • If you typed the formula correctly, the correct shipping
=VLOOKUP( price should appear: $9.99. If you want to make sure
your formula is working correctly, change the Shipping
1. Next, we'll add our arguments. The first argument Option in cell E6 from 3-Day to 2-Day. The Shipping
tells VLOOKUP what to search for. In our example, it Cost should change from $9.99 to $13.99.
will search for the Shipping Option, which we will be
typing in cell E6 of our Invoice. IF FUNCTION
=VLOOKUP(E6 As you may remember from Part 1 of 5: VLOOKUP, we used
2. The second argument tells VLOOKUP where to look the IF function to change the shipping cost to $0 if the cost of
for the value in our first argument. In our example, the order was at least $100. This time, we'll do it a bit
that's in the Shipping worksheet, in cell range A2:B4 differently: If the order is at least $100, we'll give them a credit
of $5.99, which is the cost of standard shipping. This means
standard shipping will be free, and the more expensive
shipping options will also get a discount of $5.99.

1. Just like before, our new formula will look at the Order
Subtotal in cell E5 to see if the value is greater than or
equal to $100, so our first argument will be E5>=100.
=IF(E5>=100
2. The second argument looks to see if the statement in
the first argument is true. If it's true, they'll receive a
credit for the cost of standard shipping (5.99). Since
the credit will be subtracted from the total order cost,
we'll make it a negative number: -5.99.
We'll include some extra rows in the cell range in case more =IF(E5>=100,-5.99
shipping options are added later. 3. The third argument looks to see if the statement in
the first argument is false. If it's false, they won't
=VLOOKUP(E6, Shipping!A2:B10 receive the $5.99 shipping credit. This means our third
argument will be 0 (zero). We'll also add a close
3, 4. Finally, we'll add our third and fourth arguments. In parenthesis after the last argument. Here's our new IF
this example, the Shipping Cost is in column B (the second function:
column), so our third argument is 2. And because we're only =IF(E5>=100, -5.99, 0)
looking for exact matches, the fourth argument is FALSE.

=VLOOKUP(E6, Shipping!A2:B10, 2, FALSE)

6
COMPUTER DATA MANAGEMENT – BSMLS 1E

DATA VALIDATION
Data validation allows you to control exactly what a user can
enter into a cell. In our example, we can use data validation to
ensure that the user chooses one of the three possible shipping
options. To make things even easier, we can insert a drop-
down list of the possible options.

This kind of data validation allows you to build a powerful, fool-


proof spreadsheet. Since users won't have to type in data
manually, the spreadsheet will be faster to use, and there's a
much lower chance that someone can introduce an error.
Depending on what spreadsheet program you're using, the
process of adding data validation will vary. We'll show you how
to add data validation in Microsoft Excel and Google Sheets.

DATA VALIDATION IN EXCEL


Since we already have a list of shipping options in the
Shipping worksheet, we're going to tell Excel to use the data in
that list to control which values a user can select. But before we
When you press Enter, the correct discount should appear in do this, we'll actually need to name the cell range first.
cell E8. Naming cell ranges is one way to keep track of important cell
ranges in your spreadsheet.
• If we look at the formula that calculates the Total
Order Cost in cell E9, we can calculate the total value NAMING A CELL RANGE
of Order Subtotal, Shipping Cost, and Discount. EXCEL 2007-2019
We'll simply add all three values; since the Discount is 1. Select the cell range you wish to name. In this example, we'll
a negative number, that value will be subtracted from select the cell range A2:A10 on the Shipping worksheet (we're
the Total Order Cost. including empty rows just in case more shipping options are
added later on).

2. Go to the Formulas tab, and then click Define Name.

PART 5: VLOOKUP
DATA VALIDATION

WHAT ARE WE TRYING TO FIX? 3.A dialog box will appear. Type a name for the cell range in
the Name: field and then click OK. In our example, we'll name.
If we look at cell E6, we can see that the word Standard is
it ShipRange.
misspelled. The Shipping worksheet doesn't contain the word
"Standrd", so our VLOOKUP function doesn't return any
results. While we could simply type in the correct spelling, that
wouldn't prevent someone from making the same mistake in
the future. To make sure this. doesn't happen again, we can
add data validation to our spreadsheet.

7
COMPUTER DATA MANAGEMENT – BSMLS 1E
3. A dialog box will appear. In the Allow: field, select List.

4. Click OK. The cell range will be named. 4.In the Source: field, type the equals sign (=) and the name of
If you ever add more data to your spreadsheet, you can easily your range, and then click OK. In our example, we'll type:
check all of your named ranges with the Name Manager to =ShipRange.
make sure they are including all of the data.

CREATE A DATA VALIDATION DROP-DOWN LIST


EXCEL 2007-2019
1. Select the cell where you want the drop-down list to appear.
In our example, that's cell E6 on the Invoice worksheet.

2. On the Data tab, click the Data Validation command.

5. A drop-down arrow will appear next to the selected cell. Click


the arrow to select the desired option. In our example, we'll
select Standard. Alternatively, you can type the shipping
option, but Excel will only accept it if it is spelled correctly.

8
COMPUTER DATA MANAGEMENT – BSMLS 1E

6. The selected value will appear in the cell. Now that we're 2. A dialog box will appear. Click Select data range in the
searching for the exact name of a shipping option, our Criteria: field.
VLOOKUP function is working correctly again.

3. Select the data that will appear in the drop-down list, and
then click OK. In our example, the shipping options are stored
in cell range A2:A4 on the Shipping worksheet, but we'll select
A2:A10 (we're including empty rows just in case more shipping
options are added later on).

DATA VALIDATION IN GOOGLE SHEETS


Creating a drop-down list for Google Sheets is actually a little
simpler than Excel, because you don't need to name a cell
range beforehand.

CREATE A DATA VALIDATION DROP-DOWN LIST


1. Right-click the cell where you want the drop-down list to
appear and select Data validation. In our example, we'll select
cell E6.

4. Click Save to close the dialog box.

9
COMPUTER DATA MANAGEMENT – BSMLS 1E

5. A drop-down arrow will appear within the selected cell. Click


the arrow to select the desired option. In our example, we'll
select Standard. Alternatively, you can type the shipping
option, but Google Sheets will only accept it if it is spelled
correctly.

6. The selected value will appear in the cell.

There we go—with our new data validation drop-down list, it'll


be a lot harder for someone to break our function in the future!

10

You might also like