SetFocus on Multipage UserForm

leopardhawk

Well-known Member
Joined
May 31, 2007
Messages
587
Office Version
  1. 2016
Platform
  1. Windows
Hello forum friends, I have a multipage userform with 6 pages, with each page having several textboxes. The first textbox on each page has TabIndex set to 0. I also have this little snippet in the code under "Private Sub UserForm_Initialize()"
VBA Code:
MultiPage1.Value = 0
    Me.EstCPPTextBox1.SetFocus
When the userform is first opened, the cursor is in the first textbox on page 1. If I left-click on any of the other pages on the form, the focus is on the 'tab' at the top for that page. However, If I right-click on any of the other pages, the focus is now in the first textbox on that page which is where I want it to be!!! I think most users are going to use the left-click to move between pages which then forces them to have to tab-key or click to get to the first textbox on that page. Not a huge deal but I am trying to make the user experience as best as I can. So weird, I can't figure out if it's supposed to be like this or what?

Is there some other code I can add to make this work?? Thanks for any ideas.
 

Some videos you may like

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
8,186
Office Version
  1. 2016
Platform
  1. Windows
This should put the focus on the TextBox whose TabIndex is 0 for each Page

VBA Code:
Private Sub MultiPage1_Change()

    GetTextBoxByTabIndex(Me.MultiPage1.SelectedItem, 0).SetFocus

End Sub

Private Function GetTextBoxByTabIndex(ByVal ControlParent As Object, ByVal TabIndex As Long) As Control

    Dim oCtrl As Control
    
    For Each oCtrl In ControlParent.Controls
        If TypeOf oCtrl Is MsForms.TextBox Then
            If oCtrl.TabIndex = TabIndex Then
                Set GetTextBoxByTabIndex = oCtrl
                Exit For
            End If
        End If
    Next oCtrl

End Function
 
Solution

Watch MrExcel Video

Forum statistics

Threads
1,127,752
Messages
5,626,661
Members
416,199
Latest member
Gautamsunil

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top