编译错误,在VBA中找不到变量

时间:2012-11-13 21:30:32

标签: function vba

我有以下代码,我收到以下错误: Compile error, variable not found(无法找到j) 此外,即使我将MktVol作为长度为4的向量提供,消息框也会返回N=0 你能帮我解决我的错误。

Public Function EstimateAllParameters(params, MktStrike, MktVol, F, T, b)
Dim R As Double, a As Double, V As Double, N As Integer
Dim j as integer 'stops the compile error but the function returns #VALUE! 
Dim ModelVol() As Double, sqdError() As Double
R = params(1)
V = params(2)
a = params(3)
N = MktVol.Length
MsgBox ("N= " & N)
For j = 1 To N
 ModelVol(j) = Svol(a, b, R, V, F, MktStrike(j), T)
  sqdError(j) = (ModelVol(j) - MktVol(j)) ^ 2
Next j
EstimateAllParameters = Sum(sqdError)
End Function

MktStrike在单元格E5中具有以下值:E8

12
13
14
15

MktVol在单元格F5中具有以下值:F8

0.234
0.236
0.242
0.249

1 个答案:

答案 0 :(得分:0)

明显的问题是J没有被Dim'd

我会改变:

Dim R As Double, a As Double, V As Double, N As Integer

Dim R As Double, a As Double, V As Double, N As Integer, J as Integer