volabos
06-05-2008, 09:35 AM
Hi all, I have written following UDF to calculated moving average, with window size 12:
Option Base 1
Function vv(data As Range) As Variant
Dim n As Integer, i As Integer, j As Integer, number_year As Integer
Dim summ As Double
n = data.Rows.Count
ReDim mov_avg1(1, 1 To (n - 11)) As Double
For i = 1 To (n - 11)
summ = 0
For j = i To (i + 11)
summ = summ + data(j)
Next j
mov_avg1(1, i) = summ / 12
Next i
vv = mov_avg1
End Function
However when I use this UDF I get all value same as mov_avg1(1). Can anyone please clarify me where I made mistake?
Regards,
Option Base 1
Function vv(data As Range) As Variant
Dim n As Integer, i As Integer, j As Integer, number_year As Integer
Dim summ As Double
n = data.Rows.Count
ReDim mov_avg1(1, 1 To (n - 11)) As Double
For i = 1 To (n - 11)
summ = 0
For j = i To (i + 11)
summ = summ + data(j)
Next j
mov_avg1(1, i) = summ / 12
Next i
vv = mov_avg1
End Function
However when I use this UDF I get all value same as mov_avg1(1). Can anyone please clarify me where I made mistake?
Regards,