Sub topic 9
Counting non-empty and empty cells with COUNTA and COUNTBLANK
This chapter assumes you can enter formulas in Google Sheets, read cell addresses such as B2, and write a range such as B2:B6. It also assumes you know that COUNT returns the number of numeric cells in a range. Here you will learn the two functions that count by whether a cell holds anything at all. You will also learn the edge cases that make their results surprising.
Three questions, three functions
COUNT asks whether a cell holds a number. COUNTA asks whether a cell holds anything. COUNTBLANK asks whether a cell holds nothing.
| Function | Counts | Typical use |
|---|---|---|
| `COUNT` | Cells that hold numbers (dates count too) | How many measurements do I have? |
| `COUNTA` | Cells that hold any value: numbers, text, errors, TRUE/FALSE | How many rows have an entry? |
| `COUNTBLANK` | Cells that are empty | How many entries are missing? |
The "A" in COUNTA can be read as "all values". Both functions use the same syntax as the functions you have already seen:
=COUNTA(A2:A6)
=COUNTBLANK(B2:B6)Each formula returns a whole number. Neither one cares what the values are, only whether a cell has content.
A worked example
Suppose a small stock sheet is filled in like this. Column B is the quantity on hand.
| Row | A (Item) | B (Quantity) | What is really in B |
|---|---|---|---|
| 2 | Apples | 120 | the number 120 |
| 3 | Pears | nothing | |
| 4 | Plums | n/a | the text n/a |
| 5 | Cherries | 0 | the number 0 |
| 6 | Grapes | 85 | the text 85, typed with a leading apostrophe or imported as text |
Now apply all three functions to B2:B6:
=COUNT(B2:B6)
=COUNTA(B2:B6)
=COUNTBLANK(B2:B6)The results are 2, 4, and 1.
- COUNT returns 2. Only the real numbers 120 and 0 qualify. The text
n/aand the text85are ignored, even though the second one looks like a number. - COUNTA returns 4. Every cell except B3 has content. The zero counts, and so does the text
85. - COUNTBLANK returns 1. Only B3 is empty. A zero is a value, so B5 is not blank.
Here COUNTA and COUNTBLANK add up to 5, which is the number of cells in the range. That is the normal case for a sheet with no formulas in the range. The next section shows why the sum can break.
What counts as "empty"
Intermediate users usually get caught by what looks empty but is not, and the reverse.
Cells that look empty but count as content
- A cell holding a single space. COUNTA counts it, and COUNTBLANK does not. This often happens with pasted data.
- A cell holding an empty-string formula. A formula such as
=IF(C3="","","x")that returns""displays nothing. Here COUNTA counts it, and COUNTBLANK also counts it. See the next section. - A cell with an apostrophe only. Treat this as unpredictable. Clean the data instead of relying on it.
Cells that look filled but count as blank
- None of the usual cases. A zero, a date, FALSE, and an error all count as content. COUNTBLANK treats only truly empty cells and
""results as blank.
You can test a suspicious cell with =LEN(B3). A result of 0 means the cell is empty or holds "". A result of 1 or more means something is in it, such as a space.
The double-counting case
Google Sheets counts a formula result of "" in both functions. Change B3 in the example so that it contains ="". Then:
=COUNTA(B2:B6)
=COUNTBLANK(B2:B6)COUNTA now returns 5, because B3 holds a formula. COUNTBLANK still returns 1, because B3 displays as empty. The two results add up to 6 in a range of only 5 cells.
So you cannot use COUNTA + COUNTBLANK as a check on the total number of cells when formulas are involved. To get the total number of cells in a range, use the shape of the range instead:
=ROWS(B2:B6)*COLUMNS(B2:B6)For a single column, =ROWS(B2:B6) is enough.
How each function treats other value types
This table extends the behavior from the earlier chapter on what functions ignore.
| Cell content | COUNT | COUNTA | COUNTBLANK |
|---|---|---|---|
| Number, such as 120 | counted | counted | not counted |
| Zero | counted | counted | not counted |
| Date | counted | counted | not counted |
| Text, such as `n/a` | not counted | counted | not counted |
| Number stored as text | not counted | counted | not counted |
| Error, such as `#DIV/0!` | not counted | counted | not counted |
| Truly empty cell | not counted | not counted | counted |
| Formula returning `""` | not counted | counted | counted |
Two rows deserve attention. An error cell is counted by COUNTA, so a range full of errors still looks "filled". A number stored as text is counted by COUNTA, so comparing COUNT and COUNTA is a quick way to find text-numbers.
Using the pair to find problems
Comparing the three counts on the same range tells you what is wrong with your data.
=COUNTA(B2:B6)-COUNT(B2:B6)This returns the number of cells that hold something other than a number: text, errors, and logical values. If you expect a pure numeric column, any result above 0 means you should look for text entries such as n/a, or for numbers stored as text. In the example it returns 2.
To check completeness, compare blanks against the expected number of rows:
=IF(COUNTBLANK(B2:B6)>0, "Missing data", "Complete")To get a fill rate, divide the count of entries by the number of rows, then format the cell as a percentage:
=COUNTA(B2:B6)/ROWS(B2:B6)In the example this gives 4/5, which is 80%. Because ROWS is never zero for a real range, this formula does not produce #DIV/0!. If your data contains "" formulas, this fill rate overstates completeness, because COUNTA counts them. In that case use =1-COUNTBLANK(B2:B6)/ROWS(B2:B6) instead.
Multiple arguments and ranges
COUNTA accepts several arguments, as SUM does. They can be ranges, single cells, or literal values:
=COUNTA(A2:A6, C2:C6)
=COUNTA(A2, A4, A6)Every literal value you type counts as one entry, so =COUNTA(A2:A6, "x") adds 1 to the range count.
COUNTBLANK accepts only one range argument. To count blanks in two separate ranges, add two calls:
=COUNTBLANK(B2:B6) + COUNTBLANK(D2:D6)A multi-column range works too. =COUNTBLANK(B2:D6) counts every empty cell in the 15-cell rectangle.
Whole-column references
=COUNTA(A:A) counts every non-empty cell in column A, including the header. If your header is in A1 and you want the number of data rows, either subtract 1 or start at row 2:
=COUNTA(A2:A)Be careful with COUNTBLANK(A:A). It counts the empty cells in the whole column up to the last row of the sheet's grid, which might be 1,000 rows. The result changes when you add or delete rows in the sheet, and it is almost never the number you want. Prefer a bounded range for blanks, such as A2:A100. If rows are added over time, count from the data instead:
=COUNTA(A2:A) - COUNTA(B2:B)This returns how many items in column A have no matching entry in column B, assuming both columns have no gaps and no header offset.
Choosing the right function
Common mistakes
- Using COUNTA to count numbers. Text such as
n/aand numbers stored as text inflate the result. Use COUNT when you need numbers only. - Assuming a zero is blank. COUNTBLANK does not count zeros. If you need to count zeros, use
=COUNTIF(B2:B6, 0). - Trusting
COUNTA + COUNTBLANKas the total. It double-counts""formula results. - Counting headers.
COUNTA(A:A)includes the header cell. Start the range below the header. - Hidden spaces. A cell that holds only a space counts as an entry. Clean the data with
TRIM, or test withLEN. - Placing the formula inside its own range.
=COUNTA(A:A)typed into column A creates a circular reference. Put the result in another column.
Exercises
Exercise
In the range B2:B6, B2 holds 10, B3 holds the text "pending", B4 is empty, B5 holds 0, and B6 holds a formula that returns "". What do COUNT, COUNTA, and COUNTBLANK return for B2:B6?
Show answer
COUNT returns 2, because only 10 and 0 are numbers. COUNTA returns 4, because B2, B3, B5, and B6 hold content, and the "" formula in B6 counts as content. COUNTBLANK returns 2, because the empty B4 and the "" result in B6 both count as blank. The counts add up to 6 for a 5-cell range because B6 is counted twice.
Exercise
Write one formula that returns the number of cells in B2:B100 that hold something other than a number, and say what kinds of cells it includes.
Show answer
=COUNTA(B2:B100)-COUNT(B2:B100). It counts text, numbers stored as text, errors, and TRUE/FALSE values. It also counts any cell holding only a space or a formula that returns "". It does not count truly empty cells or real numbers.
Exercise
A sheet has a header in A1 and an unknown number of items below it in column A. Write a formula that returns the number of items without counting the header, and explain why COUNTBLANK(A:A) is a poor way to find how many rows are unused.
Show answer
=COUNTA(A2:A) counts the non-empty cells below the header. COUNTBLANK(A:A) counts empty cells down to the last row of the sheet's grid, so the result depends on how many rows the sheet has and changes when rows are added or deleted. It also counts the empty cells in the used area, not only the unused rows below it.