2015-12-06 37 views
1

我有這個小組在循環中做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 

回答

4

你可以嘗試添加.ValueIf Cells(i, 2).Value > 0 And Cells(i, 3).Value > 0 Then

但選項,真正的工作是Application.Calculation = xlCalculationManual。該行停止自動重新計算(使其手動)。

要再次使用它,請使用Application.Calculation = xlCalculationAutomatic

+0

'Application.Calculation'有效。我沒有考慮這樣做。感謝您的快速回答 – manu