gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 19:49:53 24 4
gpt4 key购买 nike

我有类模块,我用它来处理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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com