Refer To Controls in a Frame in Worksheet

寵の児 提交于 2020-06-28 03:03:31

问题


I have the Class Module, and I use it to handle Click event. I use Macro3() to link events with button on the Frame T1 and it works.

I have hundred of Frames (ActiveX Control) with names like T1,T2,T3....

How do I reference the controls in each frame? I tried ActiveSheet.Shapes("T1").Controls("title_pic_tog"). It doesn't work. If we can make this work then I can use a variable to replace 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:


In the Class Module (Say Class1)

Option Explicit

Public WithEvents cBox As MSForms.ToggleButton

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

In a Module

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

Screenshot



来源:https://stackoverflow.com/questions/44341254/refer-to-controls-in-a-frame-in-worksheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!