组合框数组上的VBA单击事件

时间:2017-09-29 15:52:26

标签: excel vba excel-vba

嘿,我有以下VBA代码,允许我在动态创建的组合框和文本框上点击事件到用户窗体上。

用户窗体:

Option Explicit

Dim comboboxBoxColct  As New Collection

Private Sub UserForm_Activate()
Dim comboboxEvent           As Class1
Dim controller(100)         As Object

   '..lots more code within here
   With controller(i)
      .Name = "cmdBOM" & i
      .Top = 10
      .ForeColor = vbBlack
      .Height = 15
      .FontName = "Times New Roman"
      .FontSize = 6
      .AutoSize = False
      .Caption = "Add New Row"
      .BackColor = RGB(204, 255, 201)
      .TabStop = True
      .TabIndex = i
   End With

   Set comboboxEvent = New Class1
   Set comboboxEvent.comboboxBox = controller(i)
   comboboxBoxColct.Add comboboxEvent
End Sub

的Class1:

Option Explicit

Public WithEvents comboboxBox As MSForms.ComboBox

Sub SetCombobox(ctl As MSForms.ComboBox)
    Set comboboxBox = ctl
End Sub

Private Sub comboboxBox_Click()
    If comboboxBox.ListIndex > -1 Then
        MsgBox "You clicked on " & comboboxBox.Name & vbLf & _
               "The value is " & comboboxBox.Value
    End If
End Sub

以上代码可以正常使用。但是,一旦我点击组合框并选择了某些内容,msgbox会出现:

  

您点击了

     

值为

在查看 _Click()事件时, .Name .Value 没有值,我不确定为什么呢?我在设置comboboxBox = ctl 上设置了一个断点,它似乎随时出现""

enter image description here

我可以做些什么才能让它发挥作用?

0 个答案:

没有答案