excel - 引用工作表中框架中的控件

标签 excel vba

我有类模块,我用它来处理Click 事件。我使用 Macro3() 将事件与 Frame 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

最佳答案

在类模块中(比如 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

关于excel - 引用工作表中框架中的控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44341254/

相关文章:

vba - 使用 VBA 宏将每个 Excel 工作表另存为单独的工作簿

java - 在 selenium 脚本中从 Excel 获取数据时收到警告

电子表格 |索引匹配 |

excel - 循环遍历 ListObject 中的行来删除它们非常慢

excel - 在 vlookup 表数组中使用 OR

excel - 使用 github 存储 Excel 文件

excel - 使用 VBA 删除 Excel 表格行

excel - VBA Excel 形状

vba - 如何使用宏删除 Outlook 2003 中选定邮件的特定类别?

vba - 运行 for 循环时出现奇怪的运行时错误 '424'