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
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try this. data in Column A
VBA Code:
Sub Test()

Dim cell As Range, rngSum As Range, rngData As Range
Dim ws As Worksheet

Set ws = ActiveWorkbook.Sheets("Sheet1")
Set rngData = ws.Range("A1", ws.Cells(Rows.Count, "A").End(xlUp))

For Each cell In rngData
    If cell = "" Then
        Set rngSum = ws.Range(cell.Offset(1), cell.Offset(1).End(xlDown))
        cell = "=SUM(" & rngSum.Address(0, 0) & ")"
    End If
Next

End Sub
 
Upvote 0
Welcome to the MrExcel Message Board! :)

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Sum Cell Values Until Blank Cells With VBA - OzGrid Free Excel/VBA Help Forum
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Try this. data in Column A
VBA Code:
Sub Test()

Dim cell As Range, rngSum As Range, rngData As Range
Dim ws As Worksheet

Set ws = ActiveWorkbook.Sheets("Sheet1")
Set rngData = ws.Range("A1", ws.Cells(Rows.Count, "A").End(xlUp))

For Each cell In rngData
    If cell = "" Then
        Set rngSum = ws.Range(cell.Offset(1), cell.Offset(1).End(xlDown))
        cell = "=SUM(" & rngSum.Address(0, 0) & ")"
    End If
Next

End Sub
Thank you. The code works perfectly, there is a special case for a single cell. The code will ignore that cell.
 
Upvote 0
Do you mean something like this?
VBA Code:
Sub Test()

Dim cell As Range, rngSum As Range, rngData As Range
Dim ws As Worksheet

Set ws = ActiveWorkbook.Sheets("Sheet1")
Set rngData = ws.Range("A1", ws.Cells(Rows.Count, "A").End(xlUp))

For Each cell In rngData
    If cell = "" And Not cell.Offset(2) = "" Then
        Set rngSum = ws.Range(cell.Offset(1), cell.Offset(1).End(xlDown))
        cell = "=SUM(" & rngSum.Address(0, 0) & ")"
    End If
Next

End Sub
 
Upvote 0
Do you mean something like this?
VBA Code:
Sub Test()

Dim cell As Range, rngSum As Range, rngData As Range
Dim ws As Worksheet

Set ws = ActiveWorkbook.Sheets("Sheet1")
Set rngData = ws.Range("A1", ws.Cells(Rows.Count, "A").End(xlUp))

For Each cell In rngData
    If cell = "" And Not cell.Offset(2) = "" Then
        Set rngSum = ws.Range(cell.Offset(1), cell.Offset(1).End(xlDown))
        cell = "=SUM(" & rngSum.Address(0, 0) & ")"
    End If
Next

End Sub
Thanks zot. Not the best but you have helped me a lot. Thank you
 
Upvote 0
Do you mean something like this?
VBA Code:
Sub Test()

Dim cell As Range, rngSum As Range, rngData As Range
Dim ws As Worksheet

Set ws = ActiveWorkbook.Sheets("Sheet1")
Set rngData = ws.Range("A1", ws.Cells(Rows.Count, "A").End(xlUp))

For Each cell In rngData
    If cell = "" And Not cell.Offset(2) = "" Then
        Set rngSum = ws.Range(cell.Offset(1), cell.Offset(1).End(xlDown))
        cell = "=SUM(" & rngSum.Address(0, 0) & ")"
    End If
Next

End Sub
1639283068653.png
Hello Zot, I try to think about it. The offset function crashes with single cells. Can you help me?
 
Upvote 0
If your sheet is sheet1; column A starts from A1:
VBA Code:
Sub test()
Dim i, k, Lr As Long
With Sheets("Sheet1")
Lr = .Cells(Rows.Count, "A").End(xlUp).Row
For i = Lr To 1 Step -1
    If .Range("A" & i).Value = "" Then
    .Range("A" & i).Value = k
    k = 0
    Else
    k = k + .Range("A" & i).Value
    End If
Next
End With
End Sub
 
Upvote 0
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)
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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