Sub topic 9
Returning Several Columns at Once
Prerequisites and the basic idea
This chapter assumes that you can enter Google Sheets formulas, use cell and range references, and understand absolute references such as $A$2:$A$100.
A lookup uses a key to find one row and return related data. For example, a product ID in E2 can identify a product in column A. You might want to return its name, category, and price from columns B, C, and D.
XLOOKUP can return all of these columns with one formula. The result fills neighboring cells automatically. This behavior is sometimes called spilling.
Consider this table:
| Product ID | Name | Category | Price |
|---|---|---|---|
| P-101 | Desk Lamp | Lighting | 24.99 |
| P-102 | Wide Monitor | Displays | 249.00 |
| P-103 | USB Hub | Accessories | 18.50 |
If E2 contains P-102, enter this formula in F2:
=XLOOKUP(E2, A2:A100, B2:D100, "Not found")Google Sheets searches A2:A100 for the value in E2. It returns the matching row from B2:D100, so the result appears across F2:H2:
| F | G | H |
|---|---|---|
| Wide Monitor | Displays | 249.00 |
The lookup range and result range must have compatible row counts. Here, A2:A100 has 99 rows, and B2:D100 also has 99 rows.
How the result spreads
The result range can contain adjacent columns. XLOOKUP returns the values from the matching row in the same order as the result range.
=XLOOKUP(E2, A2:A100, B2:D100)This formula uses the default missing-value behavior. If the key is absent, Google Sheets returns #N/A. Add a message when a missing key should be understandable to someone reading the sheet:
=XLOOKUP(E2, A2:A100, B2:D100, "Product not found")The formula returns one row containing three values. It does not return three separate matches. All returned values come from the same row that matched E2.
Avoiding spill errors
The cells to the right of the formula must be empty. If G2 or H2 already contains data, the result cannot expand into those cells. Google Sheets displays a spill-related error instead of overwriting existing content.
Before entering a multi-column lookup:
- Choose the first result cell.
- Check that enough cells to the right are empty.
- Enter the formula only in the first cell.
- Do not copy the formula into each result column.
If you need to return five columns, select a five-column result range, such as B2:F100. The order of those columns determines the order of the returned values.
Why this differs from VLOOKUP
VLOOKUP returns one column selected by a numeric column index:
=VLOOKUP(E2, A2:D100, 2, FALSE)To return three columns with VLOOKUP, you usually write three formulas with different indexes:
=VLOOKUP(E2, A2:D100, 2, FALSE)
=VLOOKUP(E2, A2:D100, 3, FALSE)
=VLOOKUP(E2, A2:D100, 4, FALSE)XLOOKUP is more direct because its result range can contain multiple columns. It also avoids counting the result column from the left edge of the table.
Keep the XLOOKUP formula in the first result cell and leave the rest of the output area available for the returned values.
Exercise
In E2, the key P-102 should return the product name, category, and price from columns B through D. What formula should you enter in F2?
Show answer
=XLOOKUP(E2, A2:A100, B2:D100, "Not found")