将切换按钮分配给类模块时出现问题

时间:2018-06-04 12:50:49

标签: vba excel-vba class excel

有人可以帮我解决我在这里做错的事吗?控件没有附加到类!

我的课程模块: CTglBtn

Option Explicit
Public WithEvents tgl1 As MSForms.ToggleButton
Private ac$

Public Property Get ACNumber() As String
    ACNumber = ac
End Property

Public Property Let ACNumber(value As String)
    ac = value
End Property

Private Sub tgl1_Click()
' do something here
End Sub

这里是我将表单的控件分配给类的地方。

Dim Ctgl As CTglBtn
Dim Coll As Collection

Private Sub UserForm_Initialize()
Dim aclist As Range

Set aclist = ThisWorkbook.Sheets("panel").Range("acnum")

    For i = 1 To 10
        Set Ctgl = New CTglBtn
        Set Ctgl.tgl1 = Me.Controls("TB" & i)
        Ctgl.ACNumber = aclist.Cells(i + 1, 1)
        Me.Controls("TB" & i).Caption = Ctgl.ACNumber
        Coll.Add Ctgl
        Set Ctgl = Nothing
    Next
'        MsgBox Coll.Count
    End Sub

1 个答案:

答案 0 :(得分:2)

Dim Coll As Collection需要是模块级声明,否则在例程结束后立即清除。

您还需要初始化该变量:

Set coll = New Collection