【问题标题】:Excel VBA ComboBox IdentificationExcel VBA 组合框标识
【发布时间】:2013-03-12 04:05:52
【问题描述】:

我在一个用户表单上有 4 个以上的组合框。当它们触发时,它们触发相同的事件。我想做的是找出哪个 ComboBox 触发了事件。组合框的创建取决于有多少组件。生成 ComboBox 的代码如下所示:

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

这很好用,我可以得到用户选择的值,但是我找不到使用了哪个 ComboBox。下面的代码是它触发的事件 (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

上面这段代码由 Doug Glancy 精心编写,以使事件与代码生成的 ComboBox 一起工作。

如何获取触发事件的 ComboBox?即姓名或其他形式的身份证明。

【问题讨论】:

    标签: vba excel combobox


    【解决方案1】:

    在搜索了 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
    

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

    【讨论】:

      【解决方案2】:

      .Name 类中不会出现在组合框的智能感知列表中,因为MSForms.ComboBox 本身实际上并没有 name 属性(在 F2 对象浏览器中查看它),而是该属性由Control 基类:

      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
      

      【讨论】:

      • 谢谢这已经奏效了,经过几个小时的搜索,我也找到了一个解决方案,我现在将它们结合起来。感谢您的帮助!
      【解决方案3】:

      也许再次引用 btn.Combobox?类似于您首先将组合框分配给按钮的方式,但随后反过来:

      set combobox = btn.Combobox 
      

      【讨论】:

      • 我不确定你是从哪里来的。但是当我做 combobox.whatever 时,没有名称或任何类型的 id 选项,所以我不知道该怎么做。我查看了 100 多个网站,它们都告诉我如何添加和设置值,但没有告诉我如何找出哪个框触发了事件
      • 啊,您的原始帖子没有说您想要(名称),只是您想要“获取组合框”,因此我认为您想知道对象,而不是您想要名称对象...感谢您的澄清
      【解决方案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
      '*******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 中(替换事件处理程序)

      Private Sub mCombobox_Click()
          cbCallBack Me
      End Sub
      

      【讨论】:

        猜你喜欢
        • 2019-06-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多