Sequential Summing with For Loops

johnpalf

New Member
Joined
May 27, 2014
Messages
25
I need a way to sum the cells in a column that have been created in a variable manner. In the code I have attached I'm thinking I can insert an if statement that does this when the for loop has finished. Say the for loop runs 3 times I would need the cells in column K to sum sequentially. IE on the bottom most row, K would just be the value of J in the same row. In the 2nd from the bottom row, K would be the sum of column J in both rows. In the 3rd from bottom (top row), K would be the sum of column J, rows 1-3. Is there a way to do this? I think it's beyond my skill level.

If I didn't do it this way, once the rows have been created I could go in manually and enter the formula in the first cell =sum(J1:J$3) and then drag down to autocomplete. Can this be done with code so it's automated and doesn't have to necessarily be 3 rows?

Code:
Range("C15").Select 'set active cell to first catalyst
    For j = 1 To Unit1 'loops for each row that needs to be created
    Cat1.Select
    Selection.Copy
    Selection.Insert Shift:=xlDown
'    If j = Unit1 Then
'        ActiveCell.Offset(0, 8).Formula = "=sum(ActiveCell.Offset(0,8))"
'
    Next j
    
    For j = 1 To Unit2
    Cat2.Select
    Selection.Copy
    Selection.Insert Shift:=xlDown
    Next j
   
    For j = 1 To Unit3
    Cat3.Select
    Selection.Copy
    Selection.Insert Shift:=xlDown
    Next j
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Forum statistics

Threads
1,216,735
Messages
6,132,423
Members
449,727
Latest member
Aby2024

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