【问题标题】:Running a Vba function on button press not working in excell在按下按钮时运行 Vba 函数在 Excel 中不起作用
【发布时间】:2016-04-09 00:12:49
【问题描述】:

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

    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。 我尝试将代码放在按钮 sub 中,但它不起作用,sub 内部的调用 vanillacall 也不起作用。 喜欢..

private sub button1_click()
call vanillacall
end sub

【问题讨论】:

  • 当你在 button1_click() 中调用 vanillacall 函数时,你必须传递所有必要的参数。
  • 我认为这行不通,因为您必须将=VanillaCall() 放在一个单元格中并传递必要的参数。您可以通过从宏调用该函数来传递参数,但是您必须在该宏本身中定义要输出结果的位置。
  • 但我的论点来自在运行函数时选择的 Excel 中的一个单元格
  • @user2997767 是的,但是在宏中的任何地方,您都没有定义输出在电子表格中的位置。如果你使用private sub button1_click() call vanillacall Msgbox Vanillacall end sub你就能看到结果
  • 喜欢在 range("b12").vanillacall?不工作

标签: vba excel


【解决方案1】:
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

【讨论】:

  • 但我没有在那里传递参数,我使用 Excel 单元格数据作为函数运行时选择的参数
  • 使用该 excel 单元格数据作为参数
  • 好的,让它运行起来。谢谢,但如果 excel 数据值不断变化怎么办。
  • 我可以知道运行此程序时哪个单元格发生了变化
  • 数据(参数)来自可能会改变的 excel 表,
【解决方案2】:

我会做类似下面的事情,这将允许我选择包含我想要传递给函数的数据的范围(只要该范围是连续的并且包含 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

【讨论】:

    【解决方案3】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 2018-05-29
      • 1970-01-01
      • 1970-01-01
      • 2013-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多