'H' 列是年份数据列,'I' 列是月份数据列,'D' 列是我们要计算每月波动率的 10 年期债券收益率。这是此问题的另一个改进代码,请帮助我。返回仍然是#NAME。感谢@Comintern 的回答。根据@Comintern 的建议,我已经修改了代码。名称管理器中,“Yr”指的是当年的列(H3:H3696),“M”指的是当月的列(I3:I3696),“C_10”指的是中国10年的原始产量数据国债。
现在,我想获得收益率的每月波动率。
Function Volatility(n As Variant) As Variant
'this function uses to calculate volatility of a bond yield
'"n" is the number of data/date we need to calculate
'please manage the data name in the name manager of formulas
Dim i As Integer, dnum As Integer, mnum As Integer, vectornum As Integer
'dnum count day number, mnum count month number
Dim Result(), TempSave() As Variant
Dim Yr, M As Range
vectornum = Int(n / 20) + 1
ReDim Result(vectornum) As Variant
Yr = ActiveWorkbook.Names("Yr").Value
M = ActiveWorkbook.Names("M").Value
Bond = ActiveWorkbook.Names("C_10").Value
For i = 1 To n
If Yr(i) = Yr(i + 1) And M(i) = M(i + 1) Then
dnum = dnum + 1
ReDim Preserve TempSave(1 To dnum)
TempSave(dnum) = Bond(i)
'this is the temporary data container for the same month bond yield
Else
TempSave(dnum + 1) = Bond(i)
'because there is a gap between two month, so, we add the last 'same month bond yield' back
dnum = 0
mnum = mnum + 1
Result(mnum) = Application.WorksheetFunction.StDev_S(TempSave)
End If
Next i
Volatility = Result
End Function