Count From First Non-Blank Cell to Last Non-Blank Cell in a Row in Google Sheets

Published on

Let me shed some light on how to count columns in a row from the first non-blank cell to the last non-blank cell in Google Sheets. Yes! I am back with yet another useful and clever Google Sheets formula.

For example, I have one value in cell H2 and another in cell L2. It can be any values like special characters, strings, dates or numbers.

The cells C2, D2…G2 (C2:G2) are blank and also there is no value from the cell M2 onwards in that row.

I want a formula to return # 5 as the count (H2, I2, J2, K2, and L2) in cell B5 counting the row C2:N2 or more conveniently an infinite range C2:2.

Hope you could understand what I am trying to say. If not, please see the GIF screenshot below. The formula is in the range B2:B9.

Skipping Blank Cells at End and Beginning in Sheets

Let me share with you the clever formula that helps me count columns in a row skipping blank cells at the beginning and end of a range in Google Sheets.

You can test it with your own sample data by changing some of the cell references in the formula. I’ll explain that later.

In my above example, cell B2 contains the following formula that returns the count from the first non-blank cell H2 to the last non-blank cell L2.

Formula to Count Columns in a Row Skipping Blank Cells at the Beginning and End

Formula:

=ArrayFormula(IFNA((match(1,1/(C2:2>0),1)-match(1,1/(C2:2>0),0))+1))

This formula has been copied to B3:B9. Let me elaborate on how this formula returns the count of columns skipping blank cells at the beginning and end. Before that here is how to modify this formula for your range.

Formula Customization:

The above formula is to count from the first non-blank cell C2 to last no-blank cell in the row, not N2.

That means the formula looks the entire range C2:2 (not C2:N2) for the last non-blank cell.

Note: If you don’t understand the range C2:2 enter it as =C2:2 in the 4th row (in any row other than the 2nd row) of your Spreadsheet. It will highlight the said range.

To limit the range to a specific column you can adjust the range like C2:N2 in the formula.

It’s not an expanding formula. Want the count from the first non-blank cell to the last non-blank cell in other rows?

If so, you must copy-paste the formula to the required cells down (in column B).

I hope you will be able to adjust/customize the formula as per your Google Sheets table now.

How the Formula Returns the Count From First Non-Blank Cell to Last Non-Blank Cell?

This is the formula explanation/logic section. It involves two important points.

  1. A formula that counts the numbers of cells up to the last non-blank cell.
  2. Another formula that counts the number of cells up to the first non-blank cell.

I can relate these two points in a Generic Formula as below.

Count From First Non-Blank Cell to Last Non-Blank Cell = Point # 1 Formula – Point # 2 Formula + 1

Here are those two formulas.

Point # 1 Formula:

=ArrayFormula(match(1,1/(C2:2>0),1))

This formula already featured/explained here – Find the Last Non-Empty Column in a Row in Google Sheets.

Point # 2 Formula:

=ArrayFormula(match(1,1/(C2:2>0),0))

This is the same as the Point # 1 formula except for the Match function last argument (search_type).

It is 1, means sorted range, in Point # 1 formula. Here it is set to 0, which means the range is not sorted.

MATCH(search_key, range, [search_type])

In cell B2, I have combined these two formulas as per the Generic formula above.

When combining multiple formulas, as I have explained many times, in almost all cases, you can use one single ArrayFormula, instead of using multiple ones.

In the combination, additionally, I have used IFNA to skip N/A errors in totally blank rows.

Formula Logic:

I know a few of you may want to know the formula logic.

Formula to Count From First Non-Blank Cell to Last Non-Blank Cell

I hope the above image will well explain the same.

Additional Resources:

  1. How to Count Comma Separated Words in a Cell in Google Sheets.
  2. Count Unique Values in Visible Rows in Google Sheets.
  3. How to Count The Number of Cells in a Range in Google Sheets.
  4. Sum, Count, Cumulative Sum Comma Separated Values in Google Sheets.
  5. How to Count Orders Per Week in Google Sheets.
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 Upcoming Birthdays in Google Sheets

When highlighting upcoming birthdays in Google Sheets, one important aspect is ignoring the year...

Compare Two Tables for Differences in Excel

To compare two tables with similar data for differences, you can use the XLOOKUP...

Calculate Weighted Average in Pivot Table in Google Sheets

You can calculate a weighted average in a Pivot Table using a custom formula...

Summarize Data and Keep the Last Record in Google Sheets

In Google Sheets, we can summarize data (like sum, average, min, max, count) using...

More like this

Highlight Upcoming Birthdays in Google Sheets

When highlighting upcoming birthdays in Google Sheets, one important aspect is ignoring the year...

Calculate Weighted Average in Pivot Table in Google Sheets

You can calculate a weighted average in a Pivot Table using a custom formula...

Summarize Data and Keep the Last Record in Google Sheets

In Google Sheets, we can summarize data (like sum, average, min, max, count) using...

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.