Excel VBA Sum Dynamic Range

Nicoo00

New Member
Joined
Mar 8, 2018
Messages
1
Hi,

I am using a VBA that I found here to sum ranges until a cell (all in one column; A) is empty.

VBA:

Code:
Sub MultipleLines()
    Dim cell As Range
    Dim offset_rows As Long


    Set cell = ActiveCell
'or Range("A8").Select
    
    Do While Not IsEmpty(cell.Offset(2, 0))
        offset_rows = Range(cell, cell.End(xlDown)).Rows.Count - 1
        Set cell = cell.End(xlDown).Offset(1, 0)
        cell.FormulaR1C1 = "=SUM(R[-" & offset_rows & "]C:R[-1]C)"
    Loop
End Sub
Example:
1
1
(empty cell)

2
3
4
(empty cell)

This works fine, although "ranges" with only one row are skipped. Does anyone know how I can adjust the VBA so it just puts in =SUM(A###), correct cell reference, in case it is only one row/cell followed by an empty cell?

Thank you in advance.

Niek
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Try this :-
Code:
[COLOR="Navy"]Sub[/COLOR] MG09Mar25
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range("A8", Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]Set[/COLOR] Rng = Rng.SpecialCells(xlCellTypeConstants)
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng.Areas
    Dn(Dn.Count).Offset(1).Formula = "=Sum(" & Dn.Address & ")"
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,214,588
Messages
6,120,409
Members
448,959
Latest member
camelliaCase

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