# How to add multiple columns based on criteria in rows

1. ## How to add multiple columns based on criteria in rows

Hi All,

I'm trying to add figures in multiple rows based on multiple conditions at row level.

I did include an attachment cuase that is the easiest way to understand my issue. I know how to do this using indirect but my boss hates any file that has indirect in it!!!!

2. ## Re: How to add multiple columns based on criteria in rows

One way:

=SUM((INDEX(INDEX(\$E\$7:\$N\$11,MATCH(1,INDEX((\$B\$7:\$B\$11=\$E\$16)*(\$C\$7:\$C\$11=\$E\$17)*(\$D\$7:\$D\$11=\$E\$18),0),0),),MATCH(E14,\$E\$6:\$N\$6,0))):(INDEX(INDEX(\$E\$7:\$N\$11,MATCH(1,INDEX((\$B\$7:\$B\$11=\$E\$16)*(\$C\$7:\$C\$11=\$E\$17)*(\$D\$7:\$D\$11=\$E\$18),0),0),),MATCH(E15,\$E\$6:\$N\$6,0))))

3. ## Re: How to add multiple columns based on criteria in rows

You sent me a PM asking for an explanation. Well, here it is... Your formula in your sheet was:

=SUM((INDEX(\$E\$7:\$N\$7,MATCH(E14,\$E\$6:\$N\$6,FALSE)))INDEX(\$E\$7:\$N\$7,MATCH(E15,\$E\$6:\$N\$6,FALSE))))

The fist thing I did was move part of it down (the bits in red) to refer to the correct row (just so that I would know when i was going to get teh right answer. it then looked like:

=SUM((INDEX(\$E\$9:\$N\$9,MATCH(E14,\$E\$6:\$N\$6,FALSE)))INDEX(\$E\$9:\$N\$9,MATCH(E15,\$E\$6:\$N\$6,FALSE))))

So you formula, at its most basic used two index Matches to do =SUM(from here - to here)

Next step, OK. How can I return that?

=SUM((INDEX(\$E\$9:\$N\$9,MATCH(E14,\$E\$6:\$N\$6,FALSE)))INDEX(\$E\$9:\$N\$9,MATCH(E15,\$E\$6:\$N\$6,FALSE))))

I decided to write a formual that would take the entire table and then use the criteria that you set. I came up with:

=INDEX(\$E\$7:\$N\$11,MATCH(1,(\$B\$7:\$B\$11=\$E\$16)*(\$C\$7:\$C\$11=\$E\$17)*(\$D\$7:\$D\$11=\$E\$18),0),)

This was looking at the entire range (red), and then (blue) looking at your 3 criteria in turn (muliplying them for criterion 1 AND criterion 2 AND criterion3). This returns and array of 1s for TRUE and 0s for false... ultimately only one 1.... matching the row in the array where all 3 criteria were met. That formula is an array formula. However, it is easy to coerce it into working as a non array formula by wraping it in an INDEX:

=INDEX(\$E\$7:\$N\$11,MATCH(1,INDEX((\$B\$7:\$B\$11=\$E\$16)*(\$C\$7:\$C\$11=\$E\$17)*(\$D\$7:\$D\$11=\$E\$18),0),0),)

I then substituted that string into YOUR formula to replace the bits in red in the first formula at the top of this explanation. So, all I did was to build on what you had, by adding in a multi-criteria INDEX-MATCH to select teh correct row. It just looks horrendous!!

So... you're welcome.

It would be very nice if you were to just click the Add Reputation button at the foot of any of the posts of members who helped you reach a solution.

4. ## Re: How to add multiple columns based on criteria in rows

Posted again to get rid of the bl@@dy smilies!!!

You sent me a PM asking for an explanation. Well, here it is... Your formula in your sheet was:

=SUM((INDEX(\$E\$7:\$N\$7,MATCH(E14,\$E\$6:\$N\$6,FALSE)))INDEX(\$E\$7:\$N\$7,MATCH(E15,\$E\$6:\$N\$6,FALSE))))

