2016-04-08 125 views
1

不工作一個VBA函數,這是一個簡單的股票價格變動的代碼 我的代碼是函數(參數)運行按下按鈕在EXCELL

Function VanillaCall(S0 As Single, Exercise As Single, Mean As Single, sigma As Single, _ 
Interest As Single, Time As Single, Divisions As Integer, Runs As Integer) As Single 

deltat = Time/Divisions 
interestdelta = Exp(Interest * deltat) 
up = Exp(Mean * deltat + sigma * Sqr(deltat)) 
down = Exp(Mean * deltat - sigma * Sqr(deltat)) 
pathlength = Int(Time/deltat) 

piup = (interestdelta - down)/(up - down) 
pidown = 1 - piup 
Temp = 0 

For Index = 1 To Runs 
    upcounter = 0 
    For j = 1 To pathlength 
     If Rnd > pidown Then upcounter = upcounter + 1 
    Next j 
     callvalue = Application.Max(S0 * (up^upcounter) * (down^(pathlength - upcounter)) - Exercise, 0)/(interestdelta^pathlength) 
     Temp = Temp + callvalue 
Next Index 

VanillaCall = Temp/Runs 

End Function 

參數從細胞在Excel中通過。 我想從按鈕單擊執行此功能,並顯示單元格返回值說b12。 我已經嘗試把代碼裏面的按鈕子,但它不工作,呼籲vanillacall子內太不工作。 如..

private sub button1_click() 
call vanillacall 
end sub 
+0

當您在button1_click()中調用函數vanillacall時,必須傳遞所有必要的參數。 – Mrig

+0

我不認爲這會起作用,因爲您必須將'= VanillaCall()'放在單元格中並傳遞必要的參數。您可以通過從宏調用該函數來傳遞參數,但是您必須定義要在宏中輸出結果的位置。 – newguy

+0

但我的參數來自excell中的單元格,這些單元格在運行函數 – user2997767

回答

1
Private Sub button1_click() 
    Range("B12").Value = vanillacall(....) 
End Sub 

按照您的要求,傳遞函數參數的範圍如下圖所示。下面的代碼只是舉例(由於Excel數據的變化)

Sub testing33() 
    Range("B12") = sample(Range("A5"), Range("B5")) 
End Sub 

Function sample(a As Range, b As Range) 
    sample = a.Cells.Value & ", " & b.Cells.Value 
End Function 
+0

但我沒有傳遞參數那裏即時使用excell單元格數據作爲函數運行時選擇的參數 – user2997767

+0

使用該excel單元格數據作爲參數 –

+0

好吧,得到它運行。謝謝 但如果excel數據值不斷變化。 – user2997767

0

你需要得到從表中值並保存在變量。然後將這些變量傳遞給函數。然後將結果輸出到表格的某處。您將需要適當調整範圍地址和工作表名稱。

Private sub button1_click() 

    dim ws as worksheet 
    Set ws = worksheets("Sheet1") ' < change the sheet name as appropriate 

    dim S0 As Single 
    dim Exercise As Single 
    dim Mean As Single 
    dim sigma As Single 
    dim Interest As Single 
    dim Time As Single 
    dim Divisions As Integer 
    dim Runs As Integer As Single 

    S0 = ws.Range("B1") '< specify the cell that has this data 
    Exercise = ws.Range("B2") '< specify the cell that has this data 
    Mean = ws.Range("B3") '< specify the cell that has this data 
    sigma = ws.Range("B4") '< specify the cell that has this data 
    Interest = ws.Range("B5") '< specify the cell that has this data 
    Time = ws.Range("B6") '< specify the cell that has this data 
    Divisions = ws.Range("B7") '< specify the cell that has this data 
    Runs = ws.Range("B8") '< specify the cell that has this data 

    dim Result as Single 
    Result = vanillacall(S0, Exercise , Mean, sigma, Interest, Time, Divisions, Runs) 
    ws.Range("B10") = Result '<specify the cell where you want the result 

end sub 
1

我做類似的下面,這將允許我挑選含有我想傳遞給函數的數據的範圍內(只要所述範圍是連續的,並且包含8個細胞),並挑選細胞我想輸出結果。

Private Sub button1_click() 
Dim inRng As Range, outRng As Range 

inSelect: 
Set inRng = Application.InputBox("Select Range to Calculate", Type:=8) 
    If inRng.Cells.Count <> 8 Then 
     MsgBox "Select a range with 8 cells!", vbCritical 
     GoTo inSelect 
    End If 
outSelect: 
Set outRng = Application.InputBox("Select Cell to Output To", Type:=8) 
    If outRng.Cells.Count > 1 Then 
     MsgBox "Select only one cell!", vbCritical 
     GoTo outSelect 
    End If 

outRng.Value = VanillaCall(inRng.Cells(1), inRng.Cells(2), inRng.Cells(3), inRng.Cells(4), inRng.Cells(5), inRng.Cells(6), inRng.Cells(7), inRng.Cells(8)) 

End Sub