2012-11-13 34 views
0

我有以下代碼,並且出現以下錯誤: Compile error, variable not found(它無法找到j) 另外,即使我提供MktVol作爲長度爲4的矢量,消息框返回N=0 你能幫我解決我的錯誤嗎?編譯錯誤,在VBA中找不到變量

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 
+0

您沒有在該代碼變暗'j'。另外,你究竟傳遞給'MktVol'的是什麼? – LittleBobbyTables

+0

我試過了,它給了我#VALUE !,因此我試圖在沒有這個的情況下運行。我正在使用我傳遞給MktVol的值更新我的初始帖子 – user1155299

+1

Range'對象沒有'Length'屬性。也許你的意思是'MktVol.Cells.Count'?在填充它們的值之前,您還需要將陣列重新定位到正確的大小。並且'Sum'不是VBA函數,但是您可以使用Application.Sum來代替。 –

回答

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