Sub topic 2
VLOOKUP: search key, range, and column index
This chapter assumes you can enter formulas in Google Sheets and read ranges such as A2:C100. It also assumes you know why $A$2:$C$100 stays fixed when you copy a formula, and how to refer to another sheet. You will build up VLOOKUP one argument at a time, then see the errors each argument can cause. The sixth and later chapters of this tutorial cover the alternatives, so this chapter stays with VLOOKUP itself.
The sample data
Every example in this chapter uses a small product table on a sheet. Row 1 holds headers. The data runs from row 2 to row 6.
| Row | A | B | C |
|---|---|---|---|
| 1 | Product ID | Name | Price |
| 2 | P-101 | Widget | 4.50 |
| 3 | P-102 | Gadget | 12.00 |
| 4 | P-103 | Widget Pro | 18.25 |
| 5 | P-104 | Bracket | 2.10 |
| 6 | P-105 | Hinge | 3.75 |
The goal is to type a product ID somewhere else and get its price back. Without a lookup you would scan column A by eye, then read across to column C. VLOOKUP does that scan for you.
The three required arguments
VLOOKUP takes four arguments. Three are needed for every lookup, and the fourth controls the match type.
=VLOOKUP(search_key, range, index, [is_sorted])| Argument | What it is |
|---|---|
| `search_key` | The value you are looking for, such as `"P-102"` or a cell reference like `E2`. |
| `range` | The block of cells to search and to return values from. |
| `index` | The position of the result column inside `range`, counted from 1. |
| `is_sorted` | Optional. `FALSE` asks for an exact match. The next chapter explains this argument in full. |
Here is the first working formula. It uses FALSE for the last argument so that only an exact match counts.
=VLOOKUP("P-102", A2:C6, 3, FALSE)The result is 12. Sheets does three things in order:
- It looks down the first column of
A2:C6, which is column A, for the textP-102. - It finds the match in the second row of the range, which is sheet row 3.
- It moves across that row to the third column of the range, which is column C, and returns the value there.
The search column is not something you choose. VLOOKUP always searches the leftmost column of the range you give it. That one rule explains most of what goes right and wrong with the function.
Understanding the column index
The index counts columns inside the range, not columns on the sheet. In the range A2:C6, column A is 1, column B is 2, and column C is 3. So VLOOKUP("P-102", A2:C6, 2, FALSE) returns Gadget, and index 3 returns the price.
The two ideas differ as soon as the range does not start in column A. Suppose the same data sat in columns D to F:
=VLOOKUP("P-102", D2:F6, 3, FALSE)The price is in column F, which is the third column of D2:F6. The index is still 3 and not 6. If you count from the sheet's own column letters, you will get the wrong column or an error.
An index of 1 is legal. It returns the key's own column, so VLOOKUP("P-102", A2:C6, 1, FALSE) returns P-102. This is occasionally useful as a check that a key exists, but you will rarely need it.
Using a cell as the key
Typing keys inside the formula is only good for demos. Real sheets keep the key in a cell. Put a product ID in E2 and write:
=VLOOKUP(E2, $A$2:$C$6, 3, FALSE)Change E2 and the result changes. The dollar signs on the range matter. If you copy this formula down from F2 to F3, the key reference moves from E2 to E3, which is what you want. A range written as A2:C6 would shift to A3:C7 and then A4:C8, and the table's first rows would silently drop out of the search. Lock the range with $ whenever you intend to fill a lookup down a column.
Choosing the range
Three habits keep the range correct.
Start at the first data row or include the header. Both work. With A2:C6 the header is outside the search. With A1:C6 the header is a searchable row, which is harmless unless a key might equal a header name. Pick one style and use it everywhere so your index numbers mean the same thing.
Cover every column you need, and only start at the key column. The range must begin at the column that holds the keys. It must also reach at least as far right as the column you want back. A range of A2:B6 cannot return a price from column C, however you set the index.
Leave room for growth. If rows will be added, use a range longer than the current data, such as $A$2:$C$1000. Blank rows at the bottom do no harm for exact matches. Alternatively, use whole columns:
=VLOOKUP(E2, A:C, 3, FALSE)Whole-column ranges grow with the data and need no $ to stay put in rows. They include the header row, and on very large sheets they can make recalculation slower. For a lookup table of a few thousand rows the difference is not noticeable.
What VLOOKUP does with duplicates and case
Two behaviors are easy to miss because they never produce an error.
- Duplicates. If the key appears more than once in the first column,
VLOOKUPreturns the value from the first matching row, reading top to bottom. The other rows are ignored with no warning. If your data may contain repeated keys, check for them before trusting the result. - Case. The comparison ignores letter case.
p-102,P-102, andp-102all match the same row.
Neither of these is a bug. They do mean that a lookup returning a plausible value is not proof that it found the row you intended.
Common errors from the three arguments
Each argument has a typical way of going wrong. Knowing the message tells you where to look.
| Symptom | Likely cause | Fix |
|---|---|---|
| `#N/A` with the message "Did not find value" | The key is not in the first column of the range, or it differs in some way, such as an extra space | Check the key against column A. Make sure the keys really are in the leftmost column of the range. |
| `#REF!` with "out-of-bounds range" | The index is larger than the number of columns in the range | Count the columns in the range again, or widen the range. |
| `#VALUE!` | The index is 0 or negative | Use an index of 1 or higher. |
| Wrong value, no error | The index points at the wrong column, or the key has duplicates | Check the index against the range's columns. Check for repeated keys. |
| Results shift or fail after copying down | The range was not locked with `$` | Use `$A$2:$C$6`. |
The out-of-bounds case often appears after someone deletes a column. If the range was A2:C6 and column C is removed, Sheets shrinks the range to A2:B6, and an index of 3 now points past the edge. An index that is typed as a number does not adjust when columns are inserted or removed, so it can also quietly point at a different column after an insert. Both problems are why chapter 4 looks at the limits of VLOOKUP.
A quick way to guard against a wrong index is to compute it instead of typing it. MATCH finds a header's position inside the range:
=VLOOKUP(E2, $A$1:$C$6, MATCH("Price", $A$1:$C$1, 0), FALSE)MATCH("Price", $A$1:$C$1, 0) returns 3 here, because "Price" is the third header. If a column is later inserted, the header moves and MATCH follows it. This formula needs the header row inside both ranges, so it uses A1:C6 and A1:C1.
Lookups that return text and numbers
VLOOKUP returns whatever is in the cell, and keeps its type. A price comes back as a number you can add up. A name comes back as text. You can place the lookup inside other formulas, for example to compute a line total:
=VLOOKUP(E2, $A$2:$C$6, 3, FALSE) * F2If E2 holds P-102 and F2 holds a quantity of 5, the result is 60. When the lookup fails, the #N/A flows through the multiplication and the whole cell shows the error. Handling that is the subject of a later chapter.
Putting it together
A complete, reusable pattern for the sample data looks like this. Cell E2 holds the product ID, and the formula goes in F2:
=VLOOKUP($E2, $A$2:$C$1000, 3, FALSE)The key reference locks the column, the range is locked and has room to grow, the index names the price column, and FALSE demands an exact match. Check each piece in that order when a result looks wrong: the key, then the range, then the index.
Exercises
Exercise
A table sits in H5:K50. Column H holds employee IDs, column I holds names, column J holds departments, and column K holds salaries. Write a formula that returns the department for the ID in cell M2, as an exact match, and that is safe to copy down.
Show answer
=VLOOKUP($M2, $H$5:$K$50, 3, FALSE) The department is the third column of H5:K50 (H is 1, I is 2, J is 3). The range is locked with $ so it does not shift when the formula is copied down, and FALSE asks for an exact match.
Exercise
Using the product table in A2:C6, you type =VLOOKUP("P-104", A2:C6, 4, FALSE). What error appears, and why?
Show answer
#REF! (out-of-bounds range). The range A2:C6 has only three columns, so an index of 4 points past its right edge.