How to Use OFFSET and XMATCH Functions Together in Excel

Published on

We often use the OFFSET and XMATCH functions together to match a value in one column and return a corresponding value from another column in Excel. Additionally, we can use XMATCH to match multiple values and extract the intermediate values from another column.

When dealing with values between two points, the values to match may either both be distinct, or one value may be distinct while the other is repeated. Let’s explore these scenarios with examples of using OFFSET and XMATCH in Excel.

Example 1: OFFSET and XMATCH Basic Use in Excel

Imagine you have student names in column A and their total marks in column B. The range is A2:B4:

The following formula will return the marks of “Mike”:

=OFFSET(B2, XMATCH("Mike", A2:A4)-1, 0, 1, 1)
Using XMATCH function to offset rows in Excel

In this formula, OFFSET is used to return the value from a specified number of rows and columns away from a starting cell.

Syntax: OFFSET(reference, rows, cols, [height], [width])

The function XMATCH("Mike", A2:A4) finds the relative position of “Mike” in the range A2:A4, which is 2. Since Excel’s OFFSET uses zero-based indexing, we subtract 1 to get the correct row offset (rows).

The starting cell reference B2 (reference) is where the offset begins. If you change the reference to A2, the formula will return a value from column A instead.

This demonstrates the basic use of the OFFSET and XMATCH combination in Excel.

Example 2: OFFSET and XMATCH to Extract Values Between Two Distinct Values

In this example, the data includes student names as headers in column A, with their subject-wise marks listed below. Column B contains the corresponding marks. The range is A1:B13:

To extract all marks for “Mike” based on the names “Mike” and “Rose” as boundaries, use the following formula:

=OFFSET(B2, XMATCH("Mike", A2:A13), 0, XMATCH("Rose", A2:A13) - XMATCH("Mike", A2:A13) - 1, 1)
Using OFFSET and XMATCH functions to extract values between two distinct values in Excel

Here, XMATCH("Mike", A2:A13) locates the starting position of “Mike” (rows) in column A. Similarly, XMATCH("Rose", A2:A13) locates the starting position of “Rose”. The difference between these positions, minus 1, determines the height (number of rows) of the range to extract.

OFFSET starts at B2 (reference), moves down by the rows returned by XMATCH("Mike", A2:A13), and retrieves values from column B corresponding to the range between “Mike” and “Rose”.

This formula is useful for extracting intermediate values based on two distinct boundaries.

Example 3: OFFSET and XMATCH to Extract Values Between One Distinct Value and Another Duplicate Value

Sometimes, you may need to retrieve all marks for a student without specifying the name of the next student. For example, if you want all marks for “Rose” until the last subject “Chemistry,” use this formula:

=LET(
   xm_, XMATCH("Rose", A2:A13), 
   xmo_1, OFFSET(A2, xm_, 0, 10), 
   xm__, XMATCH("Chemistry", xmo_1), 
   OFFSET(B2, xm_, 0, xm__, 1)
)
Using OFFSET and XMATCH functions to extract values between one distinct value and another duplicate value in Excel

This formula uses LET to define intermediate variables and simplify the calculation.

The variable xm_ stores the position of “Rose” in column A, while xmo_1 offsets from A2 by this position and retrieves 10 rows (an arbitrary number larger than the expected rows). Then, xm__ calculates the position of “Chemistry” within this range.

Finally, OFFSET(B2, xm_, 0, xm__, 1) retrieves the marks for “Rose” up to “Chemistry.”

This approach is particularly useful when the second boundary value appears multiple times in the data.

Resources

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

How to Calculate Maximum Drawdown in Excel and Google Sheets

You can use the following dynamic array formula to calculate maximum drawdown (MDD) in...

Running Count with Structured References in Google Sheets

Running a count with structured references is achievable in Google Sheets tables using the...

Running Total with Structured Table References in Google Sheets

You can use two types of formulas to create a running total with structured...

Automate Multi-Colored Line Charts in Google Sheets

Usually, a line chart with a single line supports only one color. To create...

More like this

Excel Tutorial: Calculate Nights Between Dates in Each Month

When working with date ranges in Excel, you might need to calculate how many...

How to Create a Case-Sensitive Pivot Table in Excel

As you may know, Pivot Tables in Excel are case-insensitive by default. To create...

Marking Case-Sensitive Unique Values in Excel

Marking case-sensitive unique values provides several benefits compared to merely extracting them in an...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.