Sub topic 3
Returning several columns at once
This chapter assumes you can enter formulas in Google Sheets, write ranges such as A2:C100, anchor ranges with $, and reference another sheet such as Products!A:A.
Returning several columns at once
Set the result range of XLOOKUP to span several columns. The formula returns the matching row across all of them and spills the values into the cells to the right.
=XLOOKUP(E2, A2:A100, B2:D100)> The cells to the right of the formula must be empty. If any are not, Sheets shows #REF!.
Looking up values on another sheet
Prefix the range with the sheet name and an exclamation mark. The formula can sit on one sheet while the lookup and result ranges live on another.
=XLOOKUP(A2, Products!A:A, Products!C:C, "Not found")Looking up a whole column of keys
Wrap VLOOKUP in ARRAYFORMULA to process many keys in one formula. The results fill down the column.
=ARRAYFORMULA(VLOOKUP(A2:A20, Products!A2:C100, 3, FALSE))Troubleshooting #N/A results
Most #N/A errors come from one of these causes:
- Hidden spaces. The key "P-102 " with a trailing space does not match "P-102". Wrap the key in TRIM.
- Numbers stored as text. The text "1005" does not match the number 1005. Convert the key with VALUE, or reformat the column so both sides share a type.
- Unsorted data with approximate match. This returns a wrong row, not #N/A. Use FALSE for exact match.
=VLOOKUP(TRIM(A2), Products!A2:C100, 3, FALSE)Choosing between VLOOKUP and XLOOKUP
Use XLOOKUP for most new work. It looks left, accepts a missing_value argument, and does not depend on a column number. VLOOKUP requires the key in the leftmost column, and its column index breaks when someone inserts a column.
Exercise
Return columns B to D for the key in E2, searching A2:A100, and show "Not found" when the key is missing.
Show answer
=XLOOKUP(E2, A2:A100, B2:D100, "Not found")