excelvbauserformmultipage

Selecting Specific Pages in a Userform when MultiPages are Nested


I have a single userform with 3 "top-level" pages (using the multipage functionality) and then four nested pages each within two of the top-level pages.

Screenshot of Nested Multipage Userform

Screenshot of Nested Multipage Userform

In this example, the page captions and names are the same. Therefore, the page with the caption of Page4 is also named Page4. I found code online that is supposed to return the page index when the page name is passed to it:

Function fMpIndex(ByVal PageName As String)

    Dim PageCounter As Long

    With MultiPage1

        For PageCounter = 0 To .Pages.Count - 1

            If .Pages(PageCounter).Name = PageName Then fMpIndex = PageCounter: Exit Function
        Next PageCounter
    End With
    fMpIndex = -1

End Function

This works for the top-level pages, however, when I try something like:

MsgBox ("This page's index is " & fMpIndex("Page5"))

for any of the nested pages (in this example, both pages 4 & 5), then this method returns a value of -1, indicating that the page does not exist. Is this a scope issue? Does the code above only search for the top-level pages and not the nested pages?

What is the safest, most reliable way to programmatically reference a specific page by either name or caption or both? I assume it would be something like:

which userform.which page strip.which page.name("Page5")

In this example, I only have one userform, but what if I had two userforms? Also, in the above example, I am assuming that one must specify which of the two page strips are being referenced.

Ideally, I want a way to use an absolute reference to each page in the userform regardless of whether it is a top-level page or a nested page.


Solution

  • In your screenshot, the only pages in MultiPage1 are Page1, Page2, and Page3. These can be referenced using (for example) MultiPage1.Pages("Page1"). MultiPage1.Pages("Page4") does not exist.

    Page1 contains MultiPage2, and its pages are Page4 and Page5. These can be references using MultiPage2.Pages("Page4").

    You can reference pages in a MultiPage control by index, as in MultiPage1.Pages(iPageIndex), where iPageIndex goes from 0 to the number of pages minus one. In your screenshot, MultiPage1.Pages(1) references Page2, and MultiPage2.Pages(0) references Page4.