如何让Sub Auto_Open()在类模块

时间:2016-12-30 15:48:02

标签: excel vba excel-vba macros

我有一个带有VBA宏代码的数据库,可以让我的多个切换按钮执行相同的功能。但是,每当我或我的同事打开Excel电子表格时,用户必须从VBA窗口手动运行宏。如何在打开excel文件的时候让我的函数执行?

在我的课程模块(名为clsToggle)中,我有:

Option Explicit
Public WithEvents TGButton As ToggleButton
Private Sub TGButton_Click()
    With TGButton
        If .Caption = "G" Then
            .Caption = "Y"
        Else
            .Caption = "G"
        End If
        If .Value = True Then
            .BackColor = vbGreen
        Else
            .BackColor = vbYellow
        End If
    End With
End Sub

在常规模块中,我有以下内容:

Option Explicit
Dim TBTs() As New clsToggle
Sub Init_ToggleButtons()
Dim obj As OLEObject
Dim TGCount As Integer
    TGCount = 0
    For Each obj In ActiveSheet.OLEObjects
        If TypeOf obj.Object Is ToggleButton Then
            TGCount = TGCount + 1
            ReDim Preserve TBTs(1 To TGCount)
            Set TBTs(TGCount).TGButton = obj.Object
        End If
    Next
End Sub

0 个答案:

没有答案