Fill numbers based on Sum

janu319

New Member
Joined
Feb 11, 2021
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hallo,


Hope someone can help me with this request.
I have numbers between 4 and 24 in the column B(from cell B2 to B161).
I want to fill the cells in the column C with 1 to 63, based on the sum of the cells in column B.
If the sum of continuous cells in column B(minimum 1 and maximum 5 cells) is <= 24, then fill 1 for those corresponding cells in column C. Then jump to next cells and repeat the same procedure and fill 2 and so on.


In this sample, the sum of B2 to B6(5 cells) is 20, so fill C2 to C6 with 1.
Sum of B7 to B10 is 21(adding B11 makes is 29), so fill C7 to C11 with 2.
At B16, adding B17 makes is 27, so C16 filled with 5.


Can this be achieved with formulas?
Looks like it is possible with a for loop and if loops in a VBA, but I'm not good in it.

Thanks in advance.

Kind regards,
Janu

Fill_Numbers.xlsx
ABC
1S.NoCountFill numbers
2141
3241
4341
5441
6541
7642
8742
9862
10972
111083
121193
131284
1413114
151444
1615165
1716116
181776
191897
201957
212057
222147
23224
24239
25246
262512
27265
282712
292811
30296
31305
323113
333216
34334
35345
36357
37368
38378
39384
40394
41406
Sheet2
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Is the criterion <24 or <=24? In one place you sate <=24, but somewhere esle, you increment column C by 1 when the total was 20 and the next total was 24....
 
Upvote 0
Please try the following on a copy of your workbook. Change the sheet name to suit.
VBA Code:
Option Explicit
Sub janu319()
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet2")       '<-- *** Change sheet name to suit ***
    Dim a, b, i As Long, j As Long, x As Long
    a = ws.Range("B2:B" & ws.Cells(Rows.Count, "B").End(xlUp).Row)
    ReDim b(1 To UBound(a, 1), 1 To 1)
    j = 1    
    For i = 1 To UBound(a, 1)
        x = x + a(i, 1)
        If x <= 24 Then
            b(i, 1) = j
        Else
            x = 0
            i = i - 1
            j = j + 1
        End If
    Next i
    ws.Range("C2").Resize(UBound(b, 1)).Value = b
End Sub
 
Upvote 0
Thank you Kevin9999.
It almost meets my requirement except one thing, I can only sum 5 continuous cells, not 6.

Fill_Numbers.xlsx
ABC
1S.NoCountFill numbers
2141
3241
4341
5441
6541
7641
8742
9862
10972
111083
121193
131284
1413114
151444
1615165
1716116
181776
191897
201957
212057
222147
232248
242398
252468
2625129
272659
28271210
29281110
3029611
3130511
32311311
33321612
3433412
3534513
3635713
3736813
Sheet2
 
Upvote 0
Sorry, I missed that :(
Try this instead
VBA Code:
Option Explicit
Sub janu319()
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet2")       '<-- *** Change sheet name to suit ***
    Dim a, b, i As Long, j As Long, k As Long, x As Long
    a = ws.Range("B2:B" & ws.Cells(Rows.Count, "B").End(xlUp).Row)
    ReDim b(1 To UBound(a, 1), 1 To 1)
    j = 1: k = 1
    For i = 1 To UBound(a, 1)
        x = x + a(i, 1)
        If x <= 24 And k <= 5 Then
            b(i, 1) = j
            k = k + 1
        Else
            x = 0
            i = i - 1
            j = j + 1
            k = 1
        End If
    Next i
    ws.Range("C2").Resize(UBound(b, 1)).Value = b
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,248
Messages
6,171,021
Members
452,374
Latest member
keccles

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