Function Problems

How to Count Nonblank Cells Before Each Blank Row in Excel

Khadija KhanKhadija Khan Nov 11, 2026 868 views

Question details

The user needs a formula to calculate the number of non-empty cells within distinct groups separated by blank rows, displaying the count only on the last non-empty row of each group.

How to Count Nonblank Cells Before Each Blank Row in Excel
Product
Excel
Device & OS
not provided
Scenario
Counting grouped data blocks in a spreadsheet where groups are separated by empty rows, without manually doing sub-totals.
Observed behavior
A dynamic result needs to appear specifically on the row right before a blank space, representing the total non-empty cells in the preceding data block.
Before you start

Ensure your dataset is consistently entered in a single column (e.g., Column A) and that there is at least one blank row separating each distinct group of data. Leave the first row for headers, as the formula relies on a fixed starting reference point.

Solution 1Recommended

Use a Combination Formula with IF, AND, COUNTA, and SUM

This dynamic formula identifies the end of a data group and calculates its size by subtracting previous group totals from the running total of nonblank cells.

By combining logical evaluation with counting functions, Excel can automatically detect where a data block ends. The IF and AND functions check if the current cell has data while the cell below it is empty. If true, COUNTA tallies all non-empty cells from the top, and SUM subtracts the counts of any previously calculated blocks to give you the exact count for the current block.

1
Select the target cell

Click on cell B2 (or the cell adjacent to the first data point in your column) where you want the first potential count to appear.

2
Enter the counting formula

Type the following formula into the formula bar: =IF(AND(A2<>"",A3=""),COUNTA(A$1:A2)-SUM(B$1:B1),"")

3
Apply the formula

Press Enter on your keyboard to apply the formula to the cell.

4
Fill the formula down the column

Click and drag the fill handle (the small square at the bottom-right corner of cell B2) down the column to copy the formula alongside all your data groups.

Use a Combination Formula with IF, AND, COUNTA, and SUM
How the Formula References Work: The use of the absolute reference (e.g., A$1 and B$1) anchors the starting point of the count and sum ranges. As you drag the formula down, the second part of the range (A2, B1) shifts dynamically to capture the running totals up to that specific row.
Powerful Spreadsheet Software

Easily Count and Manage Grouped Data in WPS Spreadsheet

WPS Spreadsheet offers full support for advanced logical and counting formulas like IF, COUNTA, and SUM. It allows you to organize grouped data, automate complex counts, and manage your datasets with ease, providing a seamless experience for data analysts and everyday users alike.

  1. 1. Open your data file: Launch WPS Spreadsheet and open the workbook containing your grouped data.
  2. 2. Select the formula cell: Click on the cell in the adjacent column (e.g., B2) next to your first data entry.
  3. 3. Input the logic formula: Type the combined IF, AND, COUNTA, and SUM formula into the formula bar.
  4. 4. Drag to apply: Double-click or drag the fill handle down to instantly calculate counts for all groups in your worksheet.
Fully compatible with Microsoft Excel formulas (.xlsx)Supports advanced logical functions for dynamic data countingLightweight application with a familiar, easy-to-navigate tabbed interfaceFree built-in templates for data tracking and reporting
microsoft office alternative - wps office

Frequently Asked Questions

Can I use COUNTIF instead of COUNTA for this scenario?

COUNTIF requires a specific condition to evaluate against a range, whereas COUNTA simply counts any cell that is not empty. Because the logic here depends on calculating a running total of all non-empty cells regardless of their specific text or numeric content, COUNTA is the better and more efficient choice.

What happens if there are multiple consecutive blank rows separating my data?

The formula =IF(AND(A2<>"",A3="")...) specifically triggers only when the current cell is not empty but the cell immediately below it is empty. Multiple consecutive blank rows won't break the count, as the formula will simply output blanks for all those empty rows and wait for the next non-empty sequence.

Will this formula work if my data groups contain text instead of numbers?

Yes, the COUNTA function is designed to count cells containing any type of data, including text strings, numbers, booleans, or symbols. This makes the formula perfect for counting grouped text entries separated by blanks.

Why does the formula use SUM(B$1:B1)?

The SUM(B$1:B1) segment calculates the total sum of all the previous group counts that have already been displayed in column B. By subtracting this from the total nonblank cells counted so far, the formula isolates the count for just the current group.