【问题标题】:VBA Excel - How to Access Control within Frame within Worksheet? Refactor / OptimizeVBA Excel - 如何访问工作表内框架内的控件?重构/优化
【发布时间】:2016-03-11 18:47:16
【问题描述】:

使用 Excel 2010

我有一个包含三个 ActiveX 框架的工作表。这些框架中的每一个都包含两个或更多选项按钮。工作表上的重置按钮将选项按钮的值重置为“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

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

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

【问题讨论】:

  • 录制一个宏,选择其中一个帧并改变一些东西,然后查看宏源。如果可以在代码中完成,您将看到如何在该代码中选择一个框架。

标签: vba excel activex


【解决方案1】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    • 2013-08-24
    • 2018-03-31
    • 1970-01-01
    • 2016-10-24
    • 2017-05-17
    相关资源
    最近更新 更多