请参阅工作表中框架中的控件

时间:2017-06-03 06:56:50

标签: excel excel-vba vba

我有Class Module,我用它来处理Click事件。我使用Macro3()将事件与框架T1上的按钮相关联,并且它可以正常工作。

我有数百个框架(ActiveX控件),其名称为T1,T2,T3...

如何引用每个帧中的控件?我试过了ActiveSheet.Shapes("T1").Controls("title_pic_tog")。它不起作用。如果我们能够完成这项工作,那么我可以使用变量来替换Shapes("t1")

Option Explicit
Public WithEvents cBox As MSForms.ToggleButton

Private Sub cBox_Click()
    msgbox("clicked")
    'End If
End Sub

Sub Macro3()
    Set title_pic_tob_Event_Coll = New Collection

    Set title_pic_tob_Event = New titlepictog
    Set title_pic_tob_Event.cBox = ActiveSheet.t1.Controls("title_pic_tog")
    title_pic_tob_Event_Coll.Add title_pic_tob_Event
End sub

1 个答案:

答案 0 :(得分:1)

在课程模块中(说Class1

Option Explicit

Public WithEvents cBox As MSForms.ToggleButton

Private Sub cBox_Click()
    MsgBox ("clicked")
End Sub

在模块中

Dim TBArray() As New Class1

Private Sub Sample()
    Dim i As Integer, FrmBCtl As OLEObject, TBCtl As Variant
    Dim ws As Worksheet

    '~~> Change this to the relevant worksheet
    Set ws = Sheet2

    '~~> Loop through all objects in the worksheet
    For Each FrmBCtl In ws.OLEObjects
        '~~> Check if the oleobject is a frame
        If TypeName(FrmBCtl.Object) = "Frame" Then
            '~~> Loop through all controls in the frame
            For Each TBCtl In FrmBCtl.Object.Controls
                i = i + 1
                ReDim Preserve TBArray(1 To i)
                Set TBArray(i).cBox = TBCtl
            Next TBCtl
        End If
    Next FrmBCtl

    Set FrmBCtl = Nothing
    Set TBCtl = Nothing
End Sub

<强>截图 enter image description here