Sum Cell Values Until Blank Cells With VBA

blueman0110

New Member
Joined
Sep 22, 2021
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hello,
I found this code on the internet. How do I get the sum value to appear in the empty cell above the cells containing consecutive values. I'm thinking of running the code from the last row. Thank for your help :D

How to sum cell values in a column until blank cell reached?

Sub InsertTotals()
'Updateby Extendoffice
Dim xRg As Range
Dim i, j, StartRow, StartCol As Integer
Dim xTxt As String
On Error Resume Next
xTxt = ActiveWindow.RangeSelection.AddressLocal
Set xRg = Application.InputBox("please select the cells:", "Kutools for Excel", xTxt, , , , , 8)
If xRg Is Nothing Then Exit Sub
StartRow = xRg.Row
StartCol = xRg.Column
For i = StartCol To xRg.Columns.Count + StartCol - 1
For j = xRg.Row To xRg.Rows.Count + StartRow - 1
If Cells(j, i) = "" Then
Cells(j, i).Formula = "=SUM(" & Cells(StartRow, i).Address & ":" & Cells(j - 1, i).Address & ")"
StartRow = j + 1
End If
Next
StartRow = xRg.Row
Next
End Sub

1632369292869.png
 
Assuming that the numbers to be added are not the results of formulas and are on the active sheet (code can be adjusted if either is not the case)

VBA Code:
Sub Insert_Totals()
  Dim rA As Range
 
  For Each rA In Columns("A").SpecialCells(xlConstants, xlNumbers).Areas
    rA.Cells(0).Formula = "=SUM(" & rA.Address & ")"
  Next rA
End Sub

My sample data before the code:

blueman0110.xlsm
A
1
23
31
49
5
65
77
82
9
102
11
126
136
142
15
164
173
189
198
207
Sheet1


.. and after:

blueman0110.xlsm
A
113
23
31
49
514
65
77
82
92
102
1114
126
136
142
1531
164
173
189
198
207
Sheet1
Cell Formulas
RangeFormula
A1A1=SUM($A$2:$A$4)
A5A5=SUM($A$6:$A$8)
A9A9=SUM($A$10)
A11A11=SUM($A$12:$A$14)
A15A15=SUM($A$16:$A$20)
Thank you, Peter. It works perfectly. Can you guide me how to edit if the starting position is not A1?.
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Can you guide me how to edit if the starting position is not A1?.
Try something like this

Rich (BB code):
Sub Insert_Totals_v2()
  Dim rA As Range
  
  For Each rA In Range("C3", Range("C" & Rows.Count).End(xlUp)).SpecialCells(xlConstants, xlNumbers).Areas
    rA.Cells(0).Formula = "=SUM(" & rA.Address & ")"
  Next rA
End Sub

In the sheet below, the green cells were empty before the code was run.

blueman0110.xlsm
C
1
213
33
41
59
614
75
87
92
102
112
1214
136
146
152
1631
174
183
199
208
217
Sheet1
Cell Formulas
RangeFormula
C2C2=SUM($C$3:$C$5)
C6C6=SUM($C$7:$C$9)
C10C10=SUM($C$11)
C12C12=SUM($C$13:$C$15)
C16C16=SUM($C$17:$C$21)
 
Upvote 0
Solution
Try something like this

Rich (BB code):
Sub Insert_Totals_v2()
  Dim rA As Range
 
  For Each rA In Range("C3", Range("C" & Rows.Count).End(xlUp)).SpecialCells(xlConstants, xlNumbers).Areas
    rA.Cells(0).Formula = "=SUM(" & rA.Address & ")"
  Next rA
End Sub

In the sheet below, the green cells were empty before the code was run.

blueman0110.xlsm
C
1
213
33
41
59
614
75
87
92
102
112
1214
136
146
152
1631
174
183
199
208
217
Sheet1
Cell Formulas
RangeFormula
C2C2=SUM($C$3:$C$5)
C6C6=SUM($C$7:$C$9)
C10C10=SUM($C$11)
C12C12=SUM($C$13:$C$15)
C16C16=SUM($C$17:$C$21)
Great. I don't know what to say to thank you. I have been researching how to do it for more than 1 month. You have taught me a lesson in rudimentary thinking.
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,121
Members
452,381
Latest member
Nova88

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