In Google Sheets I want to count the number of cells in a range (C4:U4)
that are non-empty and non-blank. Counting non-empty is easy with COUNTIF
. The tricky issue seems to be that I want to treat cells with one or more blank as empty. (My users keep leaving blanks in cells which are not visible and I waste a lot of time cleaning them up.)
=COUNTIF(C4:U4,"<>")
treats a cell with one or more blanks as non-empty and counts it. I've also tried =COUNTA(C4:U4)
but that suffers from the same problem of counting cells with one or more blanks.
I found a solution in stackoverflow flagged as a solution by 95 people but it doesn't work for cells with blanks.
After much reading I have come up with a fancy formula:
=COUNTIF(FILTER(C4:U4,TRIM(C4:U4)>="-"),"<>")
The idea is that the TRIM
removes leading and trailing blanks before FILTER
tests the cell to be greater than or equal to a hyphen (the lowest order of printable characters I could find). The FILTER
function then returns an array to the COUNTIF
function which only contains non-empty and non-blank cells. COUNTIF
then tests against "<>"
This works (or at least "seems" to work) but I was wondering if I've missed something really obvious. Surely the problem of hidden blanks is very common and has been around since the dawn of excel and google sheets. there must be a simpler way.
(My first question so apologies for any breaches of forum rules.)