6 / 6 · Basic
Looking things up with VLOOKUP
Leave out the last argument of VLOOKUP and both Excel and Google Sheets use an approximate match. Google recommends typing FALSE.
Jump to the 11 practice questionsYou will learn
- Look up a code with an exact match
- Look up a band with an approximate match
- Read #N/A, #REF! and #VALUE! from VLOOKUP
Before this: AND, OR and nested IF
1Explain
A price list with codes
A shop keeps its prices in a small table: codes in A2:A5, item names in B2:B5, prices in C2:C5. At the till, someone types a code and wants the price. That is VLOOKUP's job. It finds a value in the first column of a table and returns something from the same row. Microsoft lists the four pieces it needs: the value to look up, the range it is in, the column number in that range that holds the answer, and whether you want an approximate (TRUE) or exact (FALSE) match.
2Explain
First column, count from 1
Two rules cause most of the trouble. First, the value must be in the first column of the range, and the answer must be to its right. VLOOKUP never looks left, so if the codes are in column B, the range has to start at B. Second, the column number counts from the first column of the range, not from column A of the sheet; in B2:D9, B is 1, C is 2 and D is 3. Both help pages give the same advice for copied formulas too. Make the range absolute, as in $A$2:$C$5, so it doesn't slide when filled down.
3Problem
Code 103
Hint
Row of 103, second column.
Solution
Answer: Banana
103 is in the second row; column 2 of the range holds Banana.
4Problem
Column E in C2:F20
The range is C2:F20. What column number returns values from column E?
Hint
C is 1, D is 2.
Solution
Answer: 3
Count from the range: C is 1, D is 2, E is 3.
5Problem
$A$2:$C$5
Why should the table range be written as $A$2:$C$5?
Hint
Fill down and watch the range.
Solution
Answer: So it does not slide when the formula is filled down
Both help pages recommend an absolute range for copied lookups.
6Explain
When the code isn't on the list
With FALSE, VLOOKUP looks for exactly the value you gave and returns the first row that matches. No match gives #N/A. A column number larger than the number of columns in the range gives #REF!, and one below 1 gives #VALUE!. Text lookups ignore case but not spaces: Google's help points out that " Apple", "Apple " and "Apple" are three different values. A number stored as text differs from the number too, so a code typed as '104 is not found by 104.
7Problem
Code 102, exact
Same table. What does =VLOOKUP(102, A2:B5, 2, FALSE) return?
Hint
102 is not on the list.
Solution
Answer: #N/A
Exact match, and 102 is not in the first column.
8Problem
Column 3 of a 2-column range
Same table. What does =VLOOKUP(103, A2:B5, 3, FALSE) return?
Hint
There is no third column.
Solution
Answer: #REF!
The range has only 2 columns, so column 3 does not exist.
9Explore
Wildcards and IFNA
With exact match, the wildcards ? and * work in both programs. ? stands for any one character and * for any run of characters, so "Ora*" finds Orange. When #N/A is a normal outcome, such as a code not yet on the price list, wrap the lookup in IFNA, which both programs have: =IFNA(VLOOKUP(E2, $A$2:$C$5, 3, FALSE), "not found"). IFERROR also works.
Drag to turn. Scroll or pinch to zoom.
10Problem
IFNA
Hint
Pencil is not on the list.
Solution
Answer: not found
Pencil is not in the list, so VLOOKUP gives #N/A and IFNA replaces it.
11Explore
Looking up a band instead of an exact code
TRUE, or no fourth argument at all, gives an approximate match, the default in both programs. Google's help describes the search: it runs down the first column until it finds a value larger than the search key, then stops on the row before. That suits bands. In a grade table with 0, 60, 70, 80 and 90 in the first column, a score of 85 runs past 80, meets 90, and returns B from the 80 row. The first column must be sorted from smallest to largest, and a value below the first row returns #N/A.
An approximate match keeps the last row whose first value is not larger than the score. Below the first row there is nothing, so #N/A.
const table = [[0, "F"], [60, "D"], [70, "C"], [80, "B"], [90, "A"]];
const score = 85;
let found = "#N/A";
for (const row of table) if (row[0] <= score) found = row[1];
console.log(found);B12Problem
85 in the grade table
Hint
The last band that is not above 85.
Solution
Answer: B
85 passes 80, meets 90, and steps back to the 80 row.
13Problem
Exactly 90
Hint
90 is not larger than 90.
Solution
Answer: A
90 is not larger than 90, so the 90 row is the last one it can use.
14Problem
Code 102, approximate
Hint
The row before the first larger code.
Solution
Answer: Apple
Approximate: it passes 101, meets 103 which is larger, and steps back to 101.
15Problem
No fourth argument
What happens when the fourth argument is left out?
Hint
The default in both programs.
Solution
Answer: Approximate match in both programs
range_lookup and is_sorted both default to TRUE.
16Explore
Practice in the grid
Drag to turn. Scroll or pinch to zoom.
| Microsoft Excel | Google Sheets | |
|---|---|---|
| Name of the 4th argument | range_lookup | is_sorted |
| Default when left out | TRUE (approximate) | TRUE (approximate) |
| Advice in the help page | sort the first column, or use FALSE | FALSE is recommended |
| XLOOKUP | Microsoft 365 and Excel 2021 onward; not in 2016 or 2019 | available |
| Unreadable formula | refused with a message | #ERROR! |
| Circular reference | warning, then 0 or the last value | #REF! |
| ROUND without digits | not accepted | rounds to a whole number |
| Arguments per function | up to 255 | any number |
17Explain
XLOOKUP
Both programs now also have XLOOKUP. It takes a lookup range and a separate result range, so the answer may sit to the left, and it uses an exact match unless told otherwise. Microsoft notes that XLOOKUP is not available in Excel 2016 or Excel 2019, so a file that must open in those versions should stay with VLOOKUP. The table above collects the differences between the two programs that this course has met.
18Problem
Versions without XLOOKUP
Which Excel versions do not have XLOOKUP?
Hint
Two older versions.
Solution
Answer: Excel 2016 and Excel 2019
Microsoft's XLOOKUP page: not available in Excel 2016 and Excel 2019.
19Practice
VLOOKUP in JavaScript
Finish vlookup(value, table, col, approx): with approx false, return column col (counting from 1) of the row whose first value equals value, or "#N/A". With approx true, use the last row whose first value is not larger than value. A col beyond the row gives "#REF!".
Banana
#N/A
Apple
0.8
#REF!const table = [
[101, "Apple", 1.5],
[103, "Banana", 0.5],
[104, "Orange", 1.01],
[107, "Pear", 0.8],
];
function vlookup(value, table, col, approx) {
let hit = null;
if (hit === null) return "#N/A";
return hit[col - 1];
}
console.log(vlookup(103, table, 2, false));
console.log(vlookup(102, table, 2, false));
console.log(vlookup(102, table, 2, true));
console.log(vlookup(107, table, 3, true));
console.log(vlookup(103, table, 4, false));
Hint
Columns count from 1 but arrays from 0, so use hit[col - 1].
A solution
const table = [
[101, "Apple", 1.5],
[103, "Banana", 0.5],
[104, "Orange", 1.01],
[107, "Pear", 0.8],
];
function vlookup(value, table, col, approx) {
if (col > table[0].length) return "#REF!";
let hit = null;
for (const row of table) {
if (approx ? row[0] <= value : row[0] === value) {
hit = row;
if (!approx) break;
}
}
if (hit === null) return "#N/A";
return hit[col - 1];
}
console.log(vlookup(103, table, 2, false));
console.log(vlookup(102, table, 2, false));
console.log(vlookup(102, table, 2, true));
console.log(vlookup(107, table, 3, true));
console.log(vlookup(103, table, 4, false));20Explain
Common mistakes
Leaving out FALSE when looking up a code.
The default is approximate match, so a code that isn't on the list can come back with the price from the row above it instead of #N/A.
Counting the column number from column A of the sheet.
It counts from the first column of the range. With C2:F20, column E is 3, not 5.
Filling a lookup down without $ on the range.
A2:C5 slides to A3:C6, A4:C7 and so on, and lower rows stop finding codes near the top of the table.
Wrapping every lookup in IFERROR.
It hides a #REF! from a wrong column number as well as the expected #N/A. IFNA hides only #N/A.
21
Summary
- VLOOKUP(value, range, column number, FALSE) for an exact match.
- The value must be in the first column; the column number counts from the range.
- TRUE (or nothing) is approximate: the first column must be sorted, and it returns the last row not larger than the value.
- Use an absolute range, IFNA for expected #N/A, and FALSE unless you mean bands.