我是VBA的新手,并尝试使用从-n
到n
定义的参数创建三项期权定价树。如何使用双循环重写VBA函数以提供所有输出,因为它似乎仅显示S
的值。最后,我想对所有值求和并对其进行折价,但我无法超越仅提供一个输出的双循环。如果将MsgBox放在k
和i
循环之间,则它不会返回任何内容。请帮助我,因为我还没有VBA的经验。
Public Function LoopTest(S As Double, u As Double, n As Integer)
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim St() As Double
ReDim St(2 * n + 1)
For k = 0 To n Step 1
For i = -k To k Step 1
St(i, k) = S * u ^ i
Debug.Print St(i, k)
MsgBox ("DebugToPrint")
' Payoff(i,k) = Application.WorksheetFunction.Max((St(i,k) - 20), 0)
Next i
Next k
' For i from n to 2 step -1
' For j from 1 to n - 2
' Payoff(i - 1, j) = Payoff(i, j) + Payoff(i, j + 1) + Payoff(i, j + 2)
' Next i
' Next k
LoopTest = St()
End Function
我如何获得输出
k=0 St = S*u^0 k=1 St = S*u^-1 , St = S*u^0 , St = S*u^1 k=2 St = S*u^-2 , St = S*u^-1 , St = S*u^0 , St = S*u^1 , St = S*u^2
等,然后将所有内容汇总为输出?
答案 0 :(得分:0)
我并没有完全遵循该示例,但是从描述中看来,您似乎对递归过程感兴趣。它可能很棘手,如果您不小心,将使您陷入无尽的循环。上一篇文章对您有帮助吗? VBA recursive "For loops" Permutation?