Results 1 to 8 of 8

Thread: SUMPRODUCT Step By Step - Part 2

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Apr 2005

    SUMPRODUCT Step By Step - Part 2

    Multiple Conditional Tests


    In the previous chapter we explored the basics of SUMPRODUCT, its basic form where it is used to multiply two or more arrays and sum the results, and then looked at how conditional tests could be used within SUMPRODUCT to count those conditional matches. Whilst this was interesting and (hopefully) informative, as this only achieving what can be done with COUNTIF it is not yet the full story.

    Testing Two Conditions

    Suppose that we want to test two conditions in a single formula. For example, with the following data

    1	11
    2	2
    3	8
    4	5
    5	53
    6	9
    and we want to count how many items that are greater than 4 but less than or equal to 20. As you can see, we have 4 that meet these criteria, one is less than 4, one is greater than 20.

    Logically, the two tests would be

    (A1:A6>4) AND (A1:A6<=20)

    which unfortunately does not work within SUMPRODUCT, that is if you try


    Excel will complain, and will seek to change this to


    which, whilst it returns a result of 0 that is wrong, it does give us a clue.

    Interestingly, if there were no numbers in that range that were less than equal to 4 and none greater than 20 it would return the correct result but for the wrong reasons (we will not go into the details of this yet).

    If we remove the AND, giving


    we get the correct result, 4.

    What is happening here is that the first conditional test of (A1:A6>4) returns an array of


    the second condition of (A1:A6<=20) returns an array of


    and the operator between these conditions looks suspiciously like the Excel multiply symbol, which of course it is. As we discussed earlier, multiply TRUE by TRUE gives 1, any other combination of Booleans gives 0, so (A1:A6>4)*(A1:A6<=20) is equivalent to


    which returns an array of {1;0;1;1;0;1}. You should be able to see that this is easily summed by SUMPRODUCT, giving a result of 4 as required.

    Better Than Countif?

    So, we can see that SUMPRODUCT can handle two conditions, but is this anything radically new? In this case, we could get the result by using COUNTIF. Not a single COUNTIF, but by counting how many items are greater than 4 and then counting how many items are greater than 20, subtracting one from the other, we get the same result. In other words


    Gives a result of 4 as there are 5 items greater than 5, one greater than 20, so subtracting one from the other gives 4.

    So SUMPRODUCT is no better than COUNTIF then? Not in this example perhaps, but what if we wanted 3 conditions, 4, 5 and so on, the COUNTIF gets somewhat cumbersome, SUMPRODUCT stays reasonably concise.

    More importantly, consider the following data

    	A	B
    1	X	4
    2	X	5
    3	Y	6
    4	Y	4
    5	X	1
    6	X	5
    where we want to count how many times column A shows an X where its corresponding value in column B is greater than 4. By sight, we can see there are two such instances, which is easily calculated with the following SUMPRODUCT formula


    Try doing that with COUNTIF!

    One little aside here, not that in COUNTIF to test for greater than 4, you have to enclose the test within quotes, >4, but in SUMPRODUCT it is a straight comparative test. A small point, but much cleaner looking in my view.

    More Than 2 Conditions

    This technique can easily be extended to more than 2 conditions as suggested above. Consider the above data extended like this

    	A	B	C
    1	X	4	M
    2	X	5	F
    3	Y	6	M
    4	Y	4	F
    5	X	1	F
    6	X	5	M
    Where we want to count how many times column A shows an X where its corresponding value in column B is greater than 4 and its corresponding value in column C is M. Three conditions which should produce a result of 1. This SUMPRODUCT formula


    gives the required result. It should not be too difficult to see that this can be extended to 4, 5 or more tests if required.

    Ranges To Test

    In SUMPRODUCT, the ranges being tested MUST be the same size in order to work correctly. They do not have to be mapped too the same rows or columns, but they must be the same size.

    This formula is valid


    although it must be understood that this is comparing when a value in A1 equals X and the value in B2 is greater than 4. However, this formula is not valid


    because the second range is smaller than the first.

    In addition, whilst SUMPRODUCT is not array entered, it is an array function in this conditional usage, which means that you cannot use whole columns in the ranges to be tested, you have to use an explicitly limited range. This is where COUNTIF and SUMIF are preferable.

    Note that this has changed in Excel 2007, which will be covered in more detail in a future part of these discussions.


    In this section, we have extended SUMPRODUCT to count items that match multiple conditions, where the conditions are applied to a single range and then to conditions applied to separate ranges.

    We have also shown how to extend the conditions tested beyond just two, and some of the rules to be adhered to when using SUMPRODUCT.

    Next Time

    We will extend into summing multiple conditions; using different operators; and OR conditions.
    Last edited by Bob Phillips; 04-19-2009 at 01:41 PM.
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts