XMATCH Visible Rows in Google Sheets

Published on

We can XMATCH in visible rows (filtered data) without using a helper range in Google Sheets. The formula will exclude hidden rows.

This method only applies to match search keys in a column and return their relative positions if found or the #N/A error values.

The relative position may not be vital in most cases. We usually use the output to know whether a value is present in a list in a column.

But, if you are particular about the relative position, you must know what happens to it when you hide rows.

I will explain this first, and then we can go to how to XMATCH visible rows in Google Sheets.

Hidden Rows and Relative Position

Below you can find a few dates in cell range B2:B11 and their relative positions entered in A2:A11 for your reference.

XMATCH Visible Rows - Example

The relative position of the date 24/12/2022 in B2:B11 (lookup_range) is 6 if we use the formula to search it from the first entry to the last one.

Formula # 1 (E4):

=xmatch(E3,B2:B11,0,1)

If we hide row # 7, the XMATCH output will be the same as it doesn’t have the option to omit hidden rows.

How to get the relative position of the search key in the next visible row?

That is what we meant by XMATCH visible rows in Google Sheets.

Let’s hide row # 7.

The formula should match the search key in visible row # 9 and return its relative position, which is 8 if you count all the rows and 7 if you count only the visible rows.

Hidden Rows and Changes in Relative Position

We have solutions to both.

1. XMATCH Visible Rows and Count All Rows

Similar to XLOOKUP Visible (Filtered) Data, we require to use BYROW, one of the LAMBDA Helper Functions, and SUBTOTAL to add the said functionality to XMATCH.

We will use the following helper formula result to create a virtual lookup_range within the XMATCH to return the relative position of the visible rows.

Helper Formula:

=BYROW(B2:B11,LAMBDA(range,SUBTOTAL(103,range)))

It would return 1 in visible cells and 0 in hidden cells.

Note:- If you want to learn this formula, check the XLOOKUP tutorial mentioned above.

How do we use this helper formula in combination with XMATCH then?

We will virtually replace the dates in B2:B11 with blank if the helper formula returns 0, else B2:B11, and use that as the lookup_range within the XMATCH.

Generic Formula (Virtual Lookup_Range): if(helper_formula_result=0,"",B2:B11)

Formula # 2 (E5):

=ArrayFormula(xmatch(E3,if(BYROW(B2:B11,LAMBDA(range,SUBTOTAL(103,range)))=0,"",B2:B11),0,1))

We must use the ARRAYFORMULA function because of the non-array IF logical test in an array.

Syntax:

XMATCH(search_key, lookup_range, [match_mode], [search_mode])

search_key: E3

lookup_range: if(BYROW(B2:B11,LAMBDA(range,SUBTOTAL(103,range)))=0,"",B2:B11)

match_mode: 0

search_mode: 1

2. XMATCH Visible Rows and Count Visible Rows

Here also, we must modify the lookup_range using the BYROW helper formula.

Instead of the IF logical test, here, we will use the FILTER function.

We will filter visible rows (helper formula output >0) and use that as the lookup_range.

That will help the XMATCH to match the search key only in visible rows and return the relative position accordingly.

Here the ARRAYFORMULA function is not required, as FILTER can handle arrays.

Formula # 3 (E6):

=xmatch(E3,filter(B2:B11,BYROW(B2:B11,LAMBDA(range,SUBTOTAL(103,range)))>0),0,1)

Relative Position When Searching From the Last Entry to the First

When we have the latest records at the bottom of the table, in some cases, we may require to search from the last entry to the first entry.

What changes should we make to the formulas above?

The above formulas have no problem handling that.

Replace the search_mode, the last argument in the function, 1 with -1.

Keep row # 7 hidden and modify formulas 1 (E4), 2 (E5), and 3 (E6) as mentioned above.

The first one will return 8, and the other two XMATCH visible rows formulas will return 7 and 8, respectively.

Test it in your Sheet to understand it even better.

That’s all. Thanks for the stay. Enjoy!

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.

Lookup Values Under Categories in Excel and Google Sheets

We can use a combination of XLOOKUP and VLOOKUP in both Excel and Google...

Extract Items Under a Category from the Same Column in Excel

In Excel, you can use a combination of the XLOOKUP and DROP functions to...

How to Create a Searchable Table in Excel Using the FILTER Function

Finding specific records, or rows containing the required information, is straightforward in Excel using...

Time Sequences in Excel by Minute, Hour, or Second Increments

Creating time sequences, whether by hour, minute, or second increments in Excel, can be...

More like this

XMATCH Row by Row: Finding Values Across a Range in Google Sheets

Using the BYROW function with XMATCH in Google Sheets allows us to match values...

Limit Formula Expansion to a Specific Row in Google Sheets

In this tutorial, I’ll explain how to limit the expansion of an array formula...

3-D Referencing Structured Data Tables in Google Sheets

When you have several tables within a single sheet—not across multiple sheets in a...

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.