How to Use VLOOKUP in Excel or Google Sheets

Latest Videos FromTom's Hardware
Avram Piltch
Managing Editor: Special Projects

Avram Piltch is Managing Editor: Special Projects. When he's not playing with the latest gadgets at work or putting on VR helmets at trade shows, you'll find him rooting his phone, taking apart his PC, or coding plugins. With his technical knowledge and passion for testing, Avram developed many real-world benchmarks, including our laptop battery test.

  • elroy.coltof
    Please don't, I swear a kitten is killed everytime someone uses VLOOKUP when INDEX should be used. I've spent too many wasted hours fixing painfully slow excel sheets that do searches when a direct indexing is possible.

    The kitten is only maimed if you use VLOOKUP when INDEX and MATCH should be used. INDEX and MATCH are much more robust against column insertions and you can directly read what column your results will be pulled from. Not to mention the huge advantage that you can now skip repeating your key column because you couldn't count out the 20+ columns in between to determine the column number in your vlookup search area. (The key column could even be to the right of your results column, but try to prevent that if at all possible.
    Reply
  • anbello262
    Useful article! I use Excel all the time and only learnt about this function recently, so keep these coming!
    Reply
  • Soaptrail
    As an accountant we hired a senior accountant who did not know how to do a vlookup. I was flabergasted. But i knew he would not last long at our company and I was right. Excel is everyone's best friend they just don't realize it.
    Reply
  • silverblue
    Ah VLOOKUP... how I dislike you. Articles on INDEX and MATCH would be very helpful, especially when somebody's working with a lot of data (and especially when you're on a fair to middling laptop).

    Array formulas take the cake, though. :)
    Reply
  • derekullo
    I need better Excel skills like / unlike this; not sure judging by comments.

    At work I do alot of traveling between locations pretty much at random.

    I may go from branch 4 to branch 2 on Monday and then branch 7 to branch 1 to branch 11 on Tuesday.

    So in an effort to make it easier to calculate distances between branches I hard coded in excel's visual basic "i believe it's visual basic" the distance between all 16 branches to every other branch on the spreadsheet for the first 3 lines on the excel page.

    if a1 = branch 1 and a2 = branch 2 then a3 = 4 (distance between branches)

    Each row in excel has at minimum 16x16 or 256 if then statements
    So with 3 rows I choose to write that's 256x3 or 768 lines of code instead of a chart lookup table thingy.

    Payroll also wants the actual name of the branch so I made another hard coded script that replaces "branch 1" with the actual name and address of the branch.

    If a1 = branch 1 then a1 = Branch 1 Street, Streetville, OH

    This effectively doubles the previous number from 768 to 1536 lines of code.

    I am 99% sure this is the wrong way to do it but this 1500+ line monstrosity works well and is remarkably quick.
    Reply
  • ewarwoo
    Who types false when you can just use 0?
    Reply
  • hermitboy
    A little history could be nice as well - which can help with optimization.
    Back when computers were much less powerful, doing any sort of lookup could take a really long time.
    Then along comes Vlookup.
    Vlookup works best when your data is sorted, because it was created with a specific shortcut in mind - narrowing the search range by half.
    Vlookup compares your lookup value to the middle value in the primary column. Then it will compare the lookup value to middle of the top half or bottom half - and will continue comparing the lookup value to increasingly smaller ranges until it finds the match.

    Not sorting the data defeats the purpose of Vlookup's shortcut processing - and can make the actual lookup take much longer than if Index-Match were used.

    Personally, I prefer index-match. Especially in situations where I need the data sorted on a column that is not related to the lookup value. But there are times when I'll use Vlookup instead.
    Reply
  • elroy.coltof
    Reading back all the comments I feel it's best to explain why VLOOKUP is a terrible choice for this particular problem.

    First off it should be noted that VLOOKUP has two wildly different modes of operation. If the last parameter is TRUE (the default if you omit it) it expects a sorted first column and is really fast using a binary search. However it also means it looks for an aproximate match. If the value you look for is not in the table it returns the closest result after the position after where your result should have been. BUT IT DOESNT TELL YOU THIS! The search is O(logN)

    The second mode, last parameter FALSE looks for an exact match and does not require sorted data, it does a lineair search and is bog slow. It does however return an N/A if no exact match is found. The search is O(N).

    To speed things up therefore you could sort the data you're searching in and use a double aproximate vlookup in an if statement: The first one you is in the condition and checks against the search value, if they are different then the item is not present, return N/A manually. If they are equal do the VLOOKUP again and return the value. If you do it in this order at least the second lookup would not be done if an item is not present. If you have a large amount of data and you are able to sort them this is faster than a single exact vlookup! Never use FALSE on large sorted datasets!!

    Now consider that if you want to merge multiple columns you have to repeat those VLOOKUPs for every column, so 20 columns is 20x as slow!

    And now INDEX/MATCH: The power here is that they are two different functions.

    INDEX allows you to take a range and directly reference a value in that range by row and column number. No searching happens, it's O(1)!

    MATCH determines the row number the matching item is in using a lineair search (the slow one, I know, but because of the way it's implemented it's still faster and uses less memory). You can store this value in a cell and then use Index for every column you want to match. It's O(N) for an exact match (last param = 0) and O(log(N)) for an aproximate search (last param = -1 or 1) and you only have to do it once for every row and not for every column! You can do the same trick with a double aproximate MATCH as you could do with VLOOKUP to benefit from the speed of binary search and still be guarantueed to be told if there is no exact match.

    So how do you then match up to data tables with the same key column?
    You make a helper column with MATCH that has the row numbers in the second file in the order they appear in the first file. This column can be on a different sheet or even in a different file. And then you use INDEX to add the data to the first file.
    Reply
  • punakuca48
    Everything that Elroy said about VLOOKUP (not to mention notorious LOOKUP) is right. Today. But, VLOOKUP was made in times when spreadsheet was ment to be spreadsheet, and not database as is case today. Times when you were very limited by size of the spreadsheet and where people would be making small tables (coutning hundreds of rows) where VLOOKUP made sense. It still does if your tables are small and you want fast rezults.
    I use it on dayly basis in such cases, but if tables grow over 10000 rows INDEX/MATCH combo cannot be beaten! Especially if you have 50+ columns and key column is third or 10th and not the first one.
    Also purpose of TRUE variant and FALSE variant are totaly different! TRUE variant gives you exact match when that match exists. But if you deal with ranges and only limits of the ranges are quoted in compare to table, FALSE variant is the only one usefull and right.
    Reply
  • peter.domanico
    I’ve taken this to the next level here: (Excel only):
    https://codereview.stackexchange.com/q/196125/138859

    Enjoy!
    Reply