Sub topic 11
Looking up a whole column of keys
This chapter assumes you can write a single VLOOKUP or XLOOKUP, use $ absolute references, and point a formula at another sheet. Here you will replace a column of copied formulas with one formula that looks up every key at once. You will also handle the problems that come with that approach: empty rows, missing keys, hidden spaces, and blocked output.
Why one formula per column
The usual way to look up many keys is to write a formula in the first row and drag it down. Each row then holds its own copy:
=VLOOKUP(A2, Products!$A$2:$C$100, 3, FALSE)This works, but it has costs:
- New rows need the formula copied into them. If you forget, the column has gaps.
- Someone can overwrite one cell with a typed value, and nobody notices.
- Sorting, filtering, or inserting rows can leave a column with mixed formulas.
- A change to the formula means refilling the whole column.
An array formula is a formula that returns many values from one cell. The result fills the cells below and to the right. That cell is the only place the formula lives, so you maintain one formula instead of hundreds.
The ARRAYFORMULA pattern
ARRAYFORMULA tells Google Sheets to evaluate its contents for every item in a range instead of only the first. If you give VLOOKUP a range of keys where it expects one key, wrapping it in ARRAYFORMULA makes it look up each key and return a column of results:
=ARRAYFORMULA(VLOOKUP(A2:A20, Products!A2:C100, 3, FALSE))Put this in B2. Sheets fills B2:B20 with the third-column value for each key in A2:A20. Only B2 contains the formula. The other cells display its output, and you cannot edit them directly.
The XLOOKUP version has the same shape:
=ARRAYFORMULA(XLOOKUP(A2:A20, Products!A2:A100, Products!C2:C100, "Not found"))The lookup range and result range stay fixed at their original size. Only the search keys are an array.
Absolute references mostly stop mattering
When you drag a formula down, $ keeps the lookup table from shifting. A single array formula is never dragged, so the table range does not move. Using $ is still harmless. It protects you if someone later copies the formula to another cell.
The key range does matter. A2:A20 fixes the formula to 19 rows. If you add keys in A21, the formula does not see them. The next section covers that.
Handling a growing column
An open-ended range such as A2:A means "from A2 to the bottom of the column". The formula then covers new rows without any edit:
=ARRAYFORMULA(VLOOKUP(A2:A, Products!A2:C100, 3, FALSE))This creates a problem. The range includes the empty cells below your data, and a lookup for an empty key finds nothing. Every blank row would show #N/A, all the way to the bottom of the sheet.
Fix it by returning a blank when the key is blank:
=ARRAYFORMULA(IF(A2:A="", "", VLOOKUP(A2:A, Products!A2:C100, 3, FALSE)))IF checks each key. For a blank key it returns an empty string, and for any other key it runs the lookup. You can then add IFNA to catch keys that are present but missing from the table:
=ARRAYFORMULA(IF(A2:A="", "", IFNA(VLOOKUP(A2:A, Products!A2:C100, 3, FALSE), "Not found")))Read it from the inside out:
- VLOOKUP finds each key's value, or
#N/A. - IFNA replaces
#N/Awith "Not found". - IF skips blank keys so the empty rows stay empty.
The XLOOKUP equivalent needs no IFNA, because XLOOKUP has a missing_value argument:
=ARRAYFORMULA(IF(A2:A="", "", XLOOKUP(A2:A, Products!A2:A100, Products!C2:C100, "Not found")))Cleaning keys in bulk
The hidden-space problem from the troubleshooting chapter appears here at scale. TRIM removes leading and trailing spaces, and inside ARRAYFORMULA it works on the whole key column:
=ARRAYFORMULA(IF(A2:A="", "", IFNA(VLOOKUP(TRIM(A2:A), Products!A2:C100, 3, FALSE), "Not found")))TRIM changes only the keys used for the search. The values in column A stay as they were. TRIM does not fix numbers stored as text. If the table holds numeric IDs and your keys are text, convert them in the same formula, for example with VALUE(TRIM(A2:A)). That fails on non-numeric text, so only use it when every key is a number.
Mixed cases cause trouble. If some keys are blank and you wrap everything in VALUE, the blank string errors out. The IF test you already have prevents this, because blank keys never reach VALUE.
Where to put the formula
An array formula needs room. It writes its results into the cells below, and every one of those cells must be empty. Otherwise Sheets shows #REF! with the message "Array result was not expanded because it would overwrite data in B5". The fix is to clear the blocking cell or move the formula.
This is the most common failure after switching from dragged formulas. Old copies of the formula often remain further down the column. Clear everything below the cell that holds the array formula before you enter it.
Putting a header and the formula together
You can often avoid managing a separate header cell. Put the header text and the array in one formula in row 1, using curly braces to stack them vertically:
={"Price"; ARRAYFORMULA(IF(A2:A="", "", IFNA(VLOOKUP(A2:A, Products!A2:C100, 3, FALSE), "Not found")))}The semicolon inside the braces stacks the items in a column. The cell shows "Price" in B1, and the lookups start in B2. The header cannot be accidentally overwritten without also breaking the formula, so this is useful in shared sheets.
Several result columns
To pull more than one field for each key, you have two choices:
- Write one array formula per result column, changing the column index (2, 3, and so on) in each.
- Use a single formula that returns several columns. XLOOKUP can do this with a multi-column result range for one key, as shown in an earlier chapter.
For many keys with several fields, separate formulas per column are the simplest to read and debug. Each one fails or succeeds independently.
Limits of the approach
| Concern | What to know |
|---|---|
| Speed | Each key triggers its own search of the table. A few thousand keys against a few thousand rows is fine. Tens of thousands of keys against a large table can slow the sheet. |
| Editing results | You cannot edit one result cell. Change the formula in the top cell instead. |
| Blocked output | Any non-empty cell in the output area gives `#REF!`. |
| Open-ended ranges | They handle new rows but need the IF blank check. |
| Mixed data types | One text key in a numeric column gives `#N/A` for that row only. |
| Approximate match | Array lookups use the same match mode as a single lookup. If you leave off `FALSE`, you get approximate matching for every key, and wrong results with no error. |
Because every result comes from one formula, an error in the formula affects the whole column at once. Test the formula on a short range such as A2:A5 first. Then widen it to A2:A once the results look right.
A worked example
Suppose Orders has product codes in column A (from row 2) and you want a price in column B. The Products sheet has code in A, name in B, and price in C. Enter this in Orders!B1:
={"Price"; ARRAYFORMULA(IF(A2:A="", "", IFNA(VLOOKUP(TRIM(A2:A), Products!A2:C, 3, FALSE), "Not found")))}What happens when you add data:
- You type a new code in A12. The range
A2:Aalready covers it, so B12 fills in automatically. - You type a code with a trailing space. TRIM strips it, so the lookup still succeeds.
- You type a code that is not in Products. The cell shows "Not found" instead of
#N/A. - Rows below your data stay blank because of the IF check.
If a price is wrong, check three things in order: the key itself, the table range, and the column index. Because there is one formula, you only look in one place.
Exercise
A sheet has product codes in A2:A and you enter =ARRAYFORMULA(VLOOKUP(A2:A, Products!A2:C, 3, FALSE)) in B2. The rows with data work, but every empty row below shows #N/A. Rewrite the formula so empty rows stay blank and missing codes show "Not found".
Show answer
=ARRAYFORMULA(IF(A2:A="", "", IFNA(VLOOKUP(A2:A, Products!A2:C, 3, FALSE), "Not found"))). The IF returns an empty string for blank keys, and IFNA replaces the #N/A for keys missing from the table.
Exercise
You enter an array lookup formula in C2 and see #REF! with the message "Array result was not expanded because it would overwrite data in C6". What is wrong, and how do you fix it?
Show answer
Cell C6 is not empty, so the formula's output cannot fill the column. Clear C6 (and any other leftover values or old formulas below C2), or move the formula to a column where the cells below it are empty.
Exercise
Why does an array lookup that leaves off the last VLOOKUP argument return wrong results for unsorted tables without showing an error?
Show answer
The last argument defaults to TRUE, which means approximate match. Approximate match assumes the first column is sorted ascending and returns the nearest smaller value, so on unsorted data it returns a wrong but valid-looking result. Use FALSE for exact matching.