[英]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.