Vlookup Tutorial

  • Upload
    otienor

  • View
    255

  • Download
    3

Embed Size (px)

Citation preview

  • 8/7/2019 Vlookup Tutorial

    1/13

    VLOOK-UP

    If you use Excel much at your job, sooner or later, youre bound to need to look up values in a

    table. One of the most useful functions in Excel, called vlookup, does exactly that. The V in

    vlookup stands for vertical and lookup is pretty self explanatory. This function allows you to

    look up values in a table that are listed in column format (how most tables are laid out), given

    another value (lets call this the key). Excel also has a sister function called hlookup (h =

    horizontal) that can be used to look up values in rows.

    Sadly, as most companies seem to rely on Excel as a poor-mans database of sorts (a totally

    unscalable solution and prone to errors with every revision, but dont get me started), once you

    know vlookup, its likely to become one of your most often used Excel functions.

    So, lets get started with a very simple example of what vlookup is all about. Suppose you had

    the following table:

    Given a list of names in another part of the table (in this case, column H), you want to figure out

    what kind of animal it is:

    Vlookups format looks like the following:

    =vlookup(lookup value, table where values reside, column # where values are located, false)

    Lets look at each of these parts a bit closer.

  • 8/7/2019 Vlookup Tutorial

    2/13

    The first thing that goes into the vlookup function is the thing you know (or are given) and that

    will be used to lookup other values. In this case, you have the names of the animals, so these

    are the things we know. In our example, they reside in column H, from cells H2 through H5. If

    we wanted to put the type of animal next to the name of the animal in column I (so I2 would

    correspond to the name of the animal in H2), we would insert the vlookup function there:

    and put H2 as the first thing in our vlookup function:

    Next, we need to know the location of the table where our values reside. These happen to be

    from cells A1 through B5 in this example, which we would highlight with our mouse to insert

    into the vlookup function. Its very important that you include all the cells in the table.

    Highlight the table with your mouse:

    At the same time, the vlookup function automatically puts in the cells youve highlighted:

  • 8/7/2019 Vlookup Tutorial

    3/13

    Next, we need the column number where the values are located. Always start with the first

    column (column A in this case) as #1 and count out to the right. In this example, the type of

    animal listed is in column 2, so thats what we would need to insert in the vlookup function.

    Note that to use vlookup, your keys always have to be to the left of your values. (Well cover

    more of this in part II of the tutorial at a later date.)

    Finally, the last attribute that vlookup takes is either true or false. I happen to always usefalse, and what this does is force vlookup to return the first exact value it finds. If that value

    isnt found, then vlookup conks out and returns #N/A. Though we wont use it in this

    example, if you select true, then rather than always looking for the exact value, vlookup will

    return the exact value if it exists, or the closest one to it that doesnt exceed the key. (If you use

    true, you will need to sort your data in ascending order before using vlookup.)

    Still with me? Again, this is what we would actually put in cells I2 if the names of the animals we

    have are located in cells H2 through H5:

    =vlookup(H2, A1:B5, 2, false)

  • 8/7/2019 Vlookup Tutorial

    4/13

    Once we close off the parenthesis and hit Enter, vlookup automatically calculates:

    And so on. We would continue down each cell in column I that we needed. One thing to note is

    to make sure that the location of your keys and values is always selected correctly. Oftentimes,

    as you copy-and-paste formulas all around Excel, the location of the data will also move around

    relative to the cell. The easiest way to prevent this is to lock the range of the location; in this

    case, we would do so by using $A$1:$B$5 instead of A1:B5. This way, as we move down

    column I, say, to cell I2, A1:B5 doesnt become A2:B6 but stays with the original range of data.

    This way, we can just copy whats in cell I2 down the rest of the cells (from I3 through I5):

    Finally, heres our result, after making the $ changes and copying and pasting the formula

    down the rest of the column:

    This has been a really simple example of vlookup, We can look at a more complex example

    bellow.

  • 8/7/2019 Vlookup Tutorial

    5/13

    This time, well look at a slightly more complicated example and show a couple of tips and tricks

    for making VLOOKUP work correctly.

    By the way, Ive received a couple of comments and thanks for my previous post and just want

    to encourage readers to let me know if there are other examples of functions or situations they

    face that they need help with. They make a great source for future posts on this site :)

    In our last example, we had a simple, two-column list of names and types of animals. In this

    post, well take a look at a list of employee names and data, say, for calculating commissions for

    sales people. Heres what our data looks like (on all images in this post, click to enlarge):

    As you can see, were given employees last and first names, their base salaries, their bonus

    percentage, and the % of the year that they were employees. Were also given a unique

    identifier in the form of an employee number. Lets examine the data a bit further.

    First, what we should notice is that there are employees with the same last and first names.

    Theres an Andrew Anderson as well as an Andrew Cobb. And a Penny and Jim Dee.

    Remember that VLOOKUP will either return the first match it finds in a list. In this case, if we

    were to use VLOOKUP to lookup a list of last names or first names, VLOOKUP would always

    return Andrew Andersons data (if we were looking using the First Name field) or Pennys

    data (if we were looking using the Last Name field).

    So, what to do?

    In this example, were lucky to have a unique identifier in the form of Employee Number.

    Each number is assigned only once to the employee, so this field would be a safe one to use for

    VLOOKUP. The only problem is that its located all the way at the end of the data, to the right of

    all the other fields. Remember that VLOOKUP has another criteria: whatever field youre using

    to look up other data has to be to the left of all the other fields.

  • 8/7/2019 Vlookup Tutorial

    6/13

    The easiest way to accomplish this is to insert a column to the left of Last Name (Column A)

    and copy-and-paste the Employee Number column there. Heres how that would look, step

    by step:

    Step 1: Select column F, where Employee Number data is located:

    Step 2: Right-click on the mouse:

    Step 3: Select Copy from the menu:

  • 8/7/2019 Vlookup Tutorial

    7/13

    All of column F is now highlighted in a dotted line:

    Step 4: Highlight column A:

    Step 5: Right-click on the mouse once more:

  • 8/7/2019 Vlookup Tutorial

    8/13

    Step 6: Select Insert Copied Cells on the menu

    Step 7: The cells from column F are now copied over to column A, and everything is shifted over

    one column:

  • 8/7/2019 Vlookup Tutorial

    9/13

    Now, employee numbers appear in both column A and G. Hit to get rid of the highlight around

    column G.

    Were now good to go!

    By the way, if you had not had unique identifiers like employee numbers readily available, you

    could potentially use the CONCATENATE or & function in Excel to create unique identifiers.

    CONCATENATE is a function that just merges two fields together. In this case, creating a unique

    identifier out of concatenating last name and first name would probably work.

    Back to the tutorial. Suppose we had a second sheet that had a list of employee numbers for

    the four employees who had worked less than 100% during the year, and we wanted tocalculate their bonuses for the year. Notice we swapped first and last name orders in this sheet

    and put the employee numbers in a different order:

  • 8/7/2019 Vlookup Tutorial

    10/13

  • 8/7/2019 Vlookup Tutorial

    11/13

    Put in a , after this to move on to the next input for VLOOKUP called table_array.

    Step 3: Now we need to highlight the area where all the data resides:

    Put in a , after this to move on to the next input for VLOOKUP, called col_index_num.

    Step 4: Remember that in this case, we need to reference column #3, where first names are

    located. We always start with the lookup value as column #1 and count toward the right.

  • 8/7/2019 Vlookup Tutorial

    12/13

    Put in a , after this to move on to the final input for VLOOKUP.

    Step 5: Finally, we want to put in false as the final input into VLOOKUP to tell it to look for

    exact matches.

    Now close off the parenthesis to VLOOKUP, and the cell is automatically populated with the

    data we need.

    The key now is to populate the rest of the cells. Can you figure out how to do this? One way

    would be to go through each cell and repeat the steps above. For example, to populate cell C2,

    we would write:

    =VLOOKUP(A2,Sheet1!A1:G8,2,FALSE)

    and so on, referencing each column where the data resides. (Salary resides in column 4,

    bonus in column 5, etc.) Another way would be to use Excels anchoring mechanism so that

    we could copy and paste formulas a bit more efficiently.

    For example, for the rest of the cells under First Name, what we could do is write the

    following instead in B2:

    =VLOOKUP($A2,Sheet1!$A$1:$G$8,3,FALSE)

    What putting a $ sign does in front of cell coordinates is to lock them in place. By putting

    $A2 instead of A2 in the first input section, we lock A in place (because all our employee

    numbers are in column A) and let the 2 change as we go down the row.

  • 8/7/2019 Vlookup Tutorial

    13/13

    By putting $A$1:$G$8 instead of A1:G8 as we originally had, we lock in the entire A1 to G8

    cells in place and keep that section locked no matter where we put the formula.

    If we then copy the formula down to cells B3 through B5, we dont have to retype the formula

    each time. Similarly, you can copy the formula across each row, making sure to just change

    each column number so that youre pulling the right data.

    Heres what the finished table would look like:

    And heres what the final column, which is just the total bonus calculation, would look like if we

    assumed that bonuses equaled salary * bonus * % of year worked:

    In this example, we populated a new table in a new sheet with data from a separate sheet. But

    keep in mind one of the powerful things of VLOOKUP is that with a unique identifier such as

    Employee Number, what we could do is create an entirely new table with elements from

    multiple other tables that each contain Employee Number. For example, salary information

    might be stored in one place, and employee names in another. By using VLOOKUP to lookupemployee numbers from each table, we could create one table that contains all information at

    once.