我在用戶窗體上有4個以上的組合框。當他們射擊時,他們射擊同一事件。我想要做的是找出哪個ComboBox觸發了事件。組合框的創建取決於組件的數量。代碼生成的組合框如下所示:Excel VBA組合框標識
For j = 0 To UBound(ComponentList) - 1
'Set Label
num = j + 1
Set control = UserForm1.Controls.Add("Forms.Label.1", "ComponentLabel" & CStr(num) & ":", True)
With control
.Caption = "Component " & CStr(num)
.Left = 30
.Top = Height
.Height = 20
.Width = 100
.Visible = True
End With
'set ComboBox
Set combo = UserForm1.Controls.Add("Forms.ComboBox.1", "Component" & num & ":", True)
With combo
.List = ComponentList()
.Left = 150
.Top = Height
.Height = 20
.Width = 50
.Visible = True
Set cButton = New clsButton
Set cButton.combobox = combo
coll.Add cButton
End With
Height = Height + 30
Next j
這工作得很好,我可以讓用戶選擇的價值,但我無法找到已使用該組合框。下面這段代碼是它火災(clsButton
)事件:
Public WithEvents btn As MSForms.CommandButton
Public WithEvents combobox As MSForms.combobox
Private combolist() As String
Private Sub btn_Click()
If btn.Caption = "Cancel" Then
MsgBox "Cancel"
Unload UserForm1
Variables.ComponentSelectionError = False
ElseIf btn.Caption = "Enter" Then
MsgBox "enter"
Unload UserForm1
Variables.ComponentSelectionError = True
End If
End Sub
Private Sub combobox_Click()
MsgBox combobox.Value
End Sub
這段代碼上面是好心的工作由道格Glancy得到的代碼工作的事件產生的組合框。
如何獲取觸發事件的組合框?即名稱或某種其他形式的標識。
感謝這個工作,我也找到了一個解決方案,經過幾個小時的搜索,我現在把它們結合起來。謝謝你的幫助! – NoLiver92 2013-03-22 11:45:34