循环遍历用户窗体中每个框架中的控件

Looping through controls in each frame in a userform

我有一个包含多个框架的用户表单,每个框架内都有多个选项按钮。我想遍历每一帧中的每个选项按钮。

我当前的代码是这样工作的,但在这里我硬编码了 Me.f_FileType.Controls 并且我想在我的用户表单中为每一帧 frm 动态更改它。

Private Sub cmdContinue_Click()

Dim ctl As msforms.control
Dim frm As msforms.control
Dim obArr() As Variant
Dim n As Integer: n = 1

For Each frm In Me.Controls
If TypeName(frm) = "Frame" Then

    ' Counting number of specific control type
    For Each ctl In Me.f_FileType.Controls
        If TypeName(ctl) = "OptionButton" Then
            n = n + 1
        End If
    Next ctl

    ' Redimensioning array to match
    ReDim obArr(1 To n, 1 To 2)
    n = 1

    ' Populating array
    Debug.Print "Option buttons in " & frm.Caption & ":"
    For Each ctl In Me.f_FileType.Controls
        If TypeName(ctl) = "OptionButton" Then
            obArr(n, 1) = ctl.TabIndex
            obArr(n, 2) = ctl.Caption
            Debug.Print "Option button '" & obArr(n, 2) & "' with the tabindex value " & obArr(n, 1)
        End If
    Next ctl

End If
Next frm

End Sub

您已经在循环查找行中的帧:

If TypeName(frm) = "Frame" Then

您只需更改行:

For Each ctl In Me.f_FileType.Controls

至:

For Each ctl In frm.Controls