在按钮上运行Vba功能在Excel中不起作用

在按钮上运行Vba功能在Excel中不起作用,vba,excel,Vba,Excel,这是一个简单的股票价格变化代码 我的代码是函数(带参数) 参数是从excel中的单元格传递的。 我想通过点击按钮来执行这个功能,并在单元格中显示返回值,比如b12。 我试着把代码放在一个按钮子里面,但它不起作用,一个叫vanillacall的按钮子里面也不起作用。 像 根据您的请求,在如下范围内传递参数。下面的代码只是示例(由于excel数据中的更改) 您需要从工作表中获取值并保存在变量中。然后将变量传递给函数。然后将结果输出到工作表的某个位置。您需要根据需要调整范围地址和工作表名称 Priva

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

参数是从excel中的单元格传递的。 我想通过点击按钮来执行这个功能,并在单元格中显示返回值,比如b12。 我试着把代码放在一个按钮子里面,但它不起作用,一个叫vanillacall的按钮子里面也不起作用。 像

根据您的请求,在如下范围内传递参数。下面的代码只是示例(由于excel数据中的更改)


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

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
Private子按钮1\u click()
将ws设置为工作表
设置ws=工作表(“Sheet1”)<根据需要更改工作表名称
单色调暗S0
像单身一样做运动
我的意思是单身
作为单一的dim-sigma
兴趣淡薄
黯淡的单身时光
将除法调整为整数
dim以整数和单精度运行
S0=ws.Range(“B1”)<指定包含此数据的单元格
Exercise=ws.Range(“B2”)<指定包含此数据的单元格
Mean=ws.Range(“B3”)“<指定包含此数据的单元格
sigma=ws.Range(“B4”)“<指定包含此数据的单元格
Interest=ws.Range(“B5”)“<指定包含此数据的单元格
Time=ws.Range(“B6”)“<指定包含此数据的单元格
Divisions=ws.Range(“B7”)“<指定包含此数据的单元格
Runs=ws.Range(“B8”)“<指定包含此数据的单元格
暗淡的结果是单一的
结果=vanillacall(S0、练习、平均值、西格玛、兴趣、时间、分段、跑步)

ws.Range(“B10”)=Result'我会做如下操作,这将允许我选择包含要传递给函数的数据的范围(只要该范围是连续的并且包含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
Private子按钮1\u click()
变暗输入范围,输出范围
插入选择:
设置inRng=Application.InputBox(“选择要计算的范围”,类型:=8)
如果inRng.Cells.Count 8,则
MsgBox“选择包含8个单元格的区域!”,vbCritical
转到插入选择
如果结束
胜出:
Set-outRng=Application.InputBox(“选择要输出到的单元格”,类型:=8)
如果outRng.Cells.Count>1,则
MsgBox“仅选择一个单元格!”,vbCritical
后藤胜出
如果结束
outRng.Value=VanillaCall(卢比单元格(1)、卢比单元格(2)、卢比单元格(3)、卢比单元格(4)、卢比单元格(5)、卢比单元格(6)、卢比单元格(7)、卢比单元格(8))
端接头

当您在button1\u click()中调用函数vanillacall时,您必须传递所有必要的参数。我认为这行不通,因为您必须将
=vanillacall()
放入单元格中并传递必要的参数。您可以通过从宏调用该函数来传递参数,但随后必须定义要在该宏本身中输出结果的位置。但我的参数来自Excel中运行时选中的单元格function@user2997767是的,但是在宏中的任何地方,您都没有定义输出应该放在电子表格上的什么位置。如果使用
private sub按钮1\u click()call vanillacall Msgbox vanillacall end sub
您将能够在范围(“b12”)中看到类似结果的内容。vanillacall?不工作但我不传递参数在那里我使用excel单元格数据作为参数,这些参数是在函数运行时选择的使用excel单元格数据作为参数好的,让它运行。谢谢,但是如果excel数据值不断变化会怎么样。我可以知道在运行此数据(参数)时哪个单元格在变化吗,
Private Sub button1_click()
    Range("B12").Value = vanillacall(....)
End Sub
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
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
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