Search code examples
stringexcelnullis-emptysumifs

Excel Sum if not Null String


I would like to sum all cells in column B that corresponds to cells in column A which are not empty. However, in Excel, the term "not empty" is a bit ambigious: if a cell contains a formula, but the result is a null string, i.e. =IF(1=0,1,"") , it is considered not empty, even though the result is essentially nothing.

However, I want to exclude such cells.

The obvious thing to try first is

=SUMIF(A:A,"<>",B:B)

But this does not work, because the operator <> only says a cell is empty if it is exactly that: empty. If there's a formula in it, such as =IF(1=0,1,""), it is considered "not empty", as explained above.

In my Google adventures I've also learned that "=" is the converse of "<>" in the formula above, but this does not help me, as far as I can tell.

What makes my problem even more challengeing is that I want to include cells in column B which correspond to both text and numeric entries in column A...

I can think of various ways to "work around", but I'm actually after a solution where I don't have to add another column to my data and where I can leave my data as is.

Thanks!


Solution

  • The behaviour of COUNTIF(S)/SUMIF(S) I agree is a touch frustrating with respect to null strings.

    You can try:

    =SUM(SUMIF(A:A,{"?*",">=0"},B:B))

    if the numbers in column A are strictly non-negative, or, if not:

    =SUM(SUMIF(A:A,{"?*",">=0","<0"},B:B))

    Alternatively you can switch to SUMPRODUCT, which does not suffer from such ambiguous handling of null strings:

    =SUMPRODUCT(0+(A1:A15<>""),B1:B15)

    though which has the drawback that, unlike with COUNTIF(S)/SUMIF(S), you cannot arbitrarily reference as many cells as you wish without detriment to performance (hence my choice of an upper row reference of 15): in fact, the use of entire column references within SUMPRODUCT is a disastrous idea.

    Regards