文本框值不会分配给vba中的变量

时间:2013-03-23 20:02:09

标签: vba variables textbox

我今天在我的软件上运行测试,发现它产生的一些值不正确。

我决定逐步执行代码并注意到我在用户表单上悬停在空白上时已分配给文本框值的变量,即使悬停在分配给它的文本框上时,用户输入的值显示。< / p>

例如,

n = BiTimeSteps_TextBox.Value

悬停在

之上
n = empty

即使

BiTimeSteps_TextBox.Value = 2

悬停时。

所以说我之后不久有一个公式

d = n*2 ,

n当盘旋时说空,d不成时为0。

如果我将其切换到

,有人告诉我

BiTimeSteps_TextBox.Value = n

它应该被识别,但它仍然没有。

可能导致这种情况的原因是什么?

请参阅下面的完整代码:(它的目的是使用二叉树定价方法为选项定价)

S = BiCurrentStockPrice_TextBox.Value
X = BiStrikePrice_TextBox.Value
r = BiRisk_Free_Rate_TextBox.Value
T = BiexpTime_TextBox.Value
Sigma = BiVolatility_TextBox.Value
n = BiTimeSteps_TextBox.Value

Dim i, j, k As Integer
Dim p, V, u, d, dt As Double


dt = T / n ' This finds the value of dt
u = Exp(Sigma * Sqr(dt)) 'formula for the up factor
d = 1 - u 'formula for the down factor

'V value of option

'array having the values
Dim bin() As Double 'is a binomial arrays, it stores the value of each node, there is a loop

'work out the risk free probability

p = (1 + r - d) / (u - d)

'probability of going up

ReDim bin(n + 1) As Double

'it redims the array, and n+1 is used because it starts from zero
'------------------------------------------------------------------------------------------------------------------------------
''European Call

If BiCall_CheckBox = True Then

    For i = 0 To n 'payoffs = value of option at final time

        bin(i + 1) = Application.WorksheetFunction.Max(0, (u ^ (n - i)) * (d ^ i) * S - X) 

       'It takes the max payoff or 0

       Cells(i + 20, n + 2) = bin(i + 1) 'to view payoffs on the isolated column on the right

    Next i


End If

'european put

If BiPut_CheckBox = True Then

    For i = 0 To n 'payoffs = value of option at final time

        bin(i + 1) = Application.WorksheetFunction.Max(0, X - (S * (u * (n - i)) * (d * i))) 

        ' European Put- It takes the max payoff or 0

        Cells(i + 20, n + 2) = bin(i + 1) 'to view payoffs on the isolated column on the right

    Next i

End If

For k = 1 To n 'backward column loop

    For j = 1 To (n - k + 1) 'loop down the column loop

        bin(j) = (p * bin(j) + (1 - p) * bin(j + 1)) / (1 + r)

        Cells(j + 19, n - k + 2) = bin(j) 
        '' print the values along the column, view of tree

    Next j

Next k

Worksheets("Binomial").Cells(17, 2) = bin(1) ' print of the value V
BiOptionPrice_TextBox = bin(1)

0 个答案:

没有答案