函数在未被调用的情况下被调用并停止主宏
Function is called without being called and stops the main macro
我有这个 Sub,它在一个循环中执行 2 个基本计算,然后我有一个 UDF(见下文)。
我的问题: 当我 运行 Sub BSM_Table
它调用此行的函数:If Cells(i, 2) > 0 And Cells(i, 3) > 0 Then
和子停止。
我猜这是因为这些单元格:Cells(i, 2)
和 Cells(i, 3)
被函数使用,所以只要这些单元格发生变化,函数就会自动重新计算。
有没有办法在我需要的时候只调用函数?
Sub BSM_Table()
Dim i As Long
Dim LastRow As Long, LastRow2 As Long
Dim Firm_Value As Double, Face_Value As Double, Rate As Double, Volatility_value As Double, Time As Double
With Sheets("BlackScholeMerton")
LastRow = Sheets("BlackScholeMerton").Range("B42:B" & Rows.Count).End(xlDown).Row
For i = 42 To LastRow
' If Cells(i, 2) > 0 And Cells(i, 3) > 0 Then
Range("E" & i).Value = Range("B" & i).Value + Range("C" & i).Value
x = Cells(i, 4) * Cells(i, 2)
y = Cells(i, 5)
Cells(i, 6) = x / y
' End If
Next i
End sub
我的函数:
Function DefProb(Firm_Value As Double, Face_Value As Double, Rate As Double, Volatility_value As Double, Time As Double) As Double
Dim t1 As Double, t2 As Double, t3 As Double
Dim d2 As Double, ta As Double, tb As Double
Dim V As Double, S As Double, x As Double
Dim r As Double, ti As Double
S = Firm_Value
x = Face_Value
r = Rate
V = Volatility_value
ti = Time
t1 = Log(S / x) + (r + V ^ 2 / 2) * ti
t2 = V * Sqr(ti)
t3 = Log(S / x) + (r - V ^ 2 / 2) * ti
d2 = t3 / t2
DefProb = WorksheetFunction.NormSDist(-d2)
End Function
您可以尝试添加 .Value
:If Cells(i, 2).Value > 0 And Cells(i, 3).Value > 0 Then
但真正有效的选项是 Application.Calculation = xlCalculationManual
。此行停止自动重新计算(使其手动)。
要再次启用它,请使用 Application.Calculation = xlCalculationAutomatic
我有这个 Sub,它在一个循环中执行 2 个基本计算,然后我有一个 UDF(见下文)。
我的问题: 当我 运行 Sub BSM_Table
它调用此行的函数:If Cells(i, 2) > 0 And Cells(i, 3) > 0 Then
和子停止。
我猜这是因为这些单元格:Cells(i, 2)
和 Cells(i, 3)
被函数使用,所以只要这些单元格发生变化,函数就会自动重新计算。
有没有办法在我需要的时候只调用函数?
Sub BSM_Table()
Dim i As Long
Dim LastRow As Long, LastRow2 As Long
Dim Firm_Value As Double, Face_Value As Double, Rate As Double, Volatility_value As Double, Time As Double
With Sheets("BlackScholeMerton")
LastRow = Sheets("BlackScholeMerton").Range("B42:B" & Rows.Count).End(xlDown).Row
For i = 42 To LastRow
' If Cells(i, 2) > 0 And Cells(i, 3) > 0 Then
Range("E" & i).Value = Range("B" & i).Value + Range("C" & i).Value
x = Cells(i, 4) * Cells(i, 2)
y = Cells(i, 5)
Cells(i, 6) = x / y
' End If
Next i
End sub
我的函数:
Function DefProb(Firm_Value As Double, Face_Value As Double, Rate As Double, Volatility_value As Double, Time As Double) As Double
Dim t1 As Double, t2 As Double, t3 As Double
Dim d2 As Double, ta As Double, tb As Double
Dim V As Double, S As Double, x As Double
Dim r As Double, ti As Double
S = Firm_Value
x = Face_Value
r = Rate
V = Volatility_value
ti = Time
t1 = Log(S / x) + (r + V ^ 2 / 2) * ti
t2 = V * Sqr(ti)
t3 = Log(S / x) + (r - V ^ 2 / 2) * ti
d2 = t3 / t2
DefProb = WorksheetFunction.NormSDist(-d2)
End Function
您可以尝试添加 .Value
:If Cells(i, 2).Value > 0 And Cells(i, 3).Value > 0 Then
但真正有效的选项是 Application.Calculation = xlCalculationManual
。此行停止自动重新计算(使其手动)。
要再次启用它,请使用 Application.Calculation = xlCalculationAutomatic