Filter Data Between Two Headings in Google Sheets

You may have data in several rows within a column, separated by headings. How do you extract the values between each heading into separate columns? Is that possible in Google Sheets?

Yes, you can filter or extract values between two headings using a range reference formed by two XLOOKUP functions in Google Sheets. If you have multiple headings, you can use the MAP function to iterate over an array of headings and separate the values into columns.

Separate Values Between Headings into Columns in Google Sheets

I’ll be sharing this relatively unknown technique with my readers.

To extract the values between two headings, you can use the following formula:

=XLOOKUP(heading1, range, range):XLOOKUP(heading2, range, range)

Where:

  • heading1: The top heading.
  • heading2: The bottom heading.
  • range: The column ranges from which to extract data.

This result will include the two headers. You may need to use the FILTER function to remove those headers and any blank cells.

Example

Assume you have the following sample data in column A on Sheet1 of your Google Sheets file:

A
USA
English
Spanish
Canada
English
French
India
Hindi
English
Bengali
Tamil
Switzerland
German
French
Italian

In Sheet2 of the file, enter “USA” in cell A1 and “Canada” in cell B1.

How do you filter the data between the country names “USA” and “Canada” to get the key languages spoken in the USA?

Step 1: Extracting Values Between Two Headings, Including Headings

To extract the data between the headers ‘USA’ and ‘Canada’ in column A on Sheet1, enter the following formula in cell A2 on Sheet2:

=XLOOKUP(A1, Sheet1!$A$1:$A, Sheet1!$A$1:$A):XLOOKUP(B1, Sheet1!$A$1:$A, Sheet1!$A$1:$A)
Filter Data Between Two Headings in Google Sheets

Note that this formula will include both headers. How can we remove them?

Step 2: Removing the Headings

Logic: Use the LET function to assign a name to the range formed by the XLOOKUP formulas and filter out the headers.

Enter the following formula in cell A2 on Sheet2:

=LET(data, XLOOKUP(A1, Sheet1!$A$1:$A, Sheet1!$A$1:$A):XLOOKUP(B1, Sheet1!$A$1:$A, Sheet1!$A$1:$A), FILTER(data, data<>A1, data<>B1, data<>""))

The LET function assigns the name ‘data’ to the range reference created by the XLOOKUPs, and the FILTER function removes the headers and any blank rows.

Move Values Between Headings in a Column to Separate Columns

In the first row of Sheet2, enter the headings. Based on our sample data, enter the country names in cells A1:D1.

ABCD
USACanadaIndiaSwitzerland

Please enter the above formula in cell A2 and drag it across to D2.

The formula will return #N/A in D2 because it’s trying to get data between the header in D1 and the header in E1, which is blank.

ABCD
USACanadaIndiaSwitzerland
EnglishEnglishHindi#N/A
SpanishFrenchEnglish
Bengali
Tamil

In cell E1, enter any character, for example, a hyphen (-).

Go to Sheet1 and enter a hyphen (-) in the first empty cell below the data in column A, such as A16. Now, check column D on Sheet2.

If you prefer using an array formula to move values between headings in a column to separate columns, clear all values in A2:D and enter the following formula in cell A2:

=MAP(A1:D1, B1:E1, LAMBDA(x, y, LET(data, XLOOKUP(x, Sheet1!A1:A, Sheet1!A1:A):XLOOKUP(y, Sheet1!A1:A, Sheet1!A1:A), FILTER(data, data<>x, data<>y, data<>""))))

Explanation

The MAP function uses two arrays:

  • Array 1: A1:D1 (Sheet2), which represents the range from the first header to the last header.
  • Array 2: B1:E1 (Sheet2), which represents the range from the second header to the last header plus one additional cell.

The LAMBDA function assigns names x and y to the values in these arrays. These names are used within the XLOOKUP functions to extract data between the headers specified by x and y.

The MAP function iterates through each pair of elements from these arrays (x and y) and returns the data that falls between the corresponding headers.

Resources

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Expert Prashanth KV is a Diamond Product Expert in Google Sheets, officially recognized by Google for his contributions to the Docs Editors Help Community and featured in the Google Product Experts Directory. Explore his blog to learn advanced formulas, automation tips, and problem-solving techniques to elevate your spreadsheet skills.

Calculate Trip Days by Month (Start, End, and Full Days) in Google Sheets

If you're managing business travel in Google Sheets, you may need to calculate how...

Mode of Comma-Separated Numbers in Excel (Dynamic Array)

There is no dedicated function in Excel to directly find the mode of comma-separated...

How to Find Mode of Comma-Separated Numbers in Google Sheets

Working with comma-separated numbers inside a single cell is a common scenario in Google...

How to Count Merged Cells in Google Sheets (and Get Their Size)

Sometimes, you may have blocks of merged cells in a column or row. But...

More like this

Calculate Trip Days by Month (Start, End, and Full Days) in Google Sheets

If you're managing business travel in Google Sheets, you may need to calculate how...

How to Find Mode of Comma-Separated Numbers in Google Sheets

Working with comma-separated numbers inside a single cell is a common scenario in Google...

How to Count Merged Cells in Google Sheets (and Get Their Size)

Sometimes, you may have blocks of merged cells in a column or row. But...

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.