繁体   English   中英

VBA运行宏,带有命令按钮中的参数,请在用户窗体中单击

[英]VBA Run macro with arguments from Command Button click in UserForm

我在与模块RunOB相同的VBA项目中有一个UserForm DataInput 这些位于Excel Project Benchmarking.xlsx中 我在DataInput窗体上有一个命令按钮,希望在其中按下模块RunOB即可运行。 另外,我正在将String参数从表单传递到模块。

'Header of 'Run OB' Module
Sub OrganizeBenchmarks(bidNum As String, name As String, _
                       state As String, year As String, category As String)

按钮的单击方法为:

Private Sub CommandButton1_Click()
    '...'
End Sub

如果可能的话,如果我可以设置一个键盘快捷方式来打开DataInput UserForm(在Excel项目中),那也会有所帮助。

可以帮助您开发自己的代码的东西.........

'Public enum type to add a set of particular vbKeys to the standard key set
Public Enum typePressKeys
    vbNoKey = 0
    vbExitTrigger = -1
    vbAnswerKey = 100
    vbLaunchKey = 102
    vbPrevious = 104
    vbNext = 106
    vbSpecialAccessKey = 108
End Enum


Public Sub OrganizeBenchmarks()
Dim stopLoop As Boolean, listOfYears As Variant, bidNum As String, name As String, state As String, year As String, category As String

'For example
listOfYears = Array(2017, 2018, 2019, 2020, 2021, 2022)

stopLoop = False
Load DataInputForm 'Assuming this is the name of your UserForm
With DataInputForm 'Assuming that this name would be an input control in your UserForm
    .yearCB.List = listOfYears 'comboboxList control
    .yearCB.ListIndex = 2
    .Tag = vbNoKey
End With
DataInputForm.Show
Do
    If DataInputForm.Tag = vbOK Then
        'I have assumed you would use these names for input controls on your UserForm
        bidNum = DataInputForm.bidNum_TextBox.Value
        name = DataInputForm.name_TextBox.Value
        state = DataInputForm.state_TextBox.Value
        year = CStr(DataInputForm.yearCB.Value)
        category = DataInputForm.category_TextBox.Value
        Unload DataInputForm
        stopLoop = True
    ElseIf DataInputForm.Tag = vbCancel Then
        Unload DataInputForm
        Exit Sub
    Else
        stopLoop = False
    End If
Loop Until stopLoop = True

'Place additional code here to take whatever actions 

End Sub

看起来很简单?

Private Sub CommandButton1_Click()
    OrganizeBenchmarks "your", "arguments", "would", "go", "here"
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM