Hello all,
A bit long title :D
I am trying to calculate a moving range based on a defined period
Example:
I have 12 data: 1,2,3,4,5,6,7,8,9,10,11,12
And the period is 4 (written in a cell), note that the period can be different for a different set of data
So the calculation that needs to be performed is: (1+2+3+4)+(2+3+4+5)+,...,+(9+10+11+12) divided by (12-4-1)
How can I write a syntax in VBA to perform such functin in one cell?
My failed test syntax is as follows:
Sample data is from D8 to D19
cell containing period value is E7
And the result should be in cell E8
Sub test()
Dim lr0 As Long
lr0 = Worksheets("sheet1").Range("D8").End(xlDown).Row
For i = 8 To lr0 - 8 + 1 - (Worksheets("sheet1").Range("E7") - 1) Step 1
For j = 8 + Worksheets("sheet1").Range("E7") - 1 To lr0 Step 1
Worksheets("sheet1").Cells(8, 5) = Application.Sum(Worksheets("sheet1").Range("D" & i&), Worksheets("sheet1") _
.Range("D" & j&))
Next j
Next i
End Sub
Can anyone assist me on this?
Thanks in advance!
Bookmarks