Sum a column until a specific number is reached

DocDeb

New Member
Joined
Jan 15, 2008
Messages
31
Office Version
  1. 365
Platform
  1. Windows
I would like to Sum a column until 50 is reached. Continue summing the same column again with the remainder from the previous cell if over 50 until 50 is reached again and keep going.
In an adjacent column insert consecutive numbers for each sum of 50.
 
Looks like a FIFO function could be used here. Try the following:

Excel Formula:
=LET(
    note, "the core elements of this method were taken from Peter Bartholomew's custom FIFO functions",
    FIFO, LAMBDA(inAmt,outAmt,
        LET(
            inRows, ROWS(inAmt) + 1,
            outRows, ROWS(outAmt) + 1,
            inRecords, HSTACK(EXPAND(SEQUENCE(inRows),, 2, ""), VSTACK(0, SCAN(0, inAmt, LAMBDA(a,v, a + v)))),
            outRecords, CHOOSECOLS(HSTACK(EXPAND(SEQUENCE(outRows),, 2, ""), VSTACK(0, SCAN(0, outAmt, LAMBDA(a,v, a + v)))), 2, 1, 3),
            unionAll, SORT(VSTACK(inRecords, outRecords), 3),
            inId, SCAN(1, CHOOSECOLS(unionAll, 1), LAMBDA(a,v, IF(v = "", a, v))),
            outId, SCAN(1, CHOOSECOLS(unionAll, 2), LAMBDA(a,v, IF(v = "", a, v))),
            amt, CHOOSECOLS(unionAll, 3),
            netAmt, VSTACK(DROP(amt, 1), TAKE(amt, -1)) - amt,
            FILTER(HSTACK(outId, netAmt), netAmt <> 0)
        )
    ),
    rng, A2:A100,
    arr, FIFO(rng, EXPAND(50, QUOTIENT(SUM(rng), 50),, 50)),
    one, CHOOSECOLS(arr, 1),
    add, one = VSTACK("", DROP(one, -1)),
    HSTACK(arr, SCAN(0, SEQUENCE(ROWS(arr)), LAMBDA(a,v, INDEX(arr, v, 2) + INDEX(add, v) * a)))
)

Adjust the rng reference (A2:A100) as needed. You could also define the FIFO function in Name Manager, if desired.
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Maybe I've misunderstood or being too simplistic here, but isn't the answer just
Excel Formula:
=MOD(SUM(A$2:A2),50)

Book1
AB
1
211
345
4611
52233
61144
76.020.02
833.02
958.02
101119.02
119918.02
121331.02
13637.02
14542.02
152315.02
Sheet1
Cell Formulas
RangeFormula
B2:B15B2=MOD(SUM(A$2:A2),50)


Can you explain what you mean by
Thank you for this. It does work for decimals but not for values over 100.

As far as what I meant with, "In an adjacent column insert consecutive numbers for each sum of 50." I wanted the count of each occurance of 50 where the 50 is occuring.

Which I got to work in a crude way. I'm sure there's an easier way, but this works

Size=A2+IF(B1>=50,MOD(B1,50),B1)=LET(x, MOD(SUM(A$2:A2),50),IF(x=0,50,x))=IF(AND($B2>=50,$C2<=50),"Vol","")=IF(AND($B2>=50,$C2<=50),"Vol "&COUNTIF($D$2:D2,"VOL"),"")
0.5​
0.5​
0.5​
20​
20.5​
20.5​
20​
40.5​
40.5​
11​
51.5​
1.5​
VolVol 1
5​
6.5​
6.5​
 
Upvote 0
Once again:

Show sample. Both sample input data and expected results. Include such details as decimal parts (and how many digits in decimal part are expected in real values), large values (larger than single summation limit), zeros, negative numbers, non-numerical values, etc,

Whichever can be present in real data.

Otherwise we can misunderstand your needs. for instance @

myall_blues

used a simpler version of formula proposed by me, which would result in 0 in case of sum being equal 50, while my proposition =MOD(SUM(A$2:A7)-1,50)+1 would result in 50 in such case. but the side effect is that for numbers with decimal parts yoy end up with 50.02.
Is this what you are referring to?
Column A could be .001 to over 100.
No Negative numbers will be used.
Expected results are accurate as counting up to 50 and starting the counting over while counting the remainder until the next 50 is reached.Expected results are accurate as counting up to 50 and starting the counting over while showing and counting the remainder until the next 50 is reached. But doesn't calculate properly with values over 100 in column A.Shows the breaks where 50 is reached

I would prefer not to need this coulmn
Shows the breaks where 50 is reached and counts the occurances of each break
Size=A2+IF(B1>=50,MOD(B1,50),B1)=LET(x, MOD(SUM(A$2:A2),50),IF(x=0,50,x))=IF(AND($B2>=50,$C2<=50),"Vol","")=IF(AND($B2>=50,$C2<=50),"Vol "&COUNTIF($D$2:D2,"VOL"),"")
0.5​
0.5​
0.5​
20​
20.5​
20.5​
20​
40.5​
40.5​
11​
51.5​
1.5​
VolVol 1
5​
6.5​
6.5​
10​
16.5​
16.5​
5​
21.5​
21.5​
11​
32.5​
32.5​
8​
40.5​
40.5​
13​
53.5​
3.5​
VolVol 2
6​
9.5​
9.5​
4​
13.5​
13.5​
23​
36.5​
36.5​
1​
37.5​
37.5​
4​
41.5​
41.5​
6​
47.5​
47.5​
22​
69.5​
19.5​
VolVol 3
11​
30.5​
30.5​
5​
35.5​
35.5​
3​
38.5​
38.5​
5​
43.5​
43.5​
11​
54.5​
4.5​
VolVol 4
8​
12.5​
12.5​
13​
25.5​
25.5​
6​
31.5​
31.5​
4​
35.5​
35.5​
23​
58.5​
8.5​
VolVol 5
1​
9.5​
9.5​
4​
13.5​
13.5​
3​
16.5​
16.5​
 
