Recording a macro to copy into a series of variable ranges

G

Guest

I have formulas that i need to copy to fill the end of each of a set of data
ranges. The data is all in A1 to A500, with the ranges seperated by empty
rows. The ranges are of variable length. The formula is required in the A
column just where the blanks appear in the ranges.

Why can't I record a relative address macro that incorporates variable
ranges? I use the end-down-down keys when recording the macro.

Do I have to use a VBA script for something this simple?
 
D

Dan R.

I'm sure there's an easier way to do this but here's one way...


Sub Test()
Dim i As Range, iRng As Range
Dim iTop As String, iBot As String

Set iRng = Range("A1:A500")
iTop = "$A$1"

For Each i In iRng
If Len(i) = 0 Then
iBot = i.Offset(-1, 0).Address
i.Formula = "=SUM(" & iTop & ":" & iBot & ")"
iTop = i.Offset(1, 0).Address
End If
Next

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top