lookup functions

Upload: vinesh

Post on 07-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 Lookup Functions

    1/4

    Ray Blake, GR Business Process Solutions Page 1

    Excel lookup functions explainedUsing 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, Ill 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. Well end with a

    trick or two involving some long, nested formulae, but by the time we get there,it will all make sense.

    First of all, then, whats a data table? Well, theres one shown on the sheetbelow:

    Youll 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 likethis have so many uses its difficult to know where to start: phone number lists,CD collections, customer lists, the uses are endless. But sooner or later, youregoing to want to extract data from such a list, perhaps for a mail merge or to fillin an invoice 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 datatable above. If I want to know Barbaras age, I can use a command called

    VLOOKUP. Its called VLOOKUP because it looks up the data in a table, based onfinding the key in a Vertical list.

    The formula Id use here is:

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

  • 8/6/2019 Lookup Functions

    2/4

    Ray Blake, GR Business Process Solutions Page 2

    Of course, this will return the number 23, which is Barbaras age. Lets lookbriefly at the format of the function. The first argument is the piece of data Iwant to look up (what I call the key) in the first column. (This must always be inthe first column, but later on Ill show you how to find values based on a key inother columns instead.)

    The second argument is the range which contains the table, in this case A2:C6.Id normally have named this range, but you dont have to.

    The third argument is the column number I want to return the value from.Looking at the table again, the first column contains names, the second ages andthe third locations. Clearly, if I want to find Barbaras location, Id put a 3 in this

    argument, but since I want to know her age, Ive 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 itemsin the 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 firstcolumn of the lookup table is definitely in alphanumeric order - if you get pastthe search phrase in the list and it's not there, don't keep looking, just use thenearest match". This would speed up your sheet if there were a lot of huge datatables in it, but here it's not even worth thinking about. It's good practice alwaysto include "FALSE" just in case it trips you up one day.

    Well, of course theres an HLOOKUP to match the VLOOKUP, too. Youd 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 Debbies 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 thanthe column number. On the whole, its much better to organise your data tablesvertically, as in the first example, because a horizontal data table cannot be

  • 8/6/2019 Lookup Functions

    3/4

    Ray Blake, GR Business Process Solutions Page 3

    sorted 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. Letshave 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 thisformula:

    =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

    is a 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-none times in ahundred, thats exactly what I want.

    INDEX is the opposite of MATCH in a way, because it tells you what the nthvalue in a range 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 doesnt seem to anything out of the ordinary sofar. But the real power of these functions only becomes apparent when youcombine them.

    Look at the vertical data table again and consider how youd 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 the

    key and HLOOKUP can only read values below the key.

  • 8/6/2019 Lookup Functions

    4/4