Logo: TechTrax...brought to you by MouseTrax Computing Solutions

Excel’s VLOOKUP Function: How to Use It and How to Nest It

by Linda Johnson, MOS

The VLOOKUP function is a handy one to know when you want Excel to lookup a value in one place and insert it in another. For example, let’s say you have a list of all of your customers on a sheet named “Accounts” and an invoice on another sheet named “Invoice”. When you type in their account number on the Invoice, you want Excel to fill in the name of the customer and their address (and this information is included for all customers on the Accounts sheet). A VLOOKUP will do this for you.

Make a small sample workbook to try this out. Name Sheet One “Invoice” and name Sheet Two “Accounts”. On the Accounts sheet, put three columns of data. Column A would be Account Numbers, Column B would be Customer Name, and Column C would be Address. Add at least five pretend customers, so you have enough to play with. On the Invoice sheet, just add these five column headings in cells A1:E1 – Date, Product Ordered, Account #, Customer Name, Address -but don’t put any data in there yet. (In reality, this sheet would be an actual invoice which included sections for you to add ordering info for any products they buy, etc. But for this example, let’s keep it simple.)

Now, before we try the VLOOKUP, the best thing to do is name the range of data that includes the info you want to pull over from the Accounts sheet. You can do VLOOKUPs without naming the range, but then you MUST be sure to use absolute cell references. So, I find naming the range a much easier way to do it. Highlight all the data on the Accounts sheet and name it Customers (don’t include the column headings in the named range – just the data). If you don’t know how to name ranges, read this TechTrax article I wrote on how to do that.

http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=281

Now that you have your data and have named the range, let’s look at building a simple VLOOKUP formula. Assume we will be typing account numbers into cell C2 and wanting the customer’s name and address to be filled into D2 and E2.

The best way to learn new formulas is to use the Insert Function button  . In Excel 97 and 2000, it's a button on your Standard Toolbar.  In Excel 2002/2003, it's on your Formula Bar.  On the Invoice sheet, click into cell D2 and click on the fx button.

(The picture above shows how this box looks in Excel 97/2000. In 2002/2003 it’s slightly different, but I think you can figure it out.)

If you know what type of function you are looking for, you can select the category and all the functions within that category will be listed.  However, if you don't know what category you need, you can select "All" in the category list and all of Excel's functions will be listed.  Notice as you click on any function name, Excel displays a description of what that function does below the boxes.  For this exercise, select the "Lookup & Reference" category and scroll down to select the VLOOKUP function.  Note that it tells you that this function “searches for a value in the leftmost column of a table and returns a value from the same row in that table based on what column in that row you specify.”  Click OK.

Once you click OK, you will get the wizard which helps you with your VLOOKUP formula. Now, because we will be typing an Account # in cell C2, that is the value we must put in the first box of this wizard which will tell Excel to look for whatever is in C2 in the leftmost column of our lookup table (which we created on the Accounts sheet and named "Customers").  So, enter C2 into the top box.

Click into the next box where it says "Table_array". Notice at the bottom of this box, it tells you what each box you click inside needs.  This is where we need to identify our table so Excel knows where to look.  So, in this box, simply type Customers. (If you didn’t name the range, you will have to put the absolute reference including the sheet name here … this is why it’s easier to just name the range.)

Click into the third box.  This one wants to know the number of the column we want returned.  Remember that what you entered in the first box in this wizard must ALWAYS be in the first column of your lookup table.  So, in our table, the Account # is in the first column and the Customer Name is in the second column.  Since the customer name is what we want to put here, just type a 2 to let Excel know we want what is in the second column.

Notice the last box is labeled "Range_lookup" and it is the only label that is not bold.  Whenever a label in this wizard is not bold, that means this "argument" of the function is not required.  However, if you do not enter anything in this box, Excel will apply the default.  If you read the instructions at the bottom of this box, you will see that the default for this box is "true" which will find the "closest match", whereas "false" will find an "exact match".  Since we want an exact match, type false in this box. This is what it should look like if you have entered all the info correctly:

Click OK and you will see that cell D2 now shows #N/A, which simply means that there is no value yet in C2, so the information is "not available".  Look in your formula bar and you will see the formula is =VLOOKUP(C2,Customers,2,FALSE).  As you get more used to using functions, you won't have to use the wizard as much if you take the time to look at the formulas and start to understand how they work.

