if, if(and), sumif or sumproduct

pippip

New Member
Joined
Nov 1, 2008
Messages
14
I'm trying to do a formula that does the following:

if A:A = "temp" and B:B = "a" then return 0, but if A:A = "temp" and B:B <> "a" then add C:C (but only those cells in C:C where A:A = "temp" and B:B <> "a").

So, in column A I have various categories (including "temp") and in column B those categories will either have an "a" against them or will be blank. In column C are the figures. In another cell I want to put the formula that will either return 0 (because every "temp" is matched by an "a") or will return the sum of those figures where "temp" is not matched by an "a").

I hope that all makes sense. I've searched around and tried different ways of doing this, but without luck. The stumbling block seems to be adding column C:C only where the two conditions are met.

Thanks in advance for your help.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Code:
=IF(AND(A1="temp",B1<>"a"),C1,0)

Fill down. Then sum C column
 
Upvote 0
Thanks, but that doesn't do it. Perhaps I wasn't too clear. Here's a snapshot:

Col A | Col B | Col C
temp | a | 20
temp | a | 20
temp | | 20

In a single cell (let's say cell F25 though it could be any other) I want to return either 0 if all "temps" are matched with an "a" or, if not, the sum of those cells that are not matched with an "a". (Sorry about the layout, not sure how to post an image.)

In the above example the figure showing in F25 would therefore be 20 (but if all the "temp" entries were matched with an "a" the figure in F25 would be 0).

Hope that's clearer. Thanks again for any input - it's much appreciated.
 
Upvote 0
Try
=SUMPRODUCT(--(A1:A100="temp"),--(B1:B100 < > "a"),C1:C100)


Note,
You cannot use entire column refs like A:A, must use Row #s like A1:A100
Unless using XL2007 or higher
Even then it's not recommended.

Hope that helps.
 
Upvote 0
I'm trying to do a formula that does the following:

if A:A = "temp" and B:B = "a" then return 0, but if A:A = "temp" and B:B <> "a" then add C:C (but only those cells in C:C where A:A = "temp" and B:B <> "a").

So, in column A I have various categories (including "temp") and in column B those categories will either have an "a" against them or will be blank. In column C are the figures. In another cell I want to put the formula that will either return 0 (because every "temp" is matched by an "a") or will return the sum of those figures where "temp" is not matched by an "a").

I hope that all makes sense. I've searched around and tried different ways of doing this, but without luck. The stumbling block seems to be adding column C:C only where the two conditions are met.

Thanks in advance for your help.
If you're using Excel 2007 or later...

=SUMIFS(C:C,A:A,"temp",B:B,"<>A")
 
Upvote 0
Thanks Jonmo1 and T.Valko. I'm using excel 2007 so I tried both formulas.

Jonmo1's sumproduct formula works perfectly (after accepting excel's suggestion from its built-in error correction that <>"a" should be just <"a").

I tried T.Valko's sumifs formula but it didn't work; it just returned 0 even though there are "temp" cells not matched with "a" cells.

Thanks again for your input - I'm so glad to have a solution.
 
Upvote 0
I tried T.Valko's sumifs formula but it didn't work; it just returned 0 even though there are "temp" cells not matched with "a" cells.

Thanks again for your input - I'm so glad to have a solution.
Hmmm....

The SUMIFS should have worked.

2i95n9s.jpg


As you can see in the screencap the formula does return the correct result.

You may have some kind of data problem.
 
Upvote 0
Jonmo1's sumproduct formula works perfectly (after accepting excel's suggestion from its built-in error correction that <>"a" should be just <"a").
Actually, that's not correct.

The correct syntax is: <>"A". Which means: not equal to "A".

If Excel tried changing it to: <"A" that means: less than "A" (which is a valid test comparison).

If it works its by "dumb luck".

An empty cell will evaluate to be less than "A".
 
Upvote 0
Hmmm....

The SUMIFS should have worked.

2i95n9s.jpg


As you can see in the screencap the formula does return the correct result.

You may have some kind of data problem.

Just tried it again and it worked. Perhaps I entered something incorrectly in my earlier attempts.

Thanks again.
 
Upvote 0
Actually, that's not correct.

The correct syntax is: <>"A". Which means: not equal to "A".

If Excel tried changing it to: <"A" that means: less than "A" (which is a valid test comparison).

If it works its by "dumb luck".

An empty cell will evaluate to be less than "A".

I thought it was strange it didn't accept the 'not equal to' syntax and preferred the 'less than' syntax. I'm happy with dumb luck when it works.

And now I have two solutions and my understanding of excel continues to grow.
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,843
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top