2

Data set:

A|B

1|a     
2|b     
3|c

Formula:

=SUMPRODUCT((B1:B3={"a";"b"}))

The sumproduct return NA there. I don't get why. I want it to return 2.

If I add c to the condition it correctly returns 3.

What am I my missing ?

4

2 回答 2

3

You are comparing an array of size 1x3 to an array of size 1x2 which is not allowed. (If you compare two vertical arrays, they must be the same size. This is why the problem is fixed when you add "c" to the formula.)

You need to compare a 1x3 array to a 2x1 array.

This expression:

B1:B3={"a";"b"}

Returns:

{TRUE;TRUE;#N/A}

SUMPRODUCT cannot handle logical (TRUE/FALSE) values so you must add 0, multiply by 1, or perform double negative operator to change to an array of numerical values.

So... this:

--(B1:B3={"a";"b"})

Returns:

{1;1;#N/A}

Performing SUMPRODUCT on this will still return #N/A since the array contains #N/A.

But if you do this: (Note the comma instead of semicolon)

B1:B3={"a","b"}

Now you get:

{TRUE,FALSE;FALSE,TRUE;FALSE,FALSE}

Note this returns a 2x3 array.

Perform double negative operator and you get this:

{1,0;0,1;0,0}

Now performing SUMPRODUCT will return 2, as you expect.

The final formula you need is:

= SUMPRODUCT(--(B1:B3 = {"a","b"}))

enter image description here

于 2019-07-18T11:46:40.780 回答
2

You'r comparing a horizontal range vs. a vertical range. However your delimiter is wrong in case of English version. Also, with the right , as delimiter, your comparison will return a range of TRUE and FALSE values.

Wrap your comparison in a double negative or multiply it by 1. e.g.:

=SUMPRODUCT(--(B1:B3={"a","b"}))

Or:

=SUMPRODUCT((B1:B3={"a","b"})*1)

This will change TRUE into 1 and FALSE into 0, values your SUMPRODUCT can actually sum.

A more comprehensive explaination on why adding a "c" would work given by @Jerry on a failry similar question I once had can be found here

于 2019-07-18T11:31:21.273 回答