Sub topic 1
What a lookup does
A lookup formula takes one value, finds it in a column of your data, and returns a related value from the same row. This chapter builds the mental model that the rest of the tutorial uses for both VLOOKUP and XLOOKUP. You will see the three parts of every lookup, how a lookup finds its answer, and the data conditions that make lookups fail. You will not write detailed VLOOKUP or XLOOKUP formulas yet. The next chapters cover that.
Prerequisites
This chapter assumes you can already do the following in Google Sheets:
- Enter a formula in a cell by starting it with
=. - Read and write range references such as
A2:C100. - Use absolute references with
$, for example$A$2:$C$100, so a range does not shift when you copy a formula down. - Refer to another sheet in a formula, for example
Products!A2:C100.
If any of these is unfamiliar, review it first. Lookup formulas combine all four, and most lookup mistakes in practice are reference mistakes.
The problem a lookup solves
Suppose you keep an order list with a product ID in each row, and a separate product table that holds each ID's name and price. The order list has the ID but not the price. You could find each price by eye, then type it in. That is slow, and it breaks as soon as a price changes.
A lookup automates the same steps you would do by eye:
- Take the ID from the order row.
- Scan the product table's ID column until you find that ID.
- Move across that row to the price column.
- Copy the price back into the order row.
Because the formula reads the table each time, the order list updates when the table changes.
The three parts of a lookup
Every lookup, in any function, has the same three parts.
| Part | What it is | Example |
|---|---|---|
| Search key | The value you already have and want to find | `"P-102"`, or a cell such as `E2` |
| Lookup column | The column that is searched for the key | The ID column, `A2:A100` |
| Result | The value you want from the matching row | The price, from column `C` |
Keep the names straight, because the tutorial uses them throughout. The search key is what you look for. The lookup column is where you look. The result column is what you get back.
Here is a small product table. Assume it sits in A1:C6, with headers in row 1.
| ID (A) | Name (B) | Price (C) |
|---|---|---|
| P-101 | Widget | 4.50 |
| P-102 | Gadget | 12.00 |
| P-103 | Widget Large | 7.25 |
| P-104 | Bracket | 2.10 |
| P-105 | Hinge | 3.40 |
If the search key is P-102, the lookup column is A2:A6, and the result column is C2:C6, the answer is 12.00.
How the match works: position, not magic
A lookup is two steps, and it helps to name them separately.
- Find the position of the key in the lookup column. In the table above,
P-102is the second item inA2:A6, so its position is 2. - Return the item at that same position in the result column. The second item in
C2:C6is12.00.
The result comes from "the same row" only because both columns have the same height and line up row by row. A lookup never checks that the rows belong together. It trusts the alignment of your data.
This two-step model explains much of what comes later:
- VLOOKUP does both steps inside one range and counts columns to reach the result.
- XLOOKUP takes the lookup column and the result column as two separate arguments. This is why it can return values from a column to the left of the lookup column.
- Approximate matching changes how step 1 decides that a key "matches". Step 2 stays the same.
A first look at the two functions
You do not need to memorize these yet. They show how the same three parts appear in each function. The tutorial explains every argument in later chapters.
=VLOOKUP("P-102", A2:C6, 3, FALSE)This searches the first column of A2:C6 for P-102 and returns the value from the third column of that range. The result is 12.00.
=XLOOKUP("P-102", A2:A6, C2:C6)This searches A2:A6 for P-102 and returns the value in the same position in C2:C6. The result is also 12.00.
Both formulas have the same search key. They differ in how they point to the lookup column and the result. VLOOKUP says "search the first column of this block and count over to column 3". XLOOKUP says "search this column and return from that column".
Rules every lookup follows
These behaviors are shared by lookups in Google Sheets and are the source of many surprises.
Only one row comes back
A lookup returns a single result for a single key. If the key appears in several rows, a standard lookup returns the first match it finds, searching from the top. The later rows are never examined. If your IDs are supposed to be unique but one is duplicated by mistake, the lookup returns the first row's data and gives no warning.
Text matching ignores case
Text comparison in these functions is not case-sensitive. A lookup for p-102 finds P-102. This is usually convenient. It means you cannot use a lookup alone to tell ab and AB apart.
Types must agree
A lookup compares values, and the number 102 is not the same as the text "102", even though both display as 102. If your key is stored as text and the lookup column holds numbers, or the reverse, the match fails. This is one of the most common causes of #N/A, and the troubleshooting chapter covers how to spot it.
Invisible characters count
A cell containing "P-102 " with a trailing space is a different value from "P-102". The two look identical on screen, but the lookup treats them as different. Data pasted from other systems often carries these extra spaces.
A missing key produces #N/A
If the key is not found, the lookup returns the error #N/A. This is the correct behavior, because the lookup has no answer to give. It is also the result you see when the key is present but fails to match because of one of the types or spaces issues above. The error alone does not tell you which situation you are in. Chapter 6 shows how to replace #N/A with a readable message. Chapter 12 shows how to diagnose it.
Exact and approximate matching: a preview
So far, "find the key" has meant finding an identical value. That is an exact match, and it is what you want for IDs, names, and codes.
Some lookups need a different rule. Consider a tier table that gives a discount rate for order totals:
| Minimum order (F) | Discount (G) |
|---|---|
| 0 | 0% |
| 100 | 5% |
| 500 | 10% |
| 1000 | 15% |
An order of 640 appears nowhere in column F, yet it should get 10%. The rule is "use the largest minimum that does not exceed the order total". That is an approximate match. The lookup finds the closest value at or below the key, then reads the same position in the result column.
Exact and approximate matching are two settings of step 1 in the two-step model. The next chapters explain how each function lets you choose between them. Sheets users run into a particular trap here, because VLOOKUP's default is approximate. For now, remember that approximate matching is useful for ranges and tiers, and dangerous for IDs.
Setting up data so lookups work
A lookup is only as reliable as the table behind it. These habits prevent most problems before you write a formula.
- Keep one record per row. Each row should describe one item, with the ID in one column and its attributes in the others.
- Make keys unique. If an ID can repeat, decide in advance which row you want, and expect the first one by default.
- Keep key formats consistent. Do not mix text and numbers in the same key column. Do not allow stray spaces.
- Use a header row, but leave it out of the lookup range. Start ranges at row 2, as in
A2:C100, so a header can never be matched. - Anchor the table range. When you copy a lookup formula down a column, write the table as
$A$2:$C$100so it does not drift. The search key should stay relative, such asE2, so each row looks up its own value. - Leave room to grow. If the table will gain rows, use a range larger than the current data, such as
A2:C100for a table that currently ends at row 6. Blank rows at the bottom of the range do no harm.
The last two habits use the absolute references from the prerequisites. A lookup copied down a column with a non-anchored table range returns correct values for the first few rows, then starts returning #N/A as the range slides off the data.
When a lookup is the right tool
Use a lookup when you have a key in one place and related data in another, and you want the link to stay live. Typical cases are prices by product ID, department by employee number, rates by tier, and status by order number.
A lookup is not the right tool when you want to combine or total many rows that share a key. That is a summing or filtering task, and functions such as SUMIF and FILTER fit it better. A lookup answers "what is the value for this one key?" It does not answer "what are all the values for this key?"
Exercise
In the product table above (IDs in A2:A6, names in B2:B6, prices in C2:C6), a cell contains the key "P-104". Name the search key, the lookup column, and the result column for a lookup that returns the price. What value comes back? Then say what you expect if the key cell actually holds "P-104 " with a trailing space, and why.
Show answer
The search key is "P-104", the lookup column is A2:A6 (the ID column), and the result column is C2:C6 (the price column). The key is in position 4 of the lookup column, so the lookup returns the 4th item of C2:C6, which is 2.10. With a trailing space the key becomes "P-104 ", which is a different value from "P-104", so no cell in A2:A6 matches an exact lookup and the result is #N/A. The data looks correct on screen, but the invisible space prevents the match.
Exercise
A product ID appears twice in your lookup column, in rows 12 and 40, with different prices. A standard lookup for that ID searches from the top. Which price does it return, and does Sheets warn you about the duplicate?
Show answer
It returns the price from row 12, the first match from the top. Sheets gives no warning about the duplicate, so a duplicated key can silently return the wrong row's data. Check that keys are unique before relying on the lookup.