The fist thing I did was move part of it down (the bits in red) to refer to the correct row (just so that I would know when i was going to get teh right answer. it then looked like:

=SUM((INDEX(\$E\$9:\$N\$9,MATCH(E14,\$E\$6:\$N\$6,FALSE)))INDEX(\$E\$9:\$N\$9,MATCH(E15,\$E\$6:\$N\$6,FALSE))))

So you formula, at its most basic used two index Matches to do =SUM(from here - to here)

Next step, OK. How can I return that?

=SUM((INDEX(\$E\$9:\$N\$9,MATCH(E14,\$E\$6:\$N\$6,FALSE)))INDEX(\$E\$9:\$N\$9,MATCH(E15,\$E\$6:\$N\$6,FALSE))))

I decided to write a formual that would take the entire table and then use the criteria that you set. I came up with:

=INDEX(\$E\$7:\$N\$11,MATCH(1,(\$B\$7:\$B\$11=\$E\$16)*(\$C\$7:\$C\$11=\$E\$17)*(\$D\$7:\$D\$11=\$E\$18),0),)

This was looking at the entire range (red), and then (blue) looking at your 3 criteria in turn (muliplying them for criterion 1 AND criterion 2 AND criterion3). This returns and array of 1s for TRUE and 0s for false... ultimately only one 1.... matching the row in the array where all 3 criteria were met. That formula is an array formula. However, it is easy to coerce it into working as a non array formula by wraping it in an INDEX:

=INDEX(\$E\$7:\$N\$11,MATCH(1,INDEX((\$B\$7:\$B\$11=\$E\$16)*(\$C\$7:\$C\$11=\$E\$17)*(\$D\$7:\$D\$11=\$E\$18),0),0),)

I then substituted that string into YOUR formula to replace the bits in red in the first formula at the top of this explanation. So, all I did was to build on what you had, by adding in a multi-criteria INDEX-MATCH to select teh correct row. It just looks horrendous!!

So... you're welcome.

It would be very nice if you were to just click the Add Reputation button at the foot of any of the posts of members who helped you reach a solution.

6. ## Re: How to add multiple columns based on criteria in rows

E21

=sumproduct((--mid(e6:n6,find("-",e6:n6)+1,99)>=--mid(e14,find("-",e14)+1,99))*(--mid(e6:n6,find("-",e6:n6)+1,99)<=--mid(e15,find("-",e15)+1,99))*(b7:b11=e16)*(c7:c11=e17)*(d7:d11=e18)*e7:n11)

7. ## Re: How to add multiple columns based on criteria in rows

Hi Glenn,

Was thinking this morning..... I think the formula would look 'less horrendous' if i were to concatenate the criteria and then base the search on the concatenated criteria.

So i would need to change this

=INDEX(\$E\$7:\$N\$11,MATCH(1,INDEX((\$B\$7:\$B\$11=\$E\$16)*(\$C\$7:\$C\$11=\$E\$17)*(\$D\$7:\$D\$11=\$E\$18),0),0),)

This would essential drop my criteria from 3 to 1. I tried to play around with the statment but im getting a #ref!

8. ## Re: How to add multiple columns based on criteria in rows

Rather than clutter your sheet up, you could always use the repeated bit as a Named Range. I used this:

=INDEX(Sheet2!\$E\$7:\$N\$11,MATCH(1,INDEX((Sheet2!\$B\$7:\$B\$11=Sheet2!\$E\$16)*(Sheet2!\$C\$7:\$C\$11=Sheet2!\$E\$17)*(Sheet2!\$D\$7:\$D\$11=Sheet2!\$E\$18),0),0),)

to create a Named Range, called... wait for it.... Range.

The formula in the sheet then becomes:

``Please Login or Register  to view this content.``

9. ## Re: How to add multiple columns based on criteria in rows

That bloody smiley again!! I do not know how to disable it permanently!!!

