user3803946
user3803946

Reputation: 11

VBA: looping through Me.Multipage1.Pages(i)

I need to acheive looping through hidden pages. I have 10 hidden pages with stacks of controls on each page.

What I need is to identify which page is next in line with something like:

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

I hope this explains. It is pretty simple, however I cannot find any looping documentation for checking Page(s).

Thanks

Upvotes: 1

Views: 2228

Answers (2)

Try this:

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

'Do something

End If
Next pg

Edit About the second question:

"from the above: .Visible = True, what would I add to find the last .Visible = True and activate the page next to it. For example if it stops on Pages(4) Then add the next page (5)"

Add a variable to remember last visible page, like this:

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

When the loop finishes the variable "lastvisible" will contain the index of the last visible page, you can now use it to set whatever you want about that page or any of the following Example (setting the page next to the last visible page to visible):

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

Upvotes: 0

Shai Rado
Shai Rado

Reputation: 33672

If you want to keep your style of coding version, and to loop from 0 until number of Pages you have in your MultiPage1 control, you need to get the number of Pages with the command: Me.MultiPage1.Pages.Count.

Code

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

Edit 1

to support the new clarifications by the 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

Upvotes: 1

Related Questions