首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >循环访问分组框中的每个控件

循环访问分组框中的每个控件
EN

Stack Overflow用户
提问于 2018-10-23 05:35:32
回答 1查看 588关注 0票数 1

我在excel表格中有一个对象列表,每个对象都有一个复选框和4个代表该对象的组框中的下拉列表。

我可以使用VBA遍历工作表中的每个分组框,但如何遍历分组框中的每个控件呢?

代码语言:javascript
复制
Dim oGroupBox As GroupBox
Dim cntrl As Control
For Each oGroupBox In Worksheets("Grapher").GroupBoxes
    For Each cntrl In oGroupBox.Controls
        Debug.Print (cntrl.Name)
    Next cntrl
Next oGroupBox
EN

回答 1

Stack Overflow用户

发布于 2018-10-23 10:06:46

下面假设您没有对FormControl形状进行分组。它将列出TopLeftCell位于GroupBox范围(从TopLeftCell到BottomRightCell)内的所有窗体控件的名称。

您可以添加新的Sub代码,以便对所使用的每种类型的表单控件执行不同的操作。

代码语言:javascript
复制
Option Explicit

Sub ListControlsInGroupBoxes()
    Dim oGroupBox As GroupBox
    For Each oGroupBox In ThisWorkbook.ActiveSheet.GroupBoxes
        ListObjectsInGroupBox oGroupBox
    Next oGroupBox
End Sub

Private Sub ListObjectsInGroupBox(GBox As GroupBox)
    Dim oBoxRange As Range, oShp As Shape
    Set oBoxRange = Range(GBox.TopLeftCell, GBox.BottomRightCell)
    Debug.Print String(50, "-")
    Debug.Print "Group Box """ & GBox.Name & """ has range " & oBoxRange.Address
    For Each oShp In GBox.Parent.Shapes
        ' Deal only with FormControls
        If oShp.Type = msoFormControl Then
            ' Display FormControl's name if inside the GroupBox range
            If Not Intersect(oShp.TopLeftCell, oBoxRange) Is Nothing Then
                ' Obmit itself
                If Not oShp Is GBox Then
                    Debug.Print """" & oShp.Name & """"
                End If
            End If
        End If
    Next oShp
    Set oBoxRange = Nothing
End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52938084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档