【发布时间】:2018-07-06 10:46:26
【问题描述】:
'VBA函数计算0.5、1、1.5、2年等1年远期利率,所有结果为零;没有错误值,只有零。谁能告诉我哪里出错了。谢谢
Function OneYearFwdRates(Mty As Range, Spots As Range) As Variant
Dim Maturities()
ReDim Maturities(Mty.Rows.Count) 'Mty is the range of maturities'
Dim SpotRates()
ReDim SpotRates(Spots.Rows.Count) 'Spots is the range of spot rates'
Dim OYFR() 'OYFR stands for One Year Forward Rates'
ReDim OYFR(Spots.Rows.Count)
Dim i
For i = 2 To Spots.Rows.Count - 2
OYFR(i) = (1 + SpotRates(i + 2)) ^ (Maturities(i + 2)) _
/ (1 + SpotRates(i)) ^ (Maturities(i)) - 1
Next i
OneYearFwdRates = OYFR
End Function
下面是电子表格。我想用上面的代码填充标题为 Forward Rates, f(x,1) 的蓝色列
Forward Rate
Maturity Spot Rate f(x,1)
0.00 0.0200
0.5 0.0218
1.0 0.0231
1.5 0.0243
2.0 0.0253
2.5 0.0261
3.0 0.0268
3.5 0.0273
4.0 0.0277
4.5 0.0281
5.0 0.0284
5.5 0.0287
6.0 0.0289
6.5 0.0291
7.0 0.0293
7.5 0.0295
8.0 0.0296
8.5 0.0298
9.0 0.0299
9.5 0.0300
10.0 0.0301
10.5 0.0302
11.0 0.0303
11.5 0.0303
12.0 0.0304
12.5 0.0304
【问题讨论】:
-
您应该提供示例数据。我假设这被用作 WorksheetFunction。您还应该说明程序试图做什么。为此目的可能有一个内置函数。可能More Forward Rates Lessons: How to calculate Forward Rates – Calculations walk through
-
嗨 TinMan,我刚刚编辑了我的问题以包含数据。感谢您的回复,希望对您有所帮助。我知道在 Excel 中简单地输入计算会更容易,但我很想掌握 VBA,尤其是用户定义的函数。
标签: vba user-defined-functions