I am a supporter of
St. Joseph's hospice.
 If you find this site useful or if it helped you, consider a small donation to
St. Joseph's, please.

Information on
St. Joseph's

JustGiving - Sponsor me now!

 

Sumproduct

"Whenever you find yourself on the side of the majority, it is time to pause and reflect." [Mark Twain]

SUMPRODUCT is a very powerful function. You can conditionally count or sum large ranges of data. On the other hand, if you need to maintain statistics on ALL elements of a larger list, the manual maintenance effort and sometimes the runtime of a recalculation can be too high.

Advantages of using SUMPRODUCT:

SUMPRODUCT is more flexible than COUNTIF or SUMIF which support ONE condition only.

Example: If you want to count all cells in A1:A100 which show a positive numerical value, e.g. which are greater zero:
=COUNTIF(A1:A100,”>0”)
can be replaced by
=SUMPRODUCT(--(A1:A100>0))

And if you need to count how many numbers of A1:A100 are greater than zero AND where the string in column B is "LONDON", use
=SUMPRODUCT(--(A1:A100>0),--(B1:B100="LONDON"))
This cannot be done with COUNTIF.

Instead of using SUMIF if you can sum up conditionally with SUMPRODUCT.

Example: If you want to sum up all cells in B1:B100 where the corresponding cells in A1:A100 show a positive numerical value:
=SUMIF(A1:A100,”>0”,B1:B100)
can be replaced by
=SUMPRODUCT(--(A1:A100>0),B1:B100)

Please note that the double unary minus translates the Boolean values True and False into the numbers 1 and 0. You could also have used 0+ or 1* to do this but I think the double unary minus canonically indicates a condition and if that's missing you know that you look at the values which you need to sum up.

Now the fun starts because you can combine conditions which COUNTIF and SUMIF cannot deal with (since there are more than one of them).

Example:
[AND Condition] If you want to add all numbers in C1:C100 where A1:A100 are negative and where B1:B100 show a "YES":
=SUMPRODUCT(--(A1:A100<0),--(B1:B100="YES"),C1:C100)
A more complex example which you still might find not too complex: Calculate the rental cost for a holiday property - see this 12 KB Excel 2007 sample file but open and use at your own risk, please read my disclaimer.

[OR Condition] If you want to add all numbers in C1:C100 where A1:A100 are negative or where B1:B100 show a "YES":
=SUMPRODUCT(SIGN((A1:A100<0)+(B1:B100=”YES”)),C1:C100)
[Please notice that the SIGN function reduces possible overlapping OR criteria - which would sum up to more than 1 - to 1 exactly! I suggest to always wrap OR criteria checks into a SIGN() function. A simple sum is naive and outright dangerous because a less skilled person could amend non-overlapping XOR criteria to overlapping OR ones.]

More complex examples:

20100903_PB_01_Sumproduct

A 20 KB Excel 2007/2010 © sample file you can find here, open and use at your own risk, please read my disclaimer.

Please note that I normally prefer SUMPRODUCT(--(),--()) to SUMPRODUCT(()*()) because this separates the conditions quite clearly ...

...but for multi-column ranges you can only use the *-approach.

Limitations of SUMPRODUCT:

SUMPRODUCT offers you a nice and complex functionality of an array function. You can count and sum up attributes/properties of cell ranges and put the result into a cell. If you need to do this for all appearing characteristics you face some difficulties, though. You need to prepare the set of unique characteristics for use with SUMPRODUCT first. If you do this manually you end up with manual maintenance if your input changes. If you want to do this with worksheet functions you increase the complexity of your worksheet.

Example: If you need to count all different strings in a column and to list them together with their number of occurrences, for example, you have two reasonable options for a worksheet function approach with SUMPRODUCT:

a) To manually write down all different entries and to count these with COUNTIF or SUMPRODUCT. You would have to maintain this list manually then.
b) A feed of unique values with worksheet functions increases the complexity of the worksheet (please see here).

For cases like this one I suggest to use my user-defined-function (UDF) Pfreq instead of SUMPRODUCT or COUNTIF, if you need to count. If you want to sum up specified cells for all occurring corresponding cells/combinations, use sbSfreq, please.

A comparison of three different approaches to a former Excel newsgroup question is shown here.

Sulprobil   Get it done   Contact   Disclaimer   Download