Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
UDF VBA生成零。没有错误值,只有零。有人能查一下我的密码吗_Vba_User Defined Functions - Fatal编程技术网

UDF VBA生成零。没有错误值,只有零。有人能查一下我的密码吗

UDF VBA生成零。没有错误值,只有零。有人能查一下我的密码吗,vba,user-defined-functions,Vba,User Defined Functions,“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(S

“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
下面是电子表格。我想用上面的代码填充标题为远期利率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    

问题是,您从未为
到期日()
即期利率
分配任何值。只要范围是一行或一列,就不需要这些变量
SpotRates(1)
将返回范围内的第一个单元格,
SpotRates(2)
第二个单元格

Function OneYearFwdRates(Mty As Range, Spots As Range) As Double()
    Dim OYFR() As Double                            'OYFR stands for One Year Forward Rates'
    ReDim OYFR(1 To Spots.Rows.Count, 1 To 1)
    Dim i

    For i = 2 To Spots.Rows.Count - 2
        OYFR(i, 1) = (1 + Spots(i + 2)) ^ (Mty(i + 2)) _
                  / (1 + Spots(i)) ^ (Mty(i)) - 1
        ' Debug.Print Spots(i), Mty(i)
    Next i
    OneYearFwdRates = OYFR
End Function

此函数用于返回数组,需要像使用Ctrl+Shift+Enter输入任何其他数组公式一样输入。

您应该提供示例数据。我假设这是一个工作表函数。您还应该说明该过程试图执行的操作。可能有一个内置函数用于此目的。可能嗨TinMan,我刚刚编辑了我的问题以包含数据。谢谢你的回复,希望你能帮忙。我知道用Excel自己简单地键入计算结果更容易,但我很想掌握VBA,尤其是用户定义的函数。TinMan,你的代码要简洁得多,但结果仍然是零。关于您关于不为Dim durities()赋值的评论,我用ReDim durities(Mty.Rows.Count)重新定义了变量-这不正确吗?好的,很高兴知道。但是,您知道(新的和改进的)代码为什么会产生零吗?太好了,您的代码现在可以工作了。你是一个传奇,非常感谢TinMan。哈哈……你一定把我和我的表弟搞混了……来自OZ:)@AJD Fair dinkum mate!!