Stop Sum when Count Maximum is Reached

Duncan20

New Member
Joined
Feb 12, 2017
Messages
11
Office Version
  1. 365
Platform
  1. Windows
I am trying to achieve a formula that will only sum rows which have a count value of 1 and ignore 0 valued rows.

However I only want the sum of the previous 12 rows with a 1 count value (Column C).

For example:

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[/TR]
[TR]
[TD]A1[/TD]
[TD]100[/TD]
[TD]1[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A2[/TD]
[TD]110
[/TD]
[TD]1[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A3[/TD]
[TD]120[/TD]
[TD]0[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A4[/TD]
[TD]130[/TD]
[TD]1[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A5[/TD]
[TD]140[/TD]
[TD]1[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A6[/TD]
[TD]150[/TD]
[TD]1[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A7[/TD]
[TD]160[/TD]
[TD]1[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A8[/TD]
[TD]170[/TD]
[TD]1[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A9[/TD]
[TD]180[/TD]
[TD]0[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A10[/TD]
[TD]190[/TD]
[TD]1[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A11[/TD]
[TD]200[/TD]
[TD]1[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A12[/TD]
[TD]210[/TD]
[TD]1[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A13[/TD]
[TD]220[/TD]
[TD]1[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A14[/TD]
[TD]230[/TD]
[TD]1[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A15[/TD]
[TD]240[/TD]
[TD]1[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A16[/TD]
[TD]250[/TD]
[TD]0[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A17[/TD]
[TD]260[/TD]
[TD]1[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A18[/TD]
[TD]270[/TD]
[TD]1[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A19[/TD]
[TD]280[/TD]
[TD]1[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A20[/TD]
[TD]290[/TD]
[TD]1[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

I would like cell D20 to sum B20+B19+B18+B17+B15+B14+B13+B12+B11+B10+B8+B7 excluding B16 & B9 as they have a zero value in Column C but to stop at B7 as I only want to sum the total of 12 cells.

There will be no regular pattern for the values in column C other than they will be 1 or 0.

Many thanks in advance
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
You post a similar question before, please try this,ctrl shift enter for the array formul:

Code:
=SUM(OFFSET(B20,,,-MATCH(12,COUNTIF(OFFSET(C20,,,-ROW(1:20),),">0"),),)*OFFSET(C20,,,-MATCH(12,COUNTIF(OFFSET(C20,,,-ROW(1:20),),">0"),),))
 
Upvote 0
Not clear how the output must look like...

Either...

In D1 enter and copy down:

=IF(COUNTIFS($C$1:C1,1)=12,SUMIFS($B$1:B1,$C$1:C1,1),"")

Or, in a single cell, control+shift+enter, not just enter:

=SUM(IF(ROW($C$1:$C$20)<=SMALL(IF($C$1:$C$20=1,ROW($C$1:$C$20)),MIN(12,COUNTIFS($C$1:$C$20,1))),IF($C$1:$C$20=1,$B$1:$B$20)))
 
Upvote 0
I suspect I've massively overengineered this but since I did some work on it, here goes:


Book1
ABCD
1A11001?
2A21101?
3A31200?
4A41301?
5A51401?
6A61501?
7A71601?
8A81701?
9A91800?
10A101901?
11A112001?
12A122101?
13A132201?
14A1423012010
15A1524012150
16A1625002150
17A1726012300
18A1827012440
19A1928012580
20A2029012720
Sheet1
Cell Formulas
RangeFormula
D1{=IFERROR(SUMIF(INDEX($C$1:$C1,MATCH(12,SUBTOTAL(9,OFFSET($C$1,ROW($C$1:$C1)-ROW($C$1),,ROW($C1)-ROW($C$1:$C1)+1)),0)):$C1,1,INDEX($B$1:$B1,MATCH(12,SUBTOTAL(9,OFFSET($C$1,ROW($C$1:$C1)-ROW($C$1),,ROW($C1)-ROW($C$1:$C1)+1)),0)):$B1),"?")}
Press CTRL+SHIFT+ENTER to enter array formulas.


WBD
 
Upvote 0

Forum statistics

Threads
1,223,981
Messages
6,175,771
Members
452,668
Latest member
mrider123

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