138

In a Google Spreadsheet: How can I count the rows of a given area that have a value? All hints about this I found up to now lead to formulas that do count the rows which have a not empty content (including formula), but a cell with

=IF(1=2;"";"")  // Shows an empty cell

is counted as well.

What is the solution to this simple task?

Rubén
  • 34,714
  • 9
  • 70
  • 166
Robbit
  • 1,537
  • 2
  • 9
  • 10
  • 6
    Wouldn't CountA work for you? Also see this SO thread: http://stackoverflow.com/questions/12519073/google-docs-count-cells-that-contain-any-text – Ozair Kafray Oct 05 '15 at 04:00
  • Possible Duplicate of [google docs count cells that contain any text](http://stackoverflow.com/q/12519073/1366033) – KyleMit Apr 10 '17 at 14:21
  • Possible duplicate of [Count cells that contain any text](https://stackoverflow.com/questions/12519073/count-cells-that-contain-any-text) – Rubén Nov 20 '18 at 04:40

13 Answers13

214

I just used =COUNTIF(Range, "<>") and it counted non-empty cells for me.

gmuraleekrishna
  • 3,375
  • 1
  • 27
  • 45
Mike
  • 2,181
  • 1
  • 9
  • 2
134
=counta(range) 
  • counta: "Returns a count of the number of values in a dataset"

    Note: CountA considers "" to be a value. Only cells that are blank (press delete in a cell to blank it) are not counted.

    Google support: https://support.google.com/docs/answer/3093991

  • countblank: "Returns the number of empty cells in a given range"

    Note: CountBlank considers both blank cells (press delete to blank a cell) and cells that have a formula that returns "" to be empty cells.

    Google Support: https://support.google.com/docs/answer/3093403

If you have a range that includes formulae that result in "", then you can modify your formula from

=counta(range)

to:

=Counta(range) - Countblank(range)

EDIT: the function is countblank, not countblanks, the latter will give an error.

johntellsall
  • 14,394
  • 4
  • 46
  • 40
Delta_zulu
  • 1,580
  • 1
  • 10
  • 9
  • 6
    Unfortunately, this does not work for ranges that include both virgin blanks and calculated blanks. (It subtracts the non-counted virgin blanks from COUNTA's value.) – David Veszelovszki May 24 '16 at 17:57
  • 4
    Thanks for the sweet downvotes, the OP specifically mentioned that the reason he has blanks is because of formula like =IF(1=2;"";""), and I specifically mentioned: "If you have a range that includes formulae that result in ""..." – Delta_zulu May 26 '16 at 00:54
  • The question is asking how to count the rows in a range. You are counting the cells in a range? – AnnanFay Mar 03 '19 at 21:09
41

Here's what I believe is the best solution so far:

=CountIf(ArrayFormula(range<>""),TRUE)

Here's why in 3 easy steps

Step 1: Simple As Pie - Add Extra Column

The answer by eniacAvenger will yield the correct solution without worrying about edge cases as =A1<>"" seems to arrive at the correct truthy/falsy value based on how we intuitively think of blank cells, either virgin blanks or created blanks.

So imagine we have this data and we want the Count of non-blanks in B2:B6:

|   |      A      |   B   |    C    |
|---|-------------|-------|---------|
| 1 | Description | Value | B1<>""  |
| 2 | Text        | H     | TRUE    |
| 3 | Number      | 1     | TRUE    |
| 4 | IF -> ""    |       | FALSE   |
| 5 | IF -> Text  | h     | TRUE    |
| 6 | Blank       |       | FALSE   |

If we relied on Column C, we could get the count of values in B like this:

=COUNTIF(C2:C6,True)

Step 2: Use FormulaArray to dynamically create Extra Column

However, consideRatio's comment is a valid one - if you need an extra column, you can often accomplish the same goal with an ArrayFormula which can create a column in memory without eating up sheet space.

So if we want to create C dynamically, we can use an array formula like this:

=ArrayFormula(B2:B6<>"")

If we simply put it in C2, it would create the vertical array with a single stroke of the pen:

|   |      A      |   B   |    C                     |
|---|-------------|-------|--------------------------|
| 1 | Description | Value | =ArrayFormula(B2:B6<>"") |
| 2 | Text        | H     | TRUE                     |
| 3 | Number      | 1     | TRUE                     |
| 4 | IF -> ""    |       | FALSE                    |
| 5 | IF -> Text  | h     | TRUE                     |
| 6 | Blank       |       | FALSE                    |

Step 3: Count Values in Dynamic Column

But with that solved, we no longer need the column to merely display the values.

ArrayFormula will resolve to the following range: {True,True,False,True,False}.
CountIf just takes in any range and in this case can count the number of True values.

So we can wrap CountIf around the values produced by ArrayFormula like this:

=CountIf(ArrayFormula(B2:B6<>""),TRUE)

Further Reading

The other solutions in this thread are either overly complex, or fail in particular edge cases that I've enumerated in this test sheet:

Google Spreadsheet - CountA Test - Demo

For why CountA works the wonky way it does, see my answer here

KyleMit
  • 30,350
  • 66
  • 462
  • 664
  • 4
    This answer is the only complete solution: It successfully counts only cells with text (including space), numbers, and TRUE/FALSE values whilst excluding generated blanks, truly empty cells, and errors (#N/A, #REF!, #VALUE, #NAME?) . It will also return 0 where there are no values – spacepickle Apr 16 '17 at 11:10
  • 2
    I got a little confused reading this, and accidentally discovered that this formula works too... =CountIf(ArrayFormula(B2:B6<>""),TRUE) ... Please, don't get me wrong, this answer is the best one, and should be upvoted more. – twindham Aug 25 '17 at 19:27
  • 2
    @twindham, agreed, that in terms of building the blocks of this answer that it's probably easier to keep each logical component intact and reorder `ArrayFormula(...)` to return the array of values inside of `CountIf` as you suggested. I've updated the answer and the Spreadsheet demo. They'll both do the same thing, but cleanliness is next to goodliness. – KyleMit Aug 25 '17 at 20:01
  • The question is asking how to count the rows in a range. You are counting the non-blank cells in a single column? Can your approach work over multiple columns? – AnnanFay Mar 03 '19 at 21:14
19

For me, none of the answers worked for ranges that include both virgin cells and cells that are empty based on a formula (e.g. =IF(1=2;"";""))

What solved it for me is this:

=COUNTA(FILTER(range, range <> ""))

David Veszelovszki
  • 2,574
  • 1
  • 24
  • 23
  • Great solution in case if a columnb contains formuals which returns "" value! Thanks a lot – user1561325 Aug 22 '16 at 12:30
  • Unfortunately, it looks like if `Filter()` doesn't find any elements, it will return `#N/A` which `COUNTA()` treats as an element and so will always return 1, even if the count should be zero. [Example in Google Sheets](https://docs.google.com/spreadsheets/d/1wGzbVbM4y6iPu0xbgLHKC-Xkaj94ArGHqI6SOlv6yrQ/edit?usp=sharing) – KyleMit Apr 12 '17 at 00:22
  • 8
    @KyleMit You can get rid of the zero count issue by adding a constant value to the range and subtracting 1 from the count with `=COUNTA(filter({1;range},{1;range}<>"")) - 1` – spacepickle Apr 16 '17 at 11:39
  • The question is asking how to count the rows in a range. You are counting the cells in a range. This code breaks if you try to apply it to a range of rows. – AnnanFay Mar 03 '19 at 21:17
11

It works for me:

=SUMPRODUCT(NOT(ISBLANK(F2:F)))

Count of all non-empty cells from F2 to the end of the column

nomnom
  • 936
  • 10
  • 21
9

Solved using a solution i found googling by Yogi Anand: https://productforums.google.com/d/msg/docs/3qsR2m-1Xx8/sSU6Z6NYLOcJ

The example below counts the number of non-empty rows in the range A3:C, remember to update both ranges in the formula with your range of interest.

=ArrayFormula(SUM(SIGN(MMULT(LEN(A3:C), TRANSPOSE(SIGN(COLUMN(A3:C)))))))

Also make sure to avoid circular dependencies, it will happen if you for example count the number of non-empty rows in A:C and place this formula in the A or C column.

consideRatio
  • 1,091
  • 13
  • 19
4

Given the range A:A, Id suggest:

=COUNTA(A:A)-(COUNTIF(A:A,"*")-COUNTIF(A:A,"?*"))

The problem is COUNTA over-counts by exactly the number of cells with zero length strings "".

The solution is to find a count of exactly these cells. This can be found by looking for all text cells and subtracting all text cells with at least one character

  • COUNTA(A:A): cells with value, including "" but excluding truly empty cells
  • COUNTIF(A:A,"*"): cells recognized as text, including "" but excluding truly blank cells
  • COUNTIF(A:A,"?*"): cells recognized as text with at least one character

This means that the value COUNTIF(A:A,"*")-COUNTIF(A:A,"?*") should be the number of text cells minus the number of text cells that have at least one character i.e. the count of cells containing exactly ""

spacepickle
  • 2,678
  • 16
  • 21
  • Using @KyleMit demo sheet, i found this formula still over counts because it will include any cells with errors – spacepickle Apr 16 '17 at 10:56
  • There's a difference between a cell that contains a zero length string and a cell that contains nothing at all? This is what makes people distrust computers. . . Do you see why we can't have nice things? – Cheeso Apr 28 '17 at 23:40
  • The question is asking how to count the rows in a range. You are counting the cells in a range? – AnnanFay Mar 03 '19 at 21:19
4

A simpler solution that works for me:

=COUNTIFS(A:A;"<>"&"")

It counts both numbers, strings, dates, etc that are not empty

user300905
  • 57
  • 1
  • 2
4

As far as I can see, most of the solutions here count the number of non empty cells, and not the number of rows with non empty cell inside.

One possible solution for the range B3:E29 is for example

=SUM(ArrayFormula(IF(B3:B29&C3:C29&D3:D29&E3:E29="";0;1)))

Here ArrayFormula(IF(B3:B29&C3:C29&D3:D29&E3:E29="";0;1)) returns a column of 0 (if the row is empty) and 1 (else).

Another one is given in consideRatio's answer.

Kpym
  • 3,743
  • 1
  • 21
  • 18
2

You can define a custom function using Apps Script (Tools > Script editor) called for example numNonEmptyRows :

function numNonEmptyRows(range) {
  Logger.log("inside");
  Logger.log(range);
  if (range && range.constructor === Array) {
    return range.map(function(a){return a.join('')}).filter(Boolean).length
  }
  else {
    return range ? 1 : 0;
  }
}

And then use it in a cell like this =numNonEmptyRows(A23:C25) to count the number of non empty rows in the range A23:C25;

Kpym
  • 3,743
  • 1
  • 21
  • 18
2

In Google Sheets, to count the number of rows which contain at least one non-empty cell within a two-dimensional range:

=ARRAYFORMULA(
  SUM(
    N(
      MMULT(
        N(A1:C5<>""),
        TRANSPOSE(COLUMN(A1:C5)^0)
      )
      >0
    )
  )
)

Where A1:C5 is the range you're checking for non-empty rows.

The formula comes from, and is explained in the following article from EXCELXOR - https://excelxor.com/2015/03/30/counting-rows-where-at-least-one-condition-is-met/

Will Rice
  • 21
  • 3
0

A very flexible way to do that kind of things is using ARRAYFORMULA.

As an example imagine you want to count non empty strings (text fields) you can use this code:

=ARRAYFORMULA(SUM(IF(Len(B3:B14)>0, 1, 0)))

What happens here is that "ArrayFormula" let you operate over a set of values. Using the SUM function you indicates "ArrayFormula" to sum any value of the set. The "If" clause is only used to check "empty" or "not empty", 1 for not empty and 0 otherwise. "Len" returns the length of the different text fields, there is where you define the set (range) you want to check. Finally "ArrayFormula" will sum 1 for each field inside the set(range) in which "len" returns more than 0.

If you want to check any other condition, just modify the first argument of the IF clause.

Pablo
  • 456
  • 2
  • 7
  • 18
-17

Make another column that determines if the referenced cell is blank using the function "CountBlank". Then use count on the values created in the new "CountBlank" column.

eniacAvenger
  • 875
  • 12
  • 17
  • 3
    I consider this as a clear statement that there is no other solution to this. Thank you very much. – Robbit Oct 10 '14 at 14:48
  • When you need to add another column, you can almost always solve it by using some extra formulas like containing some concatinate/split/join etc, ill get back to you if i solve it. – consideRatio Jan 09 '15 at 23:18
  • For example, the following line makes one single row out of a 2d range of cells, that can often solve a lot of problems: =split(ArrayFormula(concatenate(C3:O4&";")),";") ---- ill keep working on solving the issue – consideRatio Jan 09 '15 at 23:28
  • I posted an answer that solves the goal without relying on separate columns – consideRatio Jan 10 '15 at 00:56