Sub topic 3
Exact and approximate match: why the last argument matters
This chapter covers the fourth argument of VLOOKUP, which decides whether the function looks for an exact match or the nearest one. It assumes you can enter formulas in Google Sheets, write ranges such as A2:C100, and lock ranges with $. It also assumes you have read the previous chapter on the VLOOKUP search key, range, and column index.
The fourth argument
The full syntax of VLOOKUP is:
=VLOOKUP(search_key, range, index, [is_sorted])The first three arguments say what to find, where to look, and which column to return. The fourth, is_sorted, is optional. It controls how the search is carried out:
| Value | Also written as | Mode | What it does |
|---|---|---|---|
| `FALSE` | `0` | Exact | Returns the row whose first-column value equals the key. Returns `#N/A` if none does. |
| `TRUE` | `1` | Approximate | Returns the row with the largest first-column value that is less than or equal to the key. It assumes the first column is sorted ascending. |
| (omitted) | Approximate | Same as `TRUE`. |
The last row is the source of most VLOOKUP bugs. If you leave the argument out, Sheets does not ask what you meant. It silently runs an approximate search.
Exact match
Exact match is what most people want. You have an ID, a name, or an email address, and you want the one row that carries it.
=VLOOKUP("P-102", A2:C100, 3, FALSE)Sheets scans the first column of A2:C100 for P-102. If it finds the value, it returns the third column of that row. If not, it returns #N/A.
A few properties of exact match are worth knowing:
- The data does not need to be sorted.
- Text comparison ignores case, so
p-102matchesP-102. - If the key appears more than once, you get the first match from the top.
- The key and the lookup values must be the same kind of value. The text
"102"does not match the number102.
An error here is useful. #N/A means "this key is not in the table", and you can handle it on purpose with IFNA, which a later chapter covers.
Approximate match
Approximate match does not look for equality. It looks for a position. It finds the last row whose first-column value is less than or equal to the key. This is the right tool when the table describes ranges rather than individual items.
Suppose a discount table lives in F2:G6. Each row gives the minimum order total for a tier and the discount for that tier:
| F (minimum total) | G (discount) |
|---|---|
| 0 | 0% |
| 1000 | 2% |
| 5000 | 5% |
| 10000 | 8% |
| 25000 | 10% |
The formula below looks up the order total in B2:
=VLOOKUP(B2, $F$2:$G$6, 2, TRUE)Here is what it returns for several totals:
| B2 | Row chosen | Result |
|---|---|---|
| 0 | 0 | 0% |
| 999 | 0 | 0% |
| 1000 | 1000 | 2% |
| 7500 | 5000 | 5% |
| 25000 | 25000 | 10% |
| 90000 | 25000 | 10% |
| -5 | none | `#N/A` |
An order of 7,500 matches no row exactly. The search finds the largest minimum that does not exceed 7,500, which is 5,000. An order larger than the last row still maps to the last row, so the top tier has no upper limit. A key smaller than the first value has no valid row, so it returns #N/A. That is why the table starts at 0.
Other tasks fit the same pattern:
- Tax brackets
- Shipping rates by weight
- Grade letters by score
- Commission rates by sales volume
Why the approximate search needs sorted data
Approximate match runs a binary search. Instead of checking every row, it jumps to the middle of the column. It compares the key to that value and discards half the rows. It repeats this on the remaining half. This is fast on large tables, but it only works if the first column is sorted ascending. If the data is out of order, discarding half the rows can throw away the row you needed.
With unsorted data, an approximate lookup can:
- Return a value from the wrong row.
- Return
#N/Afor a key that is plainly in the table. - Return the correct answer by luck, which hides the bug until the data changes.
The problem has no fixed symptom. It depends on how the values happen to be arranged. A formula can look right on your test data and fail after someone adds a row.
The silent failure: approximate match on an ID column
The most common mistake is forgetting FALSE when the intent was exact. Take a product table whose IDs are sorted:
| A (ID) | B (Name) | C (Price) |
|---|---|---|
| P-101 | Widget | 4.50 |
| P-103 | Gadget | 9.00 |
| P-105 | Gizmo | 12.25 |
Now run two lookups for an ID that does not exist:
=VLOOKUP("P-102", A2:C4, 3, FALSE)
=VLOOKUP("P-102", A2:C4, 3)The first returns #N/A, which is correct, because P-102 is not in the table. The second returns 4.50. No row equals P-102, so the approximate search settles for the largest ID that is smaller, which is P-101. The result looks like real data, and no error warns you. A report built on this formula would show the Widget's price for a product that does not exist.
Why the default is approximate
The default can look like a poor choice, but it comes from how lookups were first used. Early spreadsheet lookups were mostly range tables, like the discount table above, and many tables were large. Binary search on sorted data was much faster than scanning every row. The default also keeps old files working the same way after the function changed.
Today most lookups are by ID, and spreadsheets are fast enough that the speed difference rarely matters. Exact match is what you want most of the time, so treat FALSE as required.
Choosing the mode
Ask what the first column of your table represents:
- If each row is an individual thing (a product, an employee, an order), use exact match with
FALSE. - If each row is the start of a range (a threshold, a bracket, a tier), use approximate match with
TRUE, and sort the table ascending by the first column.
You can also test whether the key could ever fall between two rows. An order total can be 7,500, which lies between two thresholds, so approximate match fits. A product ID of P-102 cannot lie between P-101 and P-103. It is either a product or it is not, so exact match fits.
Some further practical points:
- Lock the table range with
$in tier formulas ($F$2:$G$6) so it does not shift when you copy the formula down a column. - Put the lowest tier first and make it small enough to cover every possible key, as the
0row does above. This prevents#N/Afrom keys below the table. - Sort by the first column only. The other columns can be in any order.
- Keep range tables on their own labeled area or sheet, so nobody re-sorts or inserts rows without noticing.
A comparison to carry forward
XLOOKUP, covered in a later chapter, changes this design. It matches exactly by default, and you choose a different mode only when you need one. Its approximate modes do not require sorted data. For now, the point for VLOOKUP is that a missing argument changes the algorithm, and you cannot tell by looking at the result.
Exercise
A table in A2:B5 holds employee IDs 10, 20, 30, 40 in column A, with names in column B. A colleague writes =VLOOKUP(25, A2:B5, 2) and gets the name for ID 20, with no error. Explain why, and rewrite the formula so it reports that ID 25 does not exist.
Show answer
The fourth argument is omitted, so it defaults to TRUE and VLOOKUP runs an approximate search. No row equals 25, so it returns the row with the largest ID not above 25, which is 20. The fix is to request exact match: =VLOOKUP(25, A2:B5, 2, FALSE). That returns #N/A because 25 is not in the table.
Exercise
A shipping table in E2:F5 has weight limits in E (0, 1, 5, 20) and the shipping fee in F (3, 5, 9, 20). Which VLOOKUP formula returns the fee for a parcel weighing 7 in cell B2, and what fee does it return? What would happen if the table started at 1 instead of 0 and the parcel weighed 0.5?
Show answer
=VLOOKUP(B2, $E$2:$F$5, 2, TRUE) returns 9, because the largest weight limit not above 7 is 5. If the first row were 1 and the parcel weighed 0.5, the key would be below the first value, so no row qualifies and the formula returns #N/A.