Now click into cell E2 and add a VLOOKUP formula which will find the Address in our table.  The formula will be exactly the same, except the "Col_index_num" will be 3 instead of 2 because we want to return the address, which is in the third column of our table.  Once you have added this, you should see another #N/A in cell E2.  The formula will be =VLOOKUP(C2,Customers,3,FALSE).

NOTE:  All other information in the second VLOOKUP formula will be exactly the same as the first one.  We are still looking for the value that will be placed in C2.  We are still looking in the table named Customers.  And we still want false for an exact match.  The ONLY thing that is different is we are now going to pull the information from Column 3 instead of 2.

Move to cell C2 and type in one of the Account numbers you have in your Accounts sheet and you will see Excel fills in the Customer Name and Address for that account number   However, if you type a number that does not exist on your Accounts sheet, Excel will leave the #N/A because the information for that number is "not available 

To see a VLOOKUP formula in action, go to this page at my website and view this interactive formula:
http://www.personal-computer-tutor.com/vlookup.htm

Now that we've gotten more comfortable with VLOOKUP, let's look at nesting formulas, one inside another.  We can use the VLOOKUP formula we just made as a starter.

Let's say we don't want to see that nasty old #N/A every time one of our VLOOKUP formulas refers to an empty cell or value that is not in our lookup table.  What we need to do is tell Excel to show us the result of the VLOOKUP only IF it does not return a #NA result and, otherwise, just leave the cell empty.  So we need to use three different functions in one formula: VLOOKUP, IF, and ISNA.

Remember how we made an IF statement.  =IF(<criteria>, <value if true>,<value if false>). If you don’t remember, read these two TechTrax articles I wrote:

Intro to IF Statements:
http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=225

Nesting IF Statements:
http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=242

So, our criteria is IF the VLOOKUP returns #N/A, give me an empty cell, but if it doesn't, give me the result of the VLOOKUP.

Let's try it.  First we need to think about each of the three formulas and what they must include.  We already have our VLOOKUP formula and we already know what an IF function must include, so the only one we need to learn is the ISNA function.  All the ISNA function does is return a value of true or false.  True if the cell has a #N/A error and false if it doesn't.  So, since an IF statement wants to know if something is true or false, these two functions work very well together.

Click into cell D2 in your Invoice sheet where your first VLOOKUP formula is.

The thing you need to understand is that when you nest formulas within formulas, Excel performs the operation dictated by the deepest nested formula first, then works its way outward.  So, enter the following formula into cell D2, then enter an account number in cell C2 that does not exist on your Accounts sheet and you will see that the #N/A error does not show.

=IF(ISNA(VLOOKUP(C2,Customers,2,FALSE)),"",(VLOOKUP(C2,Customers,2,FALSE)))

NOTE:  Be sure to include all the proper commas and parentheses.  Remember that all opening parentheses must have closing parentheses or Excel will give you an error message.

Remember that Excel performs the deepest nested function first, so looking at the formula above, you will see:

  • The first thing Excel does is perform the first VLOOKUP (since that's the one that is nested deepest)

  • Then it performs the ISNA function on the result of the VLOOKUP to see if the VLOOKUP gave a #N/A error or not.  The ISNA function returns a value of true if it sees the #N/A error and a value of false if it doesn't

  • Then Excel performs the IF function on the result of the ISNA function.  If it sees a "true", it returns nothing (which is what the "" tells it). If it is "false", it returns the result of the VLOOKUP function, which is what the last VLOOKUP function is telling it to do if the value is false.

Getting the hang of it? Excel has other lookup and reference formulas that you can explore using the Insert Function box. For example, HLOOKUP is similar to VLOOKUP, but it looks for Horizontally laid out data, instead of Vertical. More information on different lookup and reference formulas is available in this article from my ezine, ABC ~ All ‘Bout Computers:

Excel Lookup Functions Explained
http://personal-computer-tutor.com/abc4/v33/ray33.htm

Linda F. Johnson
http://www.personal-computer-tutor.com/

Click to rate this article.

 

Go up to the top of this page.  

This site powered by the Logical Web Publisher™: Content management by Logical Expressions, Inc.