VBA Excel – 如何在工作表内的框架内访问控制? 重构/优化

使用Excel 2010

我有一个包含三个ActiveX框架的工作表。 每个框架包含两个或更多的选项button。 工作表上的重置button将选项button的值重置为“False”。

现在,我可以使用每个帧的单独For循环来重置它们:

Private Sub CommandButtonReset_Click() 'This button resets all the OptionButtons to False (unchecked) Dim x As Control For Each x In Frame1.Controls x.Value = False Next For Each x In Frame2.Controls x.Value = False Next For Each x In Frame3.Controls x.Value = False Next End Sub 

…但我想重置他们全部使用一个嵌套的For循环,像这样:

 Dim xControl as control Dim xFrame as Frame 

Dim xControl as control Dim xFrame as Frame

(ActiveSheet.Frames?.Shapes?.OLEObjects?)中的每个xFrame
对于xFrame中的每个xControl
xControl.Value = False
下一个
下一个

在大量的在线search和书籍search之后,我无法find访问活动工作表中每个ActiveX框架的正确方法。

尝试这个:

 Sub Tester() Dim o As OLEObject, c For Each o In Sheet1.OLEObjects 'is this a Frame? If TypeName(o.Object) = "Frame" Then For Each c In o.Object.Controls 'is this a checkbox? If TypeName(c) = "CheckBox" Then c.Value = False End If Next End If Next o End Sub