Count Rows and Entries Between Two Values in Google Sheets

Published on

You can use the following formula to count the number of rows between two values in Google Sheets:

=ROWS(XLOOKUP(value1, range, range):XLOOKUP(value2, range, range)) - 2

When using this formula, replace value1 with the first value to look up in the column and value2 with the second value to look up.

Then, replace range with the column range reference.

Count Specific Entries Between Two Values in Google Sheets

If you want to count specific values between two values, use this formula:

=COUNTIF(OFFSET(XLOOKUP(value1, lookup_range, count_range), 1, 0):OFFSET(XLOOKUP(value2, lookup_range, count_range), -1, 0), criterion)

Where:

  • value1 – The first value to look up
  • value2 – The second value to look up
  • lookup_range – The range to search for the values
  • count_range – The corresponding range to count from
  • criterion – The specific value to count

Example: Counting Rows Between Two Values in Google Sheets

In the following example, I want to count the number of rows between the strings “Q1” and “Q2” in column B (range B2:B1000).

Sample data for counting rows between two values in Google Sheets

Formula:

=ROWS(XLOOKUP("Q1", B2:B1000, B2:B1000):XLOOKUP("Q2", B2:B1000, B2:B1000)) - 2

The formula would return 6, which is the number of rows between the specified two values.

How the Formula Works

  • XLOOKUP("Q1", B2:B1000, B2:B1000) – Matches the first value and returns its reference.
  • XLOOKUP("Q2", B2:B1000, B2:B1000) – Matches the second value and returns its reference.
  • XLOOKUP("Q1", B2:B1000, B2:B1000):XLOOKUP("Q2", B2:B1000, B2:B1000) – Creates a range from the first to the second value.
  • ROWS(...) returns the number of rows in this range, including both values. Since we only need rows between these values, we subtract 2 from the result.

That’s the easiest way to count the number of rows between two values in Google Sheets.

Example: Counting Specific Entries Between Two Values in Google Sheets

In the above example, how do we count how many times the name “Annie” appears between the two values in another column?

Formula:

=COUNTIF(OFFSET(XLOOKUP("Q1", B2:B, C2:C), 1, 0):OFFSET(XLOOKUP("Q2", B2:B, C2:C), -1, 0), "Annie")

This formula will return 3, as there are three cells containing “Annie” between “Q1” and “Q2” in column C.

How the Formula Works

  • OFFSET(XLOOKUP("Q1", B2:B, C2:C), 1, 0)XLOOKUP finds “Q1,” and OFFSET moves one row down.
  • OFFSET(XLOOKUP("Q2", B2:B, C2:C), -1, 0)XLOOKUP finds “Q2,” and OFFSET moves one row up.
  • OFFSET(XLOOKUP("Q1", B2:B, C2:C), 1, 0):OFFSET(XLOOKUP("Q2", B2:B, C2:C), -1, 0) – Defines the range between “Q1” and “Q2,” excluding them.
  • COUNTIF(...) counts the occurrences of “Annie” within this range.

That’s how to count a specific entry between two values in Google Sheets.

Where Does This Apply in Real Life?

These formulas are useful for identifying values between two reference points in a column. They work with dates, numbers, and text, but they are especially useful for text-based filtering. This allows you to find all values between two labels and manipulate the data—count, average, sum, etc.—with ease.

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.

Highlight the Smallest N Values in a Column in Google Sheets

Want to visually spotlight the lowest values in your data? In this tutorial, you'll...

Sort Each Row Individually in Excel Using a LAMBDA Formula

Sorting rows in Excel typically refers to rearranging entire datasets based on values in...

Sort by Field Labels Using the SORT and XMATCH Combo in Excel

Want to sort your Excel data by column names instead of column positions? Learn...

How to Sort Pie Slices in Google Sheets

To sort pie slices in a pie chart, you need to sort the data...

More like this

Highlight the Smallest N Values in a Column in Google Sheets

Want to visually spotlight the lowest values in your data? In this tutorial, you'll...

How to Sort Pie Slices in Google Sheets

To sort pie slices in a pie chart, you need to sort the data...

Filter Items Unique to Groups in Google Sheets

In this tutorial, we'll learn how to filter items unique to groups in Google...

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.