How to Count Nonblank Cells Before Each Blank Row in Excel
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.

- 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.
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.
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.
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.
Type the following formula into the formula bar: =IF(AND(A2<>"",A3=""),COUNTA(A$1:A2)-SUM(B$1:B1),"")
Press Enter on your keyboard to apply the formula to the cell.
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.

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. Open your data file: Launch WPS Spreadsheet and open the workbook containing your grouped data.
- 2. Select the formula cell: Click on the cell in the adjacent column (e.g., B2) next to your first data entry.
- 3. Input the logic formula: Type the combined IF, AND, COUNTA, and SUM formula into the formula bar.
- 4. Drag to apply: Double-click or drag the fill handle down to instantly calculate counts for all groups in your worksheet.

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.




