Hi,

Im writing some code to calculate daily, monthly and annual realised volatlity. ( I know i can do this with stdev but for what i want to use it for i want a manual calc.)

I keep getting "subscript out of range" error once it goes beyond my last cell in the monthly calcluation

Any ideas why?

Im writing some code to calculate daily, monthly and annual realised volatlity. ( I know i can do this with stdev but for what i want to use it for i want a manual calc.)

I keep getting "subscript out of range" error once it goes beyond my last cell in the monthly calcluation

Any ideas why?

Code:

```
Sub RealisedVolbyDate1()
Dim cell As Variant
Dim cell2 As Variant
Dim cell3 As Variant
Dim count As Double
Dim rng As range
Dim rng2 As range
Dim rng3 As range
Dim formula As Double
Dim i As Long
Dim dte As Variant
Set rng = range("D7", range("D7").End(xlDown))
Set rng2 = range("D27", range("D27").End(xlDown))
Set rng3 = range("D252", range("D252").End(xlDown))
Set dte = range("G3")
cell = rng
cell2 = rng2
cell3 = rng3
For i = LBound(cell, 1) To UBound(cell, 1)
If dte = "Daily" And cell(i, 1) > 0 Then
rng(i, 3).formula = "=SQRT(SUM(RC[-1]/COUNT(RC[-1])))"
ElseIf dte = "Monthly" And cell2(i, 1) > 0 Then
rng2(i, 3).formula = "=SQRT(SUM(R[-21]C[-1]:R[0]C[-1])/COUNT(R[-21]C[-1]:R[0]C[-1])*12)"
ElseIf dte = "Annually" And cell3(i, 1) > 0 Then
rng3(i, 3).formula = "=SQRT(SUM(R[-251]C[-1]:R[0]C[-1])/COUNT(R[-251]C[-1]:R[0]C[-1])*252)"
End If
Next
End Sub
```

Last edited by a moderator: