+ Reply to Thread
Results 1 to 21 of 21

Sum Cells if Another Cell Meets Criteria and Stop When Count is Met

  1. #1
    Registered User
    Join Date
    01-14-2009
    Location
    San Antonio,TX
    MS-Off Ver
    Excel 2003
    Posts
    8

    Sum Cells if Another Cell Meets Criteria and Stop When Count is Met

    We were so close!. But it appears that the assumed correct answer only works if there are no repeating N. The repeating N gets the same count as the last Y and it throws off the sum.

    Please help again...


    I have data in two seperate columns. For Example,
    Rank........Column B2:B4........Column D2:D4
    1...........300.....................Y
    2...........200.....................Y
    3...........100.....................N
    4...........50.......................Y

    Column D1 holds a value, say 2

    I want to sum the numbers in B2:B4 if the corresponding value in D is Y, but only sum up to the number of in D1. In this case 2.

    So the result should be 250

    How do I do this sum and stop when another criteria is met.

    Also note that I will be repeating this process across several Columns such as D2:D4. B2:B4 will be constant, but I will have E1 value and E2:E4 range, F1 value and F2:F4 range etc...
    Last edited by obatiz; 01-29-2009 at 05:49 PM.

  2. #2
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898
    Not sure if you mean where the Rank is above the number in D2...cause that adds up to 250?

    Anyways....

    =Sumproduct((Rank_range>=D$1)*(D$2:D$4="Y"),$B$2:$B$4)

    copied down and perhaps across.
    Last edited by NBVC; 01-14-2009 at 12:42 PM.
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492
    Quote Originally Posted by obatiz View Post
    I have data in two seperate columns. For Example,
    Rank........Column B2:B4........Column D2:D4
    1...........300.....................Y
    2...........200.....................Y
    3...........100.....................N
    4...........50.......................Y

    Column D1 holds a value, say 2

    I want to sum the numbers in B2:B4 if the corresponding value in D is Y, but only sum up to the number of in D1. In this case 2.

    So the result should be 250
    I hope I spotted some errors in your description, because the only way I got 250 was to Sum rankings from the bottom UP, AND to expand the range to B2:B5, etc..

    This formula will do it for the D column:

    =SUMPRODUCT(($A$2:$A$5>=D$1)*(D$2:D$5="Y")*($B$2:$B$5))

    I put that formula in D6. You can copy that across row 6 and it will work for the next row, as long as there are Y/N answers above it and and a reference in row 1.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Registered User
    Join Date
    01-14-2009
    Location
    San Antonio,TX
    MS-Off Ver
    Excel 2003
    Posts
    8
    Thank you for the quick responses. I apologize for not being clear, it is my first time doing this.

    You are correct the only way to get 250 is to count from the bottom. So starting with my lowest rank in this case 4 ,sum the b column values for when Y is found in D. The key is to stop the D1 value has been met, in this case two. So I would sum rank 4 (50) and rank 2 (200) to get the 250.

  5. #5
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898
    Quote Originally Posted by obatiz View Post
    Thank you for the quick responses. I apologize for not being clear, it is my first time doing this.

    You are correct the only way to get 250 is to count from the bottom. So starting with my lowest rank in this case 4 ,sum the b column values for when Y is found in D. The key is to stop the D1 value has been met, in this case two. So I would sum rank 4 (50) and rank 2 (200) to get the 250.
    So then the formulas supplied should work... in mine, you would replace Rank_range with an actual range (same size as column B and D ranges).

  6. #6
    Registered User
    Join Date
    01-14-2009
    Location
    San Antonio,TX
    MS-Off Ver
    Excel 2003
    Posts
    8
    The formula sums all cells with a Y instead of the just the count I need.

    Can we assume for now that the ranking did not exist. An you looked just at column D and summed the the B values for only the first D (Y's) seen as dictated by D1. So starting from the bottom sum the B values for the only the first 5 Ds with Y

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492
    Did you try mine? It works from the bottom up as desired, using your D1 cell as a stop, again, as described.

  8. #8
    Registered User
    Join Date
    01-14-2009
    Location
    San Antonio,TX
    MS-Off Ver
    Excel 2003
    Posts
    8
    I also tried yours and it works for the first column only. So let me expand the problem. Starting from my lowest rank sum column B values for C and D cells that have Y until the count in C1 and D1 is reached

    C1=2 D1=1
    RankA2:A5........Column B2:B5......Column C2:C5.........Column D2:D5
    1.........................300.....................Y........................N
    2.........................200.....................Y........................Y
    3.........................100.....................N........................Y
    4..........................50......................Y........................N
    Expected Anser...............................250......................100

  9. #9
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492
    Ah, ok, then technically the first one isn't working either, it's just accidentally getting the right answer. I viewed C1 as the highest rank position acceptable, you meant it as "number of allowable matches". Not the same thing.

    I'll have to think about it.

  10. #10
    Registered User
    Join Date
    01-14-2009
    Location
    San Antonio,TX
    MS-Off Ver
    Excel 2003
    Posts
    8
    Yes, thanks. Its kind of tough. Me and several folks have been battling this for a few days.

  11. #11
    Forum Contributor mewingkitty's Avatar
    Join Date
    09-29-2008
    Location
    Fort McMurray, Alberta, Canada
    MS-Off Ver
    Excel 2003
    Posts
    949

    Max/min?

    How big/small is the field in question?

    Is it always limited to 4 rows?

    Also note that I will be repeating this process across several Columns such as D2:D4. B2:B4 will be constant, but I will have E1 value and E2:E4 range, F1 value and F2:F4 range etc...
    Descriptions like that tend to throw me off unless I can see them in a worksheet, which I think would be helpful, at least for me to give you a hand. JB seems to have a pretty good handle on it though.
    =IF(AND(OR(BLONDE,BRUNETTE,REDHEAD),OR(MY PLACE,HER PLACE),ME),BOW-CHICKA-BOW-WOW,ANOTHER NIGHT ON THE INTERNET)

  12. #12
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898
    Does this work for you?... it's a bit brute force... but I think it works....

    Please Login or Register  to view this content.
    confirmed with CTRL+SHIFT+ENTER not just ENTER.. You will see { } brackets appear. Then copy across the columns.

  13. #13
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492
    Quote Originally Posted by NBVC View Post
    Does this work for you?... it's a bit brute force... but I think it works....

    Please Login or Register  to view this content.
    confirmed with CTRL+SHIFT+ENTER not just ENTER.. You will see { } brackets appear. Then copy across the columns.
    I get a #NUM error when I tried that directly. And I tried to expand that to a larger data set, which is the implication of the question, and I can't expand it properly.

    I can do this easily with a helper column to Rank the Y answers, simple then. Would that be acceptible? You could hide those columns afterward to keep the appearance clean. Just a thought. (attached)
    Attached Files Attached Files

  14. #14
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898
    Quote Originally Posted by JBeaucaire View Post
    I get a #NUM error when I tried that directly. And I tried to expand that to a larger data set, which is the implication of the question, and I can't expand it properly.
    See attached...
    Attached Files Attached Files

  15. #15
    Registered User
    Join Date
    01-14-2009
    Location
    San Antonio,TX
    MS-Off Ver
    Excel 2003
    Posts
    8
    Not limited to 4 rows. I have to do this analysis across several data sets and each will have a distinct number of rows.

  16. #16
    Registered User
    Join Date
    01-14-2009
    Location
    San Antonio,TX
    MS-Off Ver
    Excel 2003
    Posts
    8
    Helper column is fine

  17. #17
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492
    Great formula, NBVC.

    I know my helper column worked, but NBVC's solution doesn't require it. Just change all the C$5 references to C$100 or whatever. Much nicer. Go with that.

    If that takes care of your need, be sure to EDIT your original post (Go Advanced) and mark the PREFIX box [SOLVED]

  18. #18
    Registered User
    Join Date
    01-14-2009
    Location
    San Antonio,TX
    MS-Off Ver
    Excel 2003
    Posts
    8
    Helper column is fine.

  19. #19
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492
    Quote Originally Posted by obatiz View Post
    Helper column is fine.
    You marked this thread as NOT SOLVED. Did you try the solution in the workbook posted?

    =SUM(IF(INDEX(C$2:C$5,SMALL(IF(C$2:C$5="y",ROW(C$2:C$5)-ROW(C$2)+1),
    MAX(0,COUNTIF(C$2:C$5,"y")-C$1)+1)):C$5="Y",INDEX($B$2:$B$5,
    SMALL(IF(C$2:C$5="y",ROW(C$2:C$5)-ROW(C$2)+1),
    MAX(0,COUNTIF(C$2:C$5,"y")-C$1)+1)):$B$5))


    This formula can be copied and the C$5 can be expanded to C$100 or whatever to cover as deeply as you need. It looks in C$1 for the number of matches you want. And if you copy this formula to D, E, etc, it will adjust itself.

    This one works.
    Last edited by JBeaucaire; 01-15-2009 at 02:38 AM.

  20. #20
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492
    As does this one:

    =SUM(IF(ROW($C$2:$C$15)>=LARGE(IF($C$2:$C$15="y",ROW($C$2:$C$15)),
    MIN(5,COUNTIF($C$2:$C$15,"Y"))),IF($C$2:$C$15="Y",$B$2:$B$15))


    Source: Aladin Akyurek at MrExcel
    Last edited by Simon Lloyd; 01-15-2009 at 03:36 AM.

  21. #21
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898
    From PM:

    ... I have more than the 4 rows of your excel example and I am having trouble changing your formula when my range is 20. Since I have more than 4 is in not as simple as changing the cell start and ends from yours or do I have to add more iterations to your formula as the number of rows grow.
    Yes, you need to just change the ranges throughout the formula... and remember to re-confirm it with CTRL+SHIFT+ENTER not just ENTER... then copy it across..

    For a range from row 2 to row 21 (20 rows)...try:

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1