2016-04-08 153 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