10. ## Re: How to add multiple columns based on criteria in rows

Glenn, I edited it by putting the formula inside of code tags. That seems to fix the problem that occurs when you have colon and a right or left parens next.

11. ## Re: How to add multiple columns based on criteria in rows

Thank you Alan! The is no carpet in the room I'm in. If there was, I'd be chewing it in rage!!

12. ## Re: How to add multiple columns based on criteria in rows

Hi Glenn,

Many thanks for that, amazing! My wife cannot understand why I am in front of a spreadsheet on a Saturday morning!

I never knew that a range can actually be a formula.

Some of my colleagues might freak out when they see a formula in a range. would you be able to guide me how to replace INDEX(\$E\$7:\$N\$11,MATCH(1,INDEX((\$B\$7:\$B\$11=\$E\$16)*(\$C\$7:\$C\$11=\$E\$17)*(\$D\$7:\$D\$11=\$E\$18),0),0),)

to use just one criterion instead of three. And one final question, in that statement, what is the function of the wildcard ?

13. ## Re: How to add multiple columns based on criteria in rows

What wildcard?

14. ## Re: How to add multiple columns based on criteria in rows

I thought that the * in the statement below is a wild card.
INDEX(\$E\$7:\$N\$11,MATCH(1,INDEX((\$B\$7:\$B\$11=\$E\$16)*(\$C\$7:\$C\$11=\$E\$17)*(\$D\$7:\$D\$11=\$E\$18),0),0),)

And sorry to pester you but im super curious. : how can I change the above statement to just feed from one criteria?

15. ## Re: How to add multiple columns based on criteria in rows

No it is a multiplication. Effectively requring an AND for each of the criteria.

The price of what you wan tis an array formula.

Formula:
`Please Login or Register  to view this content.`

You seem to be using an older version of Excel than me. So, please refer to the attached file. If the formulae are enclosed within a pair of { }, these are array formulae.

These are a little different from ordinary formulae in that they MUST be confirmed in the FIRST CELL ONLY by pressing CTRL+SHIFT+ENTER to activate the array, not just ENTER. After that, the array can be dragged down as normal, to cover the desired range.

You will know the array is active when you see the curly brackets { } appear around the outside of your formula. If you do not use CTRL+SHIFT+ENTER you will (almost always) get an error message or an incorrect answer. Press F2 on that cell and try again.

Don't type the curly brackets yourself - it won't work...

if your colleagyues don't like Named Ranges (idiots) they will not like array formulae either. If you click on a cell with an array formula, it loses the array and needs CTRL-SHIFT-ENTER again.

Go for the Range, or offer to buy them a piece of slate and a chisel instead of Excel... or an abacus...

16. ## Re: How to add multiple columns based on criteria in rows

Hi there glenn,

Again many thanks for your reply. I was reading online that it can be done with an array formula but i thought there must be a simpler way.... not a huge fan or array formulae to be honest. I will go with the first solution.

many many thanks

17. ## Re: How to add multiple columns based on criteria in rows

Neither am I. Users with five thumbs on each hand always knock them over... so I always tried to avoid them. Office 365 has done away with them, but has brought along a pile of new things i'm trying to learn!

But Named ranges as SOOOO useful, as you will find. Indeed, I will make one further improvement. The NR should really be made dynamic, to adjust to the size of your data, automatically.

i'll do that in a moment.

18. ## Re: How to add multiple columns based on criteria in rows

1. Are the criteria and the formula on the same sheet, or a different one?

2. If on the same one, can I move the criteria and formulae?

3. If on a different one, can it be guaranteed that there will be NOTHING below AND to the right of the raw data?

19. ## Re: How to add multiple columns based on criteria in rows

Take a look at this. It will accept new rows (currently to 1000) and columns (up to column BZ), without you needing to make any change to the ranges in the formula. They ALL adjust automatically.

There are currently 1 users browsing this thread. (0 members and 1 guests)