Excel VBA组合框标识

Excel VBA组合框标识,vba,excel,combobox,Vba,Excel,Combobox,我在用户表单上有4个以上的组合框。当他们开火时,他们发射相同的事件。我想做的是找出触发事件的组合框。组合框的创建取决于组件的数量。生成组合框的代码如下所示: 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

我在用户表单上有4个以上的组合框。当他们开火时,他们发射相同的事件。我想做的是找出触发事件的组合框。组合框的创建取决于组件的数量。生成组合框的代码如下所示:

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
):

上面的这段代码是DougGlancy精心编写的,以使事件与代码生成的组合框一起工作


如何获取触发事件的组合框?i、 e.名称或其他形式的标识。

是否可以再次引用btn.Combobox?与您最初将组合框指定给按钮的方式类似,但随后相反:

set combobox = btn.Combobox 

在类中,
。Name
不会以
MSForms的形式出现在组合框的intellisense列表中。组合框本身实际上没有Name属性(请在F2对象浏览器中查看),而该属性由
控件
基类提供:

Private Sub combobox_Click()

    MsgBox combobox.Value
    MsgBox combobox.Name '// no hint but still works

    '//cast to a Control to get the formal control interface with .Name
    Dim ctrl As Control: Set ctrl = combobox
    MsgBox ctrl.Name

End Sub

在搜索了500多个网页后,我终于回答了自己的问题(花了很长时间)

这就是我使用的,当单击某些组合框时,它会工作并启动:

Private Sub combobox_Click()
MsgBox combobox.Value
If combobox = UserForm1.Controls("Component0") Then
    MsgBox "Success1"
End If
If combobox = UserForm1.Controls("Component1") Then
    MsgBox "Success2"
End If
End Sub

希望这可以用于其他需要它的人。

您为什么不在自定义类中添加属性,并在集合中注册时设置该属性

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
'*******EDIT********
    with cButton
        .combobox = combo
        .Indx = j
    end With    'cButton
'*******************
    coll.Add cButton
End With
Height = Height + 30
Next j
类模块

Public WithEvents btn As MSForms.CommandButton
Dim WithEvents mCombobox As MSForms.comboBox
Private combolist() As String

'*******EDIT********
Public Indx As Long

Property Let comboBox(cb As MSForms.comboBox)
    Set mCombobox = cb
End Property
'*******************

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 mCombobox_Click()

'*******EDIT********
    MsgBox "Combobox " & Indx & Chr(9) & mComboBox.Value
'*******************

End Sub
因为您需要事件的多对一映射,我假设您在实际代码中有一个公共回调,所以您也可以这样做

在标准模块中

Public Sub cbCallBack(ocb As clsButton)
    MsgBox ocb.Indx
End Sub
在clsButton中(替换事件处理程序)


我不知道你是从哪里来的。但是当我做combobox.where时,没有名字或任何类型的id选项,所以我不知道怎么做。我看过100多个网站,它们都告诉我如何添加和设置值,但没有告诉我如何找出哪个框触发了事件啊,你的原始帖子没有说你想要(名称),只是你想要“获得组合框”,因此我认为你想知道对象,而不是你想要对象的名称。。。感谢你的澄清谢谢你的努力,经过数小时的搜索,我也找到了一个解决方案,现在我将两者结合起来。谢谢你的帮助!
Public Sub cbCallBack(ocb As clsButton)
    MsgBox ocb.Indx
End Sub
Private Sub mCombobox_Click()
    cbCallBack Me
End Sub