Search code examples
excelvbatabsuserformmultipage

How to dynamically get the value of current multipage tab value?


Community, I am currently hiding my tabs on my userform multipage except for the current tab. The user can click buttons to switch back and forth between pages. Some buttons share sub routines. When a user clicks a button, it hides the previous tab once the new tab has been selected. I suppose this is a dual question.

1) How can I get the previous tab selection value?

2) How can I loop through my tab values? My objective is to test the current tab caption or value against all the others. Figured this would be an easy way of hiding them all regardless as to which page and which button calls the subroutine.

Right now I only have this for one tab button...

Sub NewCreditSetup()
    MultiPage1.Pages(1).Visible = True
    MultiPage1.Value = 1
    MultiPage1.Pages(0).Visible = False
    //More code displaying tab...irrelevant
End Sub  

Solution

  • You can use the tab change event to determine when the tab is changed and store the current tab index as a variable. Then when the tab is changed again, the tab in this variable becomes the previous tab.

    Ie:

    Private iPrevTab As Integer
    Private iCurTab As Integer
    
    Private Sub MultiPage1_Change()
        iPrevTab = iCurTab
        iCurTab = MultiPage1.Index
    
        'You can also check here what that tab is to do something with it
        If MultiPage1.Value = MultiPage1.Pages("mySpecialPage").Index Then
            'Go Nuts
        End If
    
    End Sub
    

    You can then loop through all the tabs and check against their name, caption or index. Eg:

    Private Sub LoopTabs()
        Dim ii as Integer        
    
        for ii = 1 to MultiPage1.Pages.Count
            If MultiPage1.Pages(ii).Index = iPrevTab Then
               Debug.Print MultiPage1.Pages(ii).Name & " " & MultiPage1.Pages(ii).Caption
            End If
        Next ii
    End Sub
    

    It's probably also worth noting to be careful showing and hiding tabs as it is not common and could possibly confuse the user. I'll leave that up to you though.