How about:
Option Explicit
Sub R()
Dim i As Integer
Dim x As Integer
Dim y As Integer
Dim z As Integer
Dim rng As Range
For i = 2 To 15
y = i - z
x = i - 1
Set rng = Range(Cells(y, "H"), Cells(x, "H"))
Cells(i, "H").Formula = "=Sum(" & rng.Address(0, 0) & ")"
z = 0
y = 0
x = 0
Next i
End Sub
But you don't need y = 0, x = 0 at the bottom. Those variables get set in the
top statements and wouldn't care if it was set to 0 or not.
On the other hand, I have no idea what z is used for.
Fad wrote:
Hello, I'm trying to sum certain cells in macro, however, the cell range is
not fixed;(i.e cells are based on variables)
Herebelow is an example of the macro but it's giving an error at the sum
funtion (would appreciate if you could help me find the mistake):
Sub R()
Dim i As Integer, x As Integer, y As Integer, z As Integer
For i = 2 To 15
y = i - z
x = i - 1
Cells(i, "H").Formula = "=Sum(Cells(y, "H"), Cells(x, "H"))"
z = 0
y = 0
x = 0
Next i
End Sub
Thank you
--
Dave Peterson
|