XLOOKUP

The XLOOKUP function searches a range for a specified value and returns the value from the same row in another column.

XLOOKUP(search-value, search-range, return-range, if-not-found, match-type, search-type)

search-value: The value being searched for in search-range. search-value can contain any value, or a REGEX string.

search-range: The cells to search.

return-range: The cells to return.

if-not-found: An optional argument to specify the display message if a match is not found.

match-type: An optional argument that specifies the type of match to search for.

exact or next smallest (-1): If there’s no match, returns an error.

exact match (0 or omitted): If there’s no exact match, returns an error.

exact or next largest (1): If there’s no match, returns an error.

wildcard (2): *, ?, and ~ have a particular meaning. REGEX can only be used in XLOOKUP if you use wildcard.

search-type: An optional argument that specifies the order in which to search the range.

Binary descending (-2): Binary search that requires range to be sorted in descending order, otherwise returns an error.

Last to first (-1): Search the range from last to first.

First to last (1 or omitted): Search the range from first to last.

Binary ascending (2): Binary search that requires range to be sorted in ascending order, otherwise returns an error.

Notes

  • If either search-range or return-range is a spanning reference (such as "B"), headers and footers are automatically ignored.

Example

The table below, titled Products, lists products and their attributes, like size and price:

A

B

C

D

E

1

Product

Length (cm)

Width (cm)

Weight (kg)

Price

2

Product 1

16

17

10

$82.00

3

Product 2

16

20

18

$77.00

4

Product 3

11

11

15

$88.00

5

Product 4

15

16

20

$63.00

Search with XLOOKUP

With XLOOKUP, you can insert a formula in your spreadsheet that returns any associated value by first providing the product name, then the column with the value you want to return. For example, if you wanted to return the width of Product 1 in the table above, you could use the following formula, which returns 17 cm:

The formula editor showing the formula =XLOOKUP(Products::$A2,Products::A,Width).

In this formula, the following arguments are used:

  • search-value: Products::$A2, an absolute reference to the cell in the Products table containing Product 1.

  • search-range: Products::A, the column to search for Product 1.

  • return-range: Width, the column containing the value to return that is associated with Product 1.

  • match-type: Omitted. If match-type is omitted, XLOOKUP searches for an exact match by default.

Set the if-not-found string

If you wanted to search for a specific product length and return its matching width, as well as the string to return if no match is found, you could use the following formula, which returns "No match":

The formula editor showing the formula =XLOOKUP(13,Length,Width,"No match",0).

In this formula, the if-not-found argument is used to perform a more specific search:

  • search-value: 13, the value to search for in the range specified in search-range.

  • search-range: Length, the column to search in.

  • return-range: Width, the column containing the value to return if a match for search-value is found.

  • if-not-found: "No match", the string to display if a product with a length of 13 cm is not found.

  • match-type: exact match (0). This searches only for a length of 13 cm.

Find the next closest value

XLOOKUP can also provide a broad search based on a specific value and values close to it. If you change match-type from the formula above, you can return the width that matches a length of 13 cm, or the next smallest value. The formula below returns a width of 11 cm:

The formula editor showing the formula =XLOOKUP(13,Length,Width,"No match",1,-1).

In this formula, the arguments are the same as above, except a different value is used for match-type to change how the table is searched:

  • match-type: exact or next smallest (-1). This searches for either a length of 13 cm, and if that value is not found, finds the next smallest value in the Length column.

Change the search order

In some instances, it may be useful to change the order in which a table is searched with XLOOKUP. For example, in the table above, there are two products with a length of 16 cm, so there are two potential matches if you search for 16 cm in the Length column using search-value and search-range. You can set the search order using a formula like this, which returns 20 cm:

The formula editor showing the formula =XLOOKUP(16,Length,Width,"No match",1,-1).

In this formula, the search-type argument is used to set the order in which XLOOKUP searches the table for a match:

  • search-value: 16, the value to search for in the range specified in search-range.

  • search-range: Length, the column to search in.

  • return-range: Width, the column containing the value to return if a match for search-value is found.

  • if-not-found: "No match", the string to display if a product with a length of 16 cm is not found.

  • match-type: exact or next largest (1). This searches for either a length of 16 cm, and if that value is not found, finds the next largest value in the Length column.

  • search-type: Last to first (-1). This searches the column from the last value to the first value.

Use XLOOKUP with other functions

XLOOKUP can also be used with other functions, like SUM. For example, you can use a formula like the one below to return $247, the SUM of the prices of Products 1, 2, and 3:

The formula editor showing the formula =SUM(XLOOKUP(Products::$A2,Products::A,Price):XLOOKUP(Products::$A4,Products::A,Price)).

In this example, the first XLOOKUP searches for the price of Product 1, and the second XLOOKUP searches for the price of Product 3. The colon (:) between the XLOOKUP functions indicates that SUM should return not just the total price of Product 1 and Product 3, but also any values in between.

In the formula below, XLOOKUP is used with REGEX to return Product 2, the first product with a width that starts with "2":

The formula editor showing the formula =XLOOKUP(REGEX("^2.*"), Products::C2:C5, Products::A2:A5, FALSE,2).

In this example, "wildcard (2)" is used for match-type to utilize the wildcards in the REGEX function.

Additional examples

Given the following table:

A

B

C

1

Name

Age

Salary

2

Amy

35

71000

3

Matthew

27

81000

4

Chloe

42

86000

5

Sophia

51

66000

6

Kenneth

28

52000

7

Tom

49

62000

8

Aaron

63

89000

9

Mary

22

34000

10

Alice

29

52000

11

Brian

35

52500

=XLOOKUP(49,B2:B11,C2:C11) returns "62000," which is the salary of the first employee whose age is 49.

=XLOOKUP(60000,C2:C11,B2:B11,"No match") returns "No match," as there is no employee whose salary is $60,000.

=XLOOKUP(REGEX("^C.*"), A2:A11, B2:B11, FALSE, 2) returns "42", the age of "Chloe," the first employee in the range whose name starts with "C".

See alsoXMATCH
Morty Proxy This is a proxified and sanitized view of the page, visit original site.
Helpful?
Character limit: 250
Maximum character limit is 250.
Thanks for your feedback.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.