VBA:循环通过Me.Multipage1.Pages(i)

时间:2017-03-18 22:55:15

标签: vba

我需要通过隐藏页面来实现循环。我有10个隐藏页面,每页都有一堆控件。

我需要的是确定下一个页面符合以下内容:

Dim i as Integer 

For Each i In Me.MultiPage1.Pages(i)

    If Me.MultiPage1.Pages.Visible = False Then

        'DO STUFF HERE

    End If

 Next i

我希望这可以解释。这很简单,但我找不到任何用于检查页面的循环文档。

由于

2 个答案:

答案 0 :(得分:0)

试试这个:

For Each pg In Me.MultiPage1.Pages
If pg.Visible = False Then

'Do something

End If
Next pg

编辑关于第二个问题:

  

“从上面开始:.Visible = True,我会添加什么来查找最后一个.Visible = True并激活它旁边的页面。例如,如果它在Pages(4)上停止然后添加下一页(5) )“

添加变量以记住最后一个可见页面,如下所示:

Dim lastvisible As Integer
For Each pg In Me.MultiPage1.Pages
If pg.Visible = False Then
'set the varible to this page index
lastvisible = pg.index
'Do something

End If
Next pg

当循环结束时,变量“lastvisible”将包含最后一个可见页面的索引,您现在可以使用它来设置您想要的关于该页面或以下任何内容的任何内容 示例(将最后一个可见页面旁边的页面设置为可见):

Me.MultiPage1.Pages(lastvisible + 1).Visible = True

答案 1 :(得分:0)

如果您希望保留自己的编码风格版本,并在Pages控件中从0开始循环到MultiPage1个数,则需要获取Pages的数量使用命令:Me.MultiPage1.Pages.Count

<强>代码

Dim i       As Long

For i = 0 To Me.MultiPage1.Pages.Count - 1
    If Me.MultiPage1.Pages(i).Visible = False Then
        MsgBox "Page index " & i & " is hidden" ' <-- message box just for testing
        'DO STUFF HERE

    End If
Next i

编辑1

支持PO的新澄清:

Dim i   As Long, j  As Long
Dim LastVisPg       As Long

For i = Me.MultiPage1.Pages.Count - 1 To 0 Step -1
    Debug.Print MultiPage1.Pages(i).Caption
    If Not Me.MultiPage1.Pages(i).Visible Then
        For j = i - 1 To 0 Step -1
            If Me.MultiPage1.Pages(j).Visible Then
                LastVisPg = j
                GoTo LastVisPageFound
            End If
        Next j
    End If
Next i

LastVisPageFound:    
MsgBox "Last Visible Page index is  " & LastVisPg & ", Next unvisible page index is " & LastVisPg + 1

'=== OPTIONAL === : Set the next Page to Visible, and set the focus to it when the focus to this page
Me.MultiPage1.Pages(LastVisPg + 1).Visible = True ' unhide the next unvisible Page
Me.MultiPage1.Value = LastVisPg + 1 ' set focus to the next unvisible Page when User_Form loads