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

时间:2016-03-11 18:47:17

标签: excel vba excel-vba activex

使用Excel 2010

我有一个包含三个ActiveX框架的工作表。这些帧中的每一个都包含两个或更多OptionButtons。工作表上的重置按钮会将选项按钮的值重置为“错误”。

现在,我可以为每个帧使用单独的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

For Each xFrame in (ActiveSheet.Frames? .Shapes? .OLEObjects?) For Each xControl in xFrame xControl.Value = False Next Next

经过在线和书籍的广泛搜索后,我无法找到正确的方法来访问活动工作表中的每个ActiveX框架。

1 个答案:

答案 0 :(得分:0)

试试这个:

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