Upvote 0
Looks like a FIFO function could be used here. Try the following:

Excel Formula:
=LET(
    note, "the core elements of this method were taken from Peter Bartholomew's custom FIFO functions",
    FIFO, LAMBDA(inAmt,outAmt,
        LET(
            inRows, ROWS(inAmt) + 1,
            outRows, ROWS(outAmt) + 1,
            inRecords, HSTACK(EXPAND(SEQUENCE(inRows),, 2, ""), VSTACK(0, SCAN(0, inAmt, LAMBDA(a,v, a + v)))),
            outRecords, CHOOSECOLS(HSTACK(EXPAND(SEQUENCE(outRows),, 2, ""), VSTACK(0, SCAN(0, outAmt, LAMBDA(a,v, a + v)))), 2, 1, 3),
            unionAll, SORT(VSTACK(inRecords, outRecords), 3),
            inId, SCAN(1, CHOOSECOLS(unionAll, 1), LAMBDA(a,v, IF(v = "", a, v))),
            outId, SCAN(1, CHOOSECOLS(unionAll, 2), LAMBDA(a,v, IF(v = "", a, v))),
            amt, CHOOSECOLS(unionAll, 3),
            netAmt, VSTACK(DROP(amt, 1), TAKE(amt, -1)) - amt,
            FILTER(HSTACK(outId, netAmt), netAmt <> 0)
        )
    ),
    rng, A2:A100,
    arr, FIFO(rng, EXPAND(50, QUOTIENT(SUM(rng), 50),, 50)),
    one, CHOOSECOLS(arr, 1),
    add, one = VSTACK("", DROP(one, -1)),
    HSTACK(arr, SCAN(0, SEQUENCE(ROWS(arr)), LAMBDA(a,v, INDEX(arr, v, 2) + INDEX(add, v) * a)))
)

Adjust the rng reference (A2:A100) as needed. You could also define the FIFO function in Name Manager, if desired.
If I actually knew how to use this, maybe it would work?
I entered it into a cell and get #SPILL!
 
Upvote 0
Is this what you are referring to?
When we say ‘show expected result’, we mean show us what you want to happen, not what you get from formulas we have provided.
For example
  • When it crosses 50, should it show 50+a bit before it resets, or just show remainder e.g. 51 or 1?
  • Likewise, if it gets to 50 exactly, show zero or 50?
  • When you say it ’doesn’t work for values over 100 in column A’, what does that mean? Show us some values over 100 in A and what you’re expecting to happen.
 
Upvote 0
When we say ‘show expected result’, we mean show us what you want to happen, not what you get from formulas we have provided.
For example
  • When it crosses 50, should it show 50+a bit before it resets, or just show remainder e.g. 51 or 1?
  • Likewise, if it gets to 50 exactly, show zero or 50?
  • When you say it ’doesn’t work for values over 100 in column A’, what does that mean? Show us some values over 100 in A and what you’re expecting to happen.
  • When it crosses 50, Column B should show 50+the remainder before it resets e.g. 51. Column C should show just the remainder e.g. 1
  • If it gets to 50 exactly, show 50
  • When Column A has a value over 100, Column C shows the value like it's over 50 not 100, so If I have a value of 101 in Column A, the remainder should be 51, Column C currently shows 1.
Expected result Example 1
ABC
.05.05.05

Expected result Example 2
ABC
999949

Expected result Example 3
ABC
10010050

Expected result Example 4
ABC
10110151
233
49522

Expected result Example 5
ABC
51.451.41.4
23.43.4
4952.42.4
102104.454.4
45.65050
 
Upvote 0
Assuming row 1 is for headers, in B2:
Excel Formula:
=IF(ISNUMBER(C1),IF(C1>50,C1-50,C1),0)+A2
in C2:
Excel Formula:
=IF(B2>50,B2-50,B2)
and copy down

You may nest both formulas inside another if, so for both formulas in B2 and C2:
Excel Formula:
=IF(A2<>"" , formula_as_written_above_but_without_equal_sign , "")
 
Upvote 1
I don't know if i got it right. Isn't the result you need the vol column?

If so, would this work? Just check column E (vol) (the others are just quick helper column to get to that result which can be compacted into a one cell formula)


Book1
ABCDE
1SizeSumMod 50AuxVol
2
3505001Vol 1
425220 
54910111Vol 2
610220332Vol 3 to 4
745.6248.648.60 
8150398.648.63Vol 5 to 7
945443.643.61Vol 8
1015458.68.61Vol 9
Sheet1 (2)
Cell Formulas
RangeFormula
B3:B10B3=SUM($A$3:A3)
C3:C10C3=MOD(SUM($A$3:A3), 50)
D3D3=(B3-C3)/50
E3:E10E3=IF(D3, "Vol "&SUM($D$2:D2) + 1 & IF(D3>1, " to "&SUM($D$2:D2)+D3, ""), "")
D4:D10D4=(B4-C4)/50-SUM($D$3:D3)
 
Upvote 1
Solution
=IF(B2>50,B2-50,B2)
Thank you.
Although, this is still not totally there. :(

0.50.50.5 (Good)
5555.55.5 (Good)
101106.556.5 (Good)
7581.531.5 (This should be 81.5)

But, in Column C, I think I will just use =IF(B2-50<0,B2,B2-50) I wanted a more dynamic calculation, but this should do since Column B is working (as far as I can tell).
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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