Excel VBA:如何为动态创建的对象调用“ on change”?

时间:2018-09-05 15:53:31

标签: excel vba excel-vba

我正在尝试为用户窗体上的任何组合框调用更改事件。

更新

因此,按照其他人所说的(对于文本框,我已经对其进行了测试并适用于文本框),我创建了一个名为“ clsCombo_Update”的类模块

Private WithEvents MyComboBox As MSForms.comboBox

Public Property Set Control(cb As MSForms.comboBox)
    Set MyComboBox = cb
End Property

Private Sub MyComboBox_Change()
    Debug.Print "Change"
End Sub

在UserForm中,我有:

Private Sub UserForm_Initialize()

    Dim ctrl As MSForms.Control
    Dim obj As clsCombo_Update

    Set cbCollection = New Collection
        For Each ctrl In Me.Controls
            If TypeOf ctrl Is MSForms.comboBox Then
                Set obj = New clsCombo_Update
                Set obj.Control = ctrl
                cbCollection.Add obj
            End If
        Next ctrl
    Set obj = Nothing

End Sub

此设置适用于文本框,但当我尝试使其适用于组合框时无效

0 个答案:

没有答案