如何循环使用可变对象名称的用户窗体框架
How to loop through userform frames with variable object name
我试图在我的 VBA User_Form
的每个 Multipage
页面中循环一组特定的框架。但是,我似乎无法像对页面那样对每个框架控件使用可变对象名称。
我收到一个错误
object doesn't support this property or method
在下一行
For Each cCont in Me.MultiPage1.Pages(PageName).Frames(DataFrame).Controls
我的代码
Do While x <= Me.MultiPage1.Pages.Count
PageName = "Page" & CStr(x)
DataFrame = "DataFrame" & CStr(x)
For Each cCont In Me.MultiPage1.Pages(PageName).Frames(DataFrame).Controls
您实际上无法按照您认为的方式进行迭代。
首先,您需要遍历 MultiPage1
.
的所有页面
其次,遍历当前 Page
中的所有 Controls
,并检查它们是否属于 Frame
类型,如果是,则可以在 Frame
中迭代但是语法有点不同(见下面的代码)。
代码
Option Explicit
Private Sub IterateIn_MultiPage()
Dim x As Long, j As Long
Dim cCont As Control
For x = 0 To Me.MultiPage1.Pages.Count - 1 ' <-- loop through all MultiPage Pages
For Each cCont In Me.MultiPage1.Pages(x).Controls ' <-- loop through controls of current page
If TypeOf cCont Is Frame Then ' <-- check if control type is Frame
For j = 0 To cCont.Controls.Count - 1 '<-- loop through all items related to the current Frame collection
MsgBox cCont.Controls(j).Name '<-- display a message box
Next j
End If
Next cCont
Next x
End Sub
感谢@Shai 帮助整理代码,感谢其他人想知道最终代码是什么样子的。这将循环遍历每个多页中的每个帧,并将选中的复选框的标题粘贴到我想要的范围内。
Option Explicit
Sub IterateIn_MultiPage()
Dim x As Long, j As Long
Dim cCont As Control
Dim counter As Integer
Dim y As Integer
Dim Range As String
y = 1
For x = 0 To ImportData.MultiPage1.Pages.Count - 1
counter = 0
For Each cCont In ImportData.MultiPage1.Pages(x).Controls
Do While counter < 1
If TypeOf cCont Is Frame Then
For j = 0 To cCont.Controls.Count - 1
If cCont.Controls(j).Value = True Then
Range = "E" & y
If counter = 0 Then
Worksheets("Calculations").Range(Range) = cCont.Controls(j).Caption
counter = counter + 1
ElseIf counter = 1 Then
Worksheets("Calculations").Range(Range) = Worksheets("Calculations").Range(Range) & " & " & cCont.Controls(j).Caption
y = y + 1
End If
End If
Next j
End If
Loop
Next cCont
Next x
End Sub
我试图在我的 VBA User_Form
的每个 Multipage
页面中循环一组特定的框架。但是,我似乎无法像对页面那样对每个框架控件使用可变对象名称。
我收到一个错误
object doesn't support this property or method
在下一行
For Each cCont in Me.MultiPage1.Pages(PageName).Frames(DataFrame).Controls
我的代码
Do While x <= Me.MultiPage1.Pages.Count
PageName = "Page" & CStr(x)
DataFrame = "DataFrame" & CStr(x)
For Each cCont In Me.MultiPage1.Pages(PageName).Frames(DataFrame).Controls
您实际上无法按照您认为的方式进行迭代。
首先,您需要遍历 MultiPage1
.
其次,遍历当前 Page
中的所有 Controls
,并检查它们是否属于 Frame
类型,如果是,则可以在 Frame
中迭代但是语法有点不同(见下面的代码)。
代码
Option Explicit
Private Sub IterateIn_MultiPage()
Dim x As Long, j As Long
Dim cCont As Control
For x = 0 To Me.MultiPage1.Pages.Count - 1 ' <-- loop through all MultiPage Pages
For Each cCont In Me.MultiPage1.Pages(x).Controls ' <-- loop through controls of current page
If TypeOf cCont Is Frame Then ' <-- check if control type is Frame
For j = 0 To cCont.Controls.Count - 1 '<-- loop through all items related to the current Frame collection
MsgBox cCont.Controls(j).Name '<-- display a message box
Next j
End If
Next cCont
Next x
End Sub
感谢@Shai 帮助整理代码,感谢其他人想知道最终代码是什么样子的。这将循环遍历每个多页中的每个帧,并将选中的复选框的标题粘贴到我想要的范围内。
Option Explicit
Sub IterateIn_MultiPage()
Dim x As Long, j As Long
Dim cCont As Control
Dim counter As Integer
Dim y As Integer
Dim Range As String
y = 1
For x = 0 To ImportData.MultiPage1.Pages.Count - 1
counter = 0
For Each cCont In ImportData.MultiPage1.Pages(x).Controls
Do While counter < 1
If TypeOf cCont Is Frame Then
For j = 0 To cCont.Controls.Count - 1
If cCont.Controls(j).Value = True Then
Range = "E" & y
If counter = 0 Then
Worksheets("Calculations").Range(Range) = cCont.Controls(j).Caption
counter = counter + 1
ElseIf counter = 1 Then
Worksheets("Calculations").Range(Range) = Worksheets("Calculations").Range(Range) & " & " & cCont.Controls(j).Caption
y = y + 1
End If
End If
Next j
End If
Loop
Next cCont
Next x
End Sub