Sub topic 9
Returning several columns at once
This chapter covers how one XLOOKUP formula can return a whole row segment, how that result spills across cells, and what breaks it. It assumes you can write a basic XLOOKUP with a separate lookup range and result range, and that you are comfortable with ranges such as A2:D100 and absolute references such as $A$2:$D$100.
The idea: a result range with more than one column
In a basic XLOOKUP, the result range is a single column, so the formula returns one value. The result range can also be wider than one column. The formula then returns the matching row's values from every column in that range.
Suppose a sheet has a product table in A1:D100:
| Column | A | B | C | D |
|---|---|---|---|---|
| Header | ID | Name | Price | Stock |
| Row 2 | P-101 | Widget | 4.50 | 120 |
| Row 3 | P-102 | Gadget | 12.00 | 35 |
A single-value lookup for the price of P-102 looks like this:
=XLOOKUP("P-102", A2:A100, C2:C100)To get the name, price, and stock together, widen the result range to B2:D100:
=XLOOKUP("P-102", A2:A100, B2:D100)The lookup range is still one column. The result range is three columns wide. XLOOKUP finds P-102 at position 2 of A2:A100, then returns row 2 of B2:D100, which is Gadget, 12.00, and 35.
How the result spills
You type the formula in one cell. Google Sheets writes the first value in that cell and fills the neighbouring cells to the right with the remaining values. This behaviour is called a spill. The cells that receive the extra values do not contain formulas of their own. They display parts of the result of the formula in the first cell.
If the formula is in F2 and the result is three columns wide, Sheets uses F2, G2, and H2. You can click G2 and see the formula greyed out, which confirms it belongs to F2.
A typical layout for a lookup form puts the key in E2 and the formula next to it:
=XLOOKUP(E2, A2:A100, B2:D100, "Not found")Type a new ID in E2 and all three result cells update.
The two size rules
The ranges must fit together in a specific way:
- The lookup range and the result range must have the same number of rows.
A2:A100pairs withB2:D100because both cover rows 2 to 100. A mismatch such asA2:A100withB2:D99produces an error. - The width of the result range decides how many cells the formula fills. Three columns means three cells to the right.
The columns of the result range do not need to be the same columns as the lookup range. They can sit on either side of it. The result range can also include columns to the left of the key:
=XLOOKUP(E2, C2:C100, A2:B100)This looks up a value in column C and returns columns A and B from the same row. VLOOKUP cannot do this without extra work.
Blocked spills
A spilling formula needs empty cells to write into. If any target cell already contains data, the formula returns a #REF! error. Its message says the array result was not expanded because it would overwrite data in a named cell.
This happens often in practice. You build a lookup form, fill in a few cells by hand, and later widen the result range. The new, wider result now runs into a cell that you filled by hand.
To fix it, clear the cell named in the error message, or shrink the result range so it fits the space you have. Trailing spaces do not count as empty, so a cell that looks blank may still block the spill. Select it and press Delete.
Spilling also affects how you copy formulas. If you drag a multi-column XLOOKUP down a column to look up several keys, each copy spills to the right on its own row. That works as long as each row has free space to its right. It fails if the copies overlap with cells that already contain data.
Choosing which columns to return
You control the output by choosing the result range. This gives you several patterns.
All columns in a block. Use the full block, as in B2:D100.
One column. Use a single column, as in C2:C100. This is the basic form.
Columns in a different order. XLOOKUP returns columns in the order they appear in the result range. If you want stock before price, you need to reorder them. One way is to build the result range with an array literal. The curly braces join ranges side by side:
=XLOOKUP(E2, A2:A100, {D2:D100, C2:C100})This returns stock first, then price. Each part of the array must have the same number of rows as the lookup range.
Columns that are not adjacent. The same array literal skips columns you do not want. For example, to get the name and the stock but skip the price:
=XLOOKUP(E2, A2:A100, {B2:B100, D2:D100})The array literal is a good way to get a custom layout. The cost is that the formula is longer and harder to read, so use it only when the plain block does not fit.
Laying the result out vertically
By default the values spill to the right. Sometimes you want them to go down, for example in a two-column "field and value" card. Wrap the XLOOKUP in TRANSPOSE:
=TRANSPOSE(XLOOKUP(E2, A2:A100, B2:D100))The result now fills three cells going down instead of three cells going across. The same rule about blocked spills applies, now to the cells below the formula.
You can place the headers in a column next to the result to label it. Type Name, Price, and Stock in E4:E6 and put the TRANSPOSE formula in F4.
Missing keys with several columns
You can still pass a fourth argument for missing keys:
=XLOOKUP(E2, A2:A100, B2:D100, "Not found")With a multi-column result, the "Not found" text is a single value. Do not assume it fills all three cells. It is safest to test your own sheet by typing a key that does not exist and looking at which cells show the message. If you want the other cells to stay blank, check the result and clear or format them as needed. If you only care that the user sees a message, one cell is enough.
If you would rather see a blank than a message, pass an empty string "" as the fourth argument.
Doing the same thing with VLOOKUP
VLOOKUP returns one column per formula, so getting three columns means one of two approaches.
The first approach is three separate formulas, each with a different column index:
=VLOOKUP($E2, $A$2:$D$100, 2, FALSE)
=VLOOKUP($E2, $A$2:$D$100, 3, FALSE)
=VLOOKUP($E2, $A$2:$D$100, 4, FALSE)The second approach is to wrap VLOOKUP in ARRAYFORMULA and pass an array of column indexes:
=ARRAYFORMULA(VLOOKUP(E2, A2:D100, {2, 3, 4}, FALSE))This spills across three cells, like the XLOOKUP version. It still has the usual VLOOKUP limits. It counts columns by number, so inserting a column inside the range shifts the numbers and can silently return the wrong data. It also cannot look to the left of the key column.
XLOOKUP avoids the counting problem, because the result range moves with the sheet when you insert columns.
| Task | VLOOKUP | XLOOKUP |
|---|---|---|
| Three adjacent columns | `{2,3,4}` inside ARRAYFORMULA | One block range such as `B2:D100` |
| Columns left of the key | Not possible directly | Choose any result range |
| Survives inserted columns | No, index numbers shift | Yes, references adjust |
| Custom column order | Change the index array | Use an array literal |
Common mistakes
- Mismatched row counts. The lookup range
A2:A100and result rangeB2:D90have different heights. Make both end on the same row. - Putting the formula where it cannot spill. The formula needs free cells to the right, or below if you use TRANSPOSE. Leave room or choose another location.
- Referencing a spilled cell as if it held a formula. The cells to the right of the formula hold only results. If you overwrite one, the spill breaks and the formula shows
#REF!. - Treating the key range as part of the result. If you include the key column in the result range, the key itself is returned along with the other values. That is sometimes useful, but check it is what you intended.
- Forgetting absolute references when copying. If you copy the formula down for several keys, anchor the ranges with
$, as in$A$2:$A$100and$B$2:$D$100, so they do not slide.
Summary
A result range with several columns makes XLOOKUP return the matching row's values across adjacent cells in one formula. The lookup range and result range must have the same number of rows, and the cells to the right must be empty. Use TRANSPOSE to lay the result out downward, and use array literals to reorder or skip columns. VLOOKUP can approximate this with ARRAYFORMULA and an array of column numbers, but it keeps its column-counting and left-lookup limits.
Exercise
A sheet has IDs in A2:A100, names in B2:B100, prices in C2:C100, and stock counts in D2:D100. You want one formula in G2 that looks up the ID in F2 and returns the stock count first, then the name, with "Not found" for a missing ID. Write the formula. Then say what error you get if H2 already contains the text "old" and why.
Show answer
=XLOOKUP(F2, A2:A100, {D2:D100, B2:B100}, "Not found"). The array literal puts stock before name, and both parts have the same 99 rows as the lookup range. If H2 contains "old", the formula needs H2 to hold its second value, but the cell is not empty, so the spill is blocked and G2 shows #REF!. Clearing H2 fixes it.