在button上运行一个VBA函数不能在excell中工作

这是一个简单的股票价格变化代码我的代码是function(带参数)

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中的单元格传递。 我想从button单击执行此function,并显示在单元格中返回值说b12。 我已经尝试把代码里面的button子,但它不工作,呼吁香草里面子也不是工作。 喜欢..

 private sub button1_click() call vanillacall end sub 

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

根据你的请求,在下面的Range中传递参数。 下面的代码只是例子(由于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 

我会做下面这样的事情,让我select包含我想传递给函数的数据的范围(只要范围是连续的,包含8个单元格),并select我想输出结果的单元格。

 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 

您需要从工作表中获取值并保存在variables中。 然后将这些variables传递给函数。 然后将结果输出到表格的某处。 您将需要适当调整范围地址和工作表名称。

 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