excel lookup functions explained

Upload: shwetakarsh

Post on 30-May-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 Excel Lookup Functions Explained

    1/8

    Excel Lookup Functions Explained

    Excels 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 lookupa value in one place and insert it in another. For example, lets say you have alist of all of your customers on a sheet named Accounts and an invoice onanother sheet named Invoice. When you type in their account number onthe Invoice, you want Excel to fill in the name of the customer and theiraddress (and this information is included for all customers on the Accountssheet). A VLOOKUP will do this for you.

    Make a small sample workbook to try this out. Name Sheet One Invoice andname Sheet Two Accounts. On the Accounts sheet, put three columns ofdata. Column A would be Account Numbers, Column B would be CustomerName, 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 fivecolumn headings in cells A1:E1 Date, Product Ordered, Account #, CustomerName, Address -but dont put any data in there yet. (In reality, this sheetwould be an actual invoice which included sections for you to add orderinginfo for any products they buy, etc. But for this example, lets keep it simple.)Now, before we try the VLOOKUP, the best thing to do is name the range ofdata 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 touse absolute cell references. So, I find naming the range a much easier way todo it. Highlight all the data on the Accounts sheet and name it Customers(dont include the column headings in the named range just the data). If youdont know how to name ranges, read this TechTrax article I wrote on how todo that.http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=281 Now that you have your data and have named the range, lets look at buildinga simple VLOOKUP formula. Assume we will be typing account numbers intocell C2 and wanting the customers name and address to be filled into D2 andE2.

    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 Excel2002/2003, it's on your Formula Bar. On the Invoice sheet, click into cell D2and click on the fxbutton.

    http://pubs.logicalexpressions.com/Pub0009/LPMFrame.asp?CMD=AuthorDetail&ID=8http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=281http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=281http://pubs.logicalexpressions.com/Pub0009/LPMFrame.asp?CMD=AuthorDetail&ID=8
  • 8/14/2019 Excel Lookup Functions Explained

    2/8

    (The picture above shows how this box looks in Excel 97/2000. In 2002/2003its slightly different, but I think you can figure it out.)If you know what type of function you are looking for, you can select thecategory and all the functions within that category will be listed. However, ifyou don't know what category you need, you can select "All" in the categorylist and all of Excel's functions will be listed. Notice as you click on anyfunction name, Excel displays a description of what that function does belowthe boxes. For this exercise, select the "Lookup & Reference" category andscroll down to select the VLOOKUP function. Note that it tells you that thisfunction searches for a value in the leftmost column of a table and returns avalue 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 VLOOKUPformula. Now, because we will be typing an Account # in cell C2, that is thevalue we must put in the first box of this wizard which will tell Excel to look forwhatever is in C2 in the leftmost column of our lookup table (which we createdon the Accounts sheet and named "Customers"). So, enter C2 into the topbox.Click into the next box where it says "Table_array". Notice at the bottom ofthis box, it tells you what each box you click inside needs. This is where weneed to identify our table so Excel knows where to look. So, in this box,simply type Customers. (If you didnt name the range, you will have to put

    the absolute reference including the sheet name here this is why its easierto just name the range.)Click into the third box. This one wants to know the number of the column wewant returned. Remember that what you entered in the first box in this wizardmust ALWAYS be in the first column of your lookup table. So, in our table, theAccount # is in the first column and the Customer Name is in the secondcolumn. Since the customer name is what we want to put here, just type a 2to let Excel know we want what is in the second column.

  • 8/14/2019 Excel Lookup Functions Explained

    3/8

    Notice the last box is labeled "Range_lookup" and it is the only label that isnot 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 enteranything in this box, Excel will apply the default. If you read the instructionsat 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 "exactmatch". Since we want an exact match, type false in this box. This is what itshould 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 inyour formula bar and you will see the formula is=VLOOKUP(C2,Customers,2,FALSE). As you get more used to usingfunctions, you won't have to use the wizard as much if you take the time tolook at the formulas and start to understand how they work.Now click into cell E2 and add a VLOOKUP formula which will find the Addressin 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, youshould 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 exactlythe same as the first one. We are still looking for the value that will be placedin C2. We are still looking in the table named Customers. And we still wantfalse for an exact match. The ONLY thing that is different is we are now goingto pull the information from Column 3 instead of 2.Move to cell C2 and type in one of the Account numbers you have in yourAccounts sheet and you will see Excel fills in the Customer Name and Addressfor that account number However, if you type a number that does not exist

  • 8/14/2019 Excel Lookup Functions Explained

    4/8

    on your Accounts sheet, Excel will leave the #N/A because the information forthat number is "not availableTo see a VLOOKUP formula in action, go to this page at my website and viewthis interactive formula:http://www.personal-computer-tutor.com/vlookup.htm

    Now that we've gotten more comfortable with VLOOKUP, let's look at nestingformulas, one inside another. We can use the VLOOKUP formula we justmade as a starter.Let's say we don't want to see that nasty old #N/A every time one of ourVLOOKUP formulas refers to an empty cell or value that is not in our lookuptable. What we need to do is tell Excel to show us the result of the VLOOKUPonly IF it does not return a #NA result and, otherwise, just leave the cellempty. So we need to use three different functions in one formula: VLOOKUP,IF, and ISNA.Remember how we made an IF statement. =IF(, ,). If you dont 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 ifit 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 whatthey must include. We already have our VLOOKUP formula and we alreadyknow what an IF function must include, so the only one we need to learn is theISNA 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 IFstatement wants to know if something is true or false, these two functionswork 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 withinformulas, Excel performs the operation dictated by the deepest nestedformula first, then works its way outward. So, enter the following formula intocell D2, then enter an account number in cell C2 that does not exist on yourAccounts 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 orExcel will give you an error message.Remember that Excel performs the deepest nested function first, so looking atthe formula above, you will see:

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

    http://www.personal-computer-tutor.com/vlookup.htmhttp://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=225http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=242http://www.personal-computer-tutor.com/vlookup.htmhttp://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=225http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=242
  • 8/14/2019 Excel Lookup Functions Explained

    5/8

    Then it performs the ISNA function on the result of the VLOOKUP to seeif the VLOOKUP gave a #N/A error or not. The ISNA function returns avalue 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 thelast VLOOKUP function is telling it to do if the value is false

    ~~Ray Blake, GR Business Process Solutions

    Using VLOOKUP, HLOOKUP, INDEX, and MATCH in Excel tointerrogate data tables

    Lookup tables are fantastically useful things in Excel. I remember when someoneshowed me for the first time how to build a data table and perform some simplelookups on it. For the first time, I began to realise just how powerful Excel couldbe in the right hands.

    In this article, I'll talk about what a data table is, why you might find it useful tohave one, and why and how you might want to interrogate it. We'll end with atrick or two involving some nested formulae, but by the time we get there, it willall make sense.

    First of all, then, what's a data table? Well, there's one shown below:

    You'll notice that some related data is set out in columns, each with a heading inbold at the top. So many other functions in Excel can use those headings

    intelligently, that I have always made it a habit to put them in. Data tables like thishave so many uses it's difficult to know where to start: phone number lists, CDcollections, customer lists, the uses are endless. But sooner or later, you're goingto want to extract data from such a list, perhaps for a mail merge or to fill in aninvoice automatically, say.

    Probably the best way of learning about the LOOKUP functions is to ask somequestions and use formulae to answer them. For instance, look again at the data

    http://personal-computer-tutor.com/abc/ray_blake.htmhttp://personal-computer-tutor.com/abc4/v33/ray33.htmhttp://personal-computer-tutor.com/abc4/v33/ray33.htmhttp://personal-computer-tutor.com/abc4/v33/ray33.htmhttp://personal-computer-tutor.com/abc4/v33/ray33.htmhttp://personal-computer-tutor.com/abc4/v33/ray33.htmhttp://personal-computer-tutor.com/abc/ray_blake.htmhttp://personal-computer-tutor.com/abc4/v33/ray33.htmhttp://personal-computer-tutor.com/abc4/v33/ray33.htmhttp://personal-computer-tutor.com/abc4/v33/ray33.htmhttp://personal-computer-tutor.com/abc4/v33/ray33.htmhttp://personal-computer-tutor.com/abc4/v33/ray33.htm
  • 8/14/2019 Excel Lookup Functions Explained

    6/8

    table above. If I want to know Barbara's age, I can use a command calledVLOOKUP. It's called VLOOKUP because it looks up the data in a table, basedon finding the key in a Vertical list.

    The formula I'd use here is:

    =VLOOKUP("Barbara",A2:C6,2,FALSE)

    Of course, this will return the number 23, which is Barbara's age. Let's lookbriefly at the format of the function. The first argument is the piece of data I wantto look up (what I call the 'key') in the first column. (This must always be in thefirst column, but later on I'll show you how to find values based on a key in othercolumns instead.)

    The second argument is the range which contains the table, in this case A2:C6.I'd normally have named this range, but you don't have to.

    The third argument is the column number I want to return the value from. Lookingat the table again, the first column contains names, the second ages and thethird locations. Clearly, if I want to find Barbara's location, I'd put a 3 in thisargument, but since I want to know her age, I've used 2.

    The fourth argument, the FALSE, is supposed to be an optional argument, butmy advice is ALWAYS to use it. What it means is, "Don't rely on the list of items inthe first column of the table being in alphanumeric order - check every one ofthem until you get an exact match." Leaving it out is like saying, "The first columnof the lookup table is definitely in alphanumeric order - if you get past the search

    phrase in the list and it's not there, don't keep looking, just use the nearestmatch". This would speed up your sheet if there were a lot of huge data tables init, but here it's not even worth thinking about. It's good practice always to include"FALSE" just in case it trips you up one day.

    Well, of course there's an HLOOKUP to match the VLOOKUP, too. You'd use thiswhen your table is oriented left to right, rather than top to bottom. Here is anexample of what I mean:

    I can use the HLOOKUP function to find what date Debbie's birthday falls in likethis:

    =HLOOKUP("Debbie",F2:J4,3,FALSE)

    This formula returns April. The arguments work the same way as for VLOOKUP,except of course that the third argument refers to the row number rather than the

    http://personal-computer-tutor.com/abc4/v33/ray33.htmhttp://personal-computer-tutor.com/abc4/v33/ray33.htmhttp://personal-computer-tutor.com/abc4/v33/ray33.htmhttp://personal-computer-tutor.com/abc4/v33/ray33.htmhttp://personal-computer-tutor.com/abc4/v33/ray33.htmhttp://personal-computer-tutor.com/abc4/v33/ray33.htm
  • 8/14/2019 Excel Lookup Functions Explained

    7/8

    column number. On the whole, it's much better to organise your data tablesvertically, as in the first example, because a horizontal data table cannot besorted or filtered by Excel as easily as a vertical one, but there are times when ithas to be horizontal for some reason.

    The MATCH formula appears at first to do something quite unremarkable. Let'shave another look at our vertical data table:

    MATCH allows me to find the position of an item within a range. For instance, if Iwant to know how far down in the list of names Charlie is, I can use this formula:

    =MATCH("Charlie",A2:A6,0)

    The return from a MATCH function is always a number, in this case the number 3because Charlie is the third entry in the range A2:A6. The zero at the end there isa bit like the 'FALSE' in VLOOKUP and HLOOKUP - optional but risky to omit.When set at zero, it says: "Make it an exact match". Ninety-nine times in ahundred, that's exactly what I want.

    INDEX is the opposite of MATCH in a way. It tells you what the nth value in arange is. For instance, who is in position 5 in the list? Easy:

    =INDEX(A2:A6,5)

    This returns the name 'Elvis' because he is the fifth item in the range A2:A6.

    Like INDEX, the MATCH function doesn't seem to do anything out of the ordinaryso far. But the real power of these functions only becomes apparent when youcombine them.

    Look at the vertical data table again and consider how you'd find out who lived inBelfast. The two LOOKUP formulas are no use, because the key value is not inthe first column. Remember, VLOOKUP can only read values to the right of thekey and HLOOKUP can only read values below the key.

    But, look at it another way. I can break the question down into two smaller ones,like this:

    http://personal-computer-tutor.com/abc4/v33/ray33.htmhttp://personal-computer-tutor.com/abc4/v33/ray33.htmhttp://personal-computer-tutor.com/abc4/v33/ray33.htmhttp://personal-computer-tutor.com/abc4/v33/ray33.htm
  • 8/14/2019 Excel Lookup Functions Explained

    8/8

    1. How far down the 'locations' list does 'Belfast' appear?2. Whose name is in the 'names' list exactly as far down?

    Put in those terms, it is pretty clear. The first question can be answered, ofcourse, by using a MATCH function:

    = MATCH("Belfast",C2:C6,0)

    This will tell us that Belfast is number 4 in the list, so we can put the number 4into an INDEX formula:

    =INDEX(A2:A6,4)

    Of course, this formula will return the name 'Debbie', which answers the originalquestion. But in the same way that original question is made up of two subquestions, so we can turn our two formulas into a single one, like this:

    =INDEX(A2:A6,MATCH("Belfast",C2:C6,0))

    Again, this gives us the answer 'Debbie'.

    Of course, we've looked so far at simple tables, and it's been far quicker just tolook at our table and answer the questions than to sit down and write formulae!However, there will be times when the data tables are huge, or when you wantExcel to work things out for itself and get on with things. At times like those, you'llfind that the LOOKUP functions of Excel are an invaluable part of your toolkit.