XMATCH Function in Excel

Master the XMATCH function with practical examples and error solutions. Learn how to find exact and approximate matches in Excel 365 and Google Sheets.

ExcelExcel
Google SheetsGoogle Sheets
lookup
intermediate
Syntax Preview
ExcelExcelGoogle SheetsGoogle Sheets
=XMATCH(lookup_value, lookup_array, [match_mode], [search_mode])
Comprehensive Explanation

Practical Examples

Basic Exact Match Lookup

Find the position of a product ID in an inventory list

Result: 15

Reverse Search - Find Last Occurrence

Find the position of the most recent transaction for a customer in a transaction log

Result: 856

Approximate Match - Next Smaller Value

Assign grade brackets based on student scores in a grading table

Result: 3

Wildcard Search for Partial Matches

Find products containing specific keywords using wildcard pattern matching

Result: 23

Two-Way Lookup with INDEX and XMATCH

Create a dynamic two-way lookup replacing VLOOKUP/HLOOKUP limitations

Result: $285,000

Common Errors and Solutions

#N/A

XMATCH cannot find the lookup value in the array

Cause:

The lookup value doesn't exist in the lookup array, or there's a data type mismatch (number stored as text, extra spaces, different formatting)

Solution:

**Step 1:** Verify the lookup value exists in the array using Ctrl+F (Windows) or Cmd+F (Mac) to manually search. **Step 2:** Check for extra spaces by wrapping your lookup value with TRIM: `=XMATCH(TRIM(A1), B:B, 0)` **Step 3:** Ensure data types match. Numbers stored as text won't match numeric values. Use VALUE to convert text to numbers: `=XMATCH(VALUE(A1), B:B, 0)` **Step 4:** Check for hidden characters or formatting differences that might prevent matching. **Step 5:** Use IFERROR to handle missing values gracefully: `=IFERROR(XMATCH(A1, B:B, 0), "Not Found")` **Prevention Tips:** - Always validate data types match before creating lookup formulas - Use data validation to ensure consistency in lookup columns - Clean imported data with TRIM and VALUE functions - Document expected data formats in your workbook

Prevention:

Always validate data types and clean data before lookups. Use data validation to ensure consistency.

Frequency: 55%

Example:

#VALUE!

Invalid match_mode or search_mode parameter

Cause:

Using invalid values for match_mode (must be -1, 0, 1, or 2) or search_mode (must be -1, 1, -2, or 2). Also occurs if lookup_array is not a single row or column.

Solution:

**Step 1:** Verify match_mode is one of these valid values: - 0 = Exact match (default) - -1 = Exact match or next smaller item - 1 = Exact match or next larger item - 2 = Wildcard character match **Step 2:** Verify search_mode is one of these valid values: - 1 = Search first to last (default) - -1 = Search last to first - 2 = Binary search, lookup_array sorted ascending - -2 = Binary search, lookup_array sorted descending **Step 3:** Ensure lookup_array is a single row or single column, not a 2D range. XMATCH cannot search multi-dimensional arrays. **Step 4:** Check for typos in parameter values or cell references that might contain invalid numbers. **Prevention Tips:** - Use dropdown lists or named constants for mode parameters - Document which mode values are valid in your workbook - Test formulas with sample data before applying to full datasets

Prevention:

Use parameter constants or clearly document which values are valid. Always test formulas with sample data.

Frequency: 20%

Example:

#N/A with Binary Search

Binary search fails to find the value

Cause:

Using search_mode 2 (binary ascending) or -2 (binary descending) on unsorted data. Binary search requires data to be properly sorted to work correctly.

Solution:

**Step 1:** If using search_mode = 2, verify your data is sorted in ascending order (A to Z, 0 to 9). Use Excel's Sort feature on the lookup column. **Step 2:** If using search_mode = -2, verify your data is sorted in descending order (Z to A, 9 to 0). **Step 3:** Check for any gaps or inconsistencies in the sorting. Even one out-of-place value can break binary search. **Step 4:** For unsorted data, use search_mode = 1 or -1 instead. These perform linear searches that work on any data order but are slower on large datasets. **Step 5:** If you need binary search performance, sort your data permanently or use a helper column with sorted references. **When to Use Binary Search:** Binary search (modes 2 or -2) is 10-100x faster on large datasets (1,000+ rows) but requires sorted data. Only use these modes when you're certain data is properly sorted and will remain sorted. **Prevention Tips:** - Document sorting requirements when using binary search modes - Set up data validation rules to prevent out-of-order entries - Consider using dynamic sorting with the SORT function - Test binary search formulas after any data changes

Prevention:

Only use binary search (modes 2 or -2) when you're certain data is properly sorted. Document sorting requirements in your workbook.

Frequency: 15%

Example:

Best Practices and Advanced Techniques

Always Use XMATCH with INDEX for Lookups

While XMATCH returns positions, its real power shines when combined with INDEX to retrieve values. This INDEX/XMATCH combination is the modern replacement for VLOOKUP and HLOOKUP, offering unlimited flexibility in lookup directions and no column number maintenance. The pattern is simple: INDEX returns a value from a range at a specific position, and XMATCH provides that position. Together, they create flexible, maintainable lookup formulas that adapt as your data structure changes. **Benefits:** - No column counting required (VLOOKUP's biggest pain point) - Bi-directional lookups (search left, right, up, down) - More maintainable when columns are inserted or deleted - Easier to audit and debug - Works with dynamic table structures

Use Binary Search for Large Datasets

When working with sorted data containing thousands of rows, use search_mode = 2 (ascending) or -2 (descending) for dramatically faster performance. Binary search can be 10-100 times faster than linear search on large datasets. Binary search works by repeatedly dividing the search range in half, making it exponentially faster as data size increases. For 10,000 rows, linear search makes an average of 5,000 comparisons while binary search makes only about 14. **Performance Comparison:** - 1,000 rows: Linear ~0.02s, Binary ~0.0002s (100x faster) - 10,000 rows: Linear ~0.2s, Binary ~0.002s (100x faster) - 100,000 rows: Linear ~2s, Binary ~0.02s (100x faster) **Critical Requirement:** Data MUST be sorted correctly for binary search to work. Using binary search on unsorted data returns incorrect results without errors.

Leverage Reverse Search for Latest Values

Use search_mode = -1 to find the last occurrence of a value instead of the first. This is perfect for finding the most recent transaction, latest status update, or current price without having to sort your data. Reverse search is particularly valuable in transaction logs, audit trails, and time-series data where new entries are appended to the bottom. Instead of sorting by date (which can be slow on large datasets), simply search backward to find the latest entry. **Common Use Cases:** - Finding the most recent customer transaction - Getting the latest product price from a price history - Locating the current status in a status update log - Retrieving the latest inventory count from a daily snapshot table **Performance Tip:** Reverse search on unsorted data is just as fast as forward search - both are O(n) operations.

Match Mode Requires Proper Sorting

When using match_mode = -1 (exact or next smaller), data must be sorted in **descending** order. When using match_mode = 1 (exact or next larger), data must be sorted in **ascending** order. Unsorted data will return incorrect results without error messages. This is one of the most common XMATCH mistakes because the function won't throw an error - it will simply return the wrong position. Always double-check your sort order when using approximate matching. **Sort Requirements:** - match_mode = -1: Sort descending (90, 80, 70, 60...) - match_mode = 1: Sort ascending (0, 60, 70, 80, 90...) - match_mode = 0 or 2: No sorting required **Pro Tip:** Use Excel's Sort & Filter features to ensure proper sorting before implementing approximate match formulas. Consider protecting sorted ranges to prevent accidental reordering.

Wildcard Search Limitations

Wildcard matching (match_mode = 2) only works with text values and supports two wildcards: * (matches any number of characters) and ? (matches exactly one character). Wildcard mode cannot be combined with approximate match modes (-1 or 1). **Wildcard Character Guide:** - `*` (asterisk): Matches zero or more characters - "*Smith" finds "Smith", "John Smith", "Blacksmith" - "Wireless*" finds "Wireless", "Wireless Mouse", "Wireless123" - "*2024*" finds any text containing "2024" - `?` (question mark): Matches exactly one character - "Col?r" finds "Color" and "Colour" - "200?" finds "2000", "2001", "2009" but not "200" or "20000" **Important Notes:** - Wildcards only work with text, not numbers or dates - Searches are case-insensitive by default - Cannot combine wildcard mode with approximate matching - Use escape character ~ to search for literal * or ? characters

XMATCH vs MATCH vs VLOOKUP

Need Help with XMATCH Function in Excel?

Stop struggling with formula syntax. Use AskFormulas to generate validated formulas instantly with our AI-powered tool.

Example Excel formula:

Related Formulas

INDEX Function Excel & Sheets

Master INDEX to retrieve values from specific positions in arrays. Learn array manipulation with practical examples and solutions.

intermediate
lookup
ExcelExcel
Google SheetsSheets
Validated
MATCH Function

Find value positions in Excel/Sheets with MATCH. Learn syntax, examples, errors, and combine with INDEX for powerful lookups.

intermediate
lookup
ExcelExcel
Google SheetsSheets
Validated
VLOOKUP Function Guide

Master the VLOOKUP function with practical examples and error solutions. Learn how to search and retrieve data efficiently in Excel and Google Sheets.

intermediate
lookup
ExcelExcel
Google SheetsSheets
Validated
CHOOSE Function in Excel

The CHOOSE function returns a value from a list based on index number. Learn with practical examples and error solutions for Excel and Sheets.

beginner
lookup
ExcelExcel
Google SheetsSheets
Validated