Adding numerical values based on multiple values in another column

G

Guest

Please help
I am trying to add numerical values in one column based on multiple values
in another column.

A B
Red $24
Blue $16
Green $21
Blue $15
Green $12

I would like to add the values in column B for all rows that have a value of
red and green in column A. Thank you in advance for your help!
 
P

Peo Sjoblom

A couple of ways

=SUM(SUMIF(A2:A6,{"Red";"Green"},B2:B6))

or

=SUMPRODUCT(--((A2:A6="Red")+(A2:A6="Green")>0),B2:B6)

both returns 57
 
G

Guest

THANK YOU!!!!! WORKS LIKE A CHARM!!!

Peo Sjoblom said:
A couple of ways

=SUM(SUMIF(A2:A6,{"Red";"Green"},B2:B6))

or

=SUMPRODUCT(--((A2:A6="Red")+(A2:A6="Green")>0),B2:B6)

both returns 57
 
B

Bernard Liengme

Why not just
=SUMPRODUCT((A1:A5="red")+(A1:A5="green"),B1:B5)
No need for "- -" since the addition operator will coerce logical values to
numeric
No need for ">0" since 0 * anything = 0
best wishes
 
P

Peo Sjoblom

The reason I use it is because I want to be fairly consistent, if for
instance the OP had asked Green OR Red in A2:B5 then sum C2:C5. If for
instance A2 holds red and B2 holds green your formula would do 2 x C2 while
mine would do
1 x C2
 
H

Harlan Grove

Bernard Liengme said:
Why not just
=SUMPRODUCT((A1:A5="red")+(A1:A5="green"),B1:B5)
No need for "- -" since the addition operator will coerce logical
values to numeric
....

For another reason,

=SUM(SUMIF(A1:A5,{"red";"green"},B1:B5))

is more efficient both in terms of storage and recalc speed. And it's
more general (not that this is an issue in this instance), i.e.,

=SUM(SUMIF(A:A,{"red";"green"},B:B))

works, while

=SUMPRODUCT((A:A="red")+(A:A="green"),B:B)

will fail in XL2003 and prior versions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top