Sumif with multiple conditions [quick tip]

Posted on July 22nd, 2009 in Excel Howtos - 27 comments

Here is a little formula trick if you need to sum a range of cells based on multiple conditions.

Assuming you have the starfleet, captain and flight data like this (seriously, what are you doing with excel again?)

Sumif with multiple conditions - data

In order to findout how many flights James Kirk did on the Amarillo, you can write an array formula like this:
=SUM(($D$3:$D$24)*($B$3:$B$24="Amarillo")*($C$3:$C$24="James Kirk"))

Sumif with Multiple ConditionsRemember, the above is an array formula. So you need to type it and press CTRL+SHIFT+Enter.

Update: Or you can also use SUMPRODUCT (replace SUM in the above formula with SUMPRODUCT and just hit Enter (Thanks to Simon for teaching me this tip)

As you can guess, column B has ship names, C has captains and D has # of flights.

Using the same logic, you can make a cross tab (or pivot) like the one shown on the right too.

Further Resources:

| More
Excel School - Online Excel Training Program

Comments
Simon July 22, 2009

Could you not just use the sumproduct function in the exact same way, yet with more flexibilty as you are avoiding using it as an array?

ie ( grain of salt on table location, please :) ) =SUMPRODUCT(($A$1:$A$7=$F3)*($B$1:$B$7=G$1)*($C$1:$C$7))

Worm July 22, 2009

I would normally do this with SUMPRODUCT

Is there any advantage to using arrays?

Chandoo July 22, 2009

@Simon.. yeah. Very good point. I didnt know SUMPRODUCT could work like this. Donut for you.. :)

@Worm… I am not sure if there would be some advantages of using arrays. May be when you combine the sum with some other formulas in a model, array formula might be better than the normal version. But I dont know any good examples of that. May be one of the more experienced readers can tell me.

Kapil Kithani July 22, 2009

Hi, I am fairly new to this site and I really thank Chandoo for maintaining this site, but trust me on Sum Product, It is THE best formula that I have ever come across.

For Eg you can replace a PIVOT table using sum product. Yeah I see a lot of frowns, but SUM Product equally works on numbers as well as on text.

For Text, use =sumproduct(–(Range1=”abc”),–(Range2=”XYZ”)) where range1 is the range and abc is the condition in that range
In this, excel would return an array in the 1st range(1s and 0s for matching abc in the Range1), and return an array in the second range(1s and 0s for matching XYZ in the Range2), and voila, you have the data. Just make sure that the start and end points of Range1 and Range2 are same. (Length of both the range should be exactly same or else you get a #VALUE error.

As for Numbers, just remove the 2 hyphens before opening the brackets and remove the condition…. ! :) and additionally, you can mix text and numbers and remove hyphens for any ranges having only numbers(it would return the count of occurence)

Hope you got the point….. I am all for Sum product..

One word of caution though..Since this returns and array for every condition, if might get heavy on the excelsheet.

Ken Puls July 22, 2009

Hi Chandoo,

In Excel 2007 you can also use the new SUMIFS function to sum with multiple conditions. The only rub is that it isn’t backwards compatible like the array or SUMPRODUCT solutions.

Martin July 22, 2009

You guys are fantastic !!! I’m finding easier each time to work with named ranges !
Here’s my little contribution:
Named Ranges (should be dynamic, but….)
Ship $A$2:$A$8
Captain $B$2:$B$8
flights $C$2:$C$8
in F:F
Summary_ship $F$2:$I$2
this 3:3
Summary_Captain $E$3:$E$6

=SUMPRODUCT((Ship=in Summary_ship)*(Captain=this Summary_Captain)*(flights))

data is in range A1:C8, and summary is in E1:I6.

rgds,

Martín

Mathias July 22, 2009

I like that the 8th starship flight on your list has been pixellated. Is this the secret black-ops flight to Titan, and did the Intergalactic Intelligence Agency pressure you to keep that info under wraps?

Cheryl July 23, 2009

Kpil, would you explain what the hyphens are for please? Or is it only to indicate text rather then numbers?
Thank you.

Cheryl July 23, 2009

Kapil, would you explain what the hyphens are for please? Or is it only to indicate text rather then numbers?
Thank you.

derek July 23, 2009

Cheryl, the output of an expression of the form X=Y, in Excel, is one of the logical values TRUE or FALSE. These can often be evaluated as 1 or 0, but SUMPRODUCT needs a little clue first. Typing in two negative signs provides the clue by turning the logcal values into numerical values (negating and then negating again leaves the values unchanged, but now they’re obviously numbers and not Boolean entities).

There is an alternative to two minus signs, and that’s the N() function, which evaluates anything inside the brackets as a number. But there’s no obvious advantage to using N(), and most Excel users don’t bother.

Kapil Kithani July 24, 2009

Cheryl : the 2 hypens make sumproduct return an array, and multiple conditions then have multiple arrays…

Once you have the result in arrays then a sumif is performed internally and result is displayed..

I hope this helps…

Kapil Kithani July 24, 2009

Rather put it simply, it converts the SUMPRODUCT into an array formula (something similar to CTRL+SHIFT+ENTER thing that many ppl do to convert a formula into array formula… )

For More on Arrays, Chandoo and his wonderful site can explain in a much subtle and simpler way :)

Derek: I am sorry, but I did not understand your post. When the two hypens are used, it ask sumproduct to return the output in the form of an array (something similar to CTRL+SHIFT+ENTER). Please correct me if I am wrong, since I use this day in and day out but i never encountered a problem.. :)

Worm July 24, 2009

Derek was correct.

The two ‘hyphens’ are simply two negative symbols, turning TRUE or FALSE into 1 or 0, and using the * between the arrays in Simon’s example does the same thing.

I use the — method because it looks clearer to me, but both work.

My SUMPRODUCT formulas end up like :

=SUMPRODUCT(–(Section=”Management”),–(Salary=”Too High”))

the other way is

=SUMPRODUCT((Section=”Management”)*(Salary=”Too High”))

horses for course really…

Worm July 24, 2009

silly formatting changed two – into –
should be – - ;)

Cheryl July 24, 2009

Thanks! very very helpful :0)

Chandoo July 26, 2009

@Ken: Thanks, I am aware of the IFS functions in excel 2007. Since they are not backward compatible, I thought recommending them would be unwise.

@Kapil, Worm and Derek.. that is so cool. I have always seen – - being used in formulas and wondered what it meant. Now I know :)

@Mathias: Hush… !

@Martin: Very cool.. thanks :)

@Cheryl: You are welcome.

skrell July 27, 2009

i’m sorry if this is a stupid question, but why do you use ‘$’ in the function? Isn’t ($D$3:$D$24) the same thing as writing (D3:D24) meaning the “range of values from D3 block to D24 block”? Is it because you are working with arrays?

Worm July 28, 2009

The $ makes an absolute reference that doesn’t change if you copy the formula to another cell.

If you copy a formula with ($D$3:$D$24) from A1 to B1 it stays the same, but if you copy a formula with (D3:D24) it becomes (E3:E24) – this is probably not going to be what you want – although it might be – depends on circumstances (as always … :) )

This is why using named ranges can be useful as it makes it clearer what is being done.

(IMHO of course…)

Steven July 28, 2009

I am using arrays in a spreadsheet ,but have found them to be very slow. Will my problems be solved if I replace them with SUMPRODUCT?

skrell July 28, 2009

oh! thanks! I learned something today!

Chandoo July 29, 2009

@Steven… It is true that arrays make your spreadsheet slow. But only if the array formula is applied over a large range or is processing lot of cells. But I am sure even sumproduct will become slow when you paste it over thousands of cells.

My suggestion is use sumproduct if you can. It is cleaner and simpler to use. It is easy to explain and change. If the formulas are dragging your spreadsheet, then you may have to change the calculation options to manual and press F9 before taking a coffee break.

Steven August 3, 2009

Thanks Chandoo – you have a great site !

Manoj Tiwari August 15, 2009

Very helpful?
Please solve the following situation in Excel:
A2:A10 is different fruits name
B2:B10 contains either day or night
C2:C10 contains contents in number
Now,
Search “Apple” from A2:A10,
search “Day” from B2:B10 and
sum all the numbers (c2:c10)meeting the above criteria

Chandoo August 15, 2009

@Manoj: Are you not able to use sumproduct for the above?

Worm August 18, 2009

Indeed. Perfect case for SUMPRODUCT

=SUMPRODUCT(–(A2:A10=”Apple”),–(B2:B10=”Day”),C2:C10)
or
=SUMPRODUCT((A2:A10=”Apple”)*(B2:B10=”Day”)*(C2:C10))

Using named ranges would make it even clearer…

Glenn August 20, 2009

I am trying to put together a tool that can can search, column by column,
for a certain background/fill color, using Conditional Formating (Red), and then copy/paste the
value/text in various columns of the same row for each instance of Red
background colored cells, into another worksheet.

Thanks, any help would be greatly appreciated…

RSS feed for comments on this post. TrackBack URI

Leave a comment

   Name (required)

   E-mail (required, never displayed)

   URL


If you have a question, please ask in the forums

Recommended Excel, Charting, VBA books