Q: set focus dynamically in multipage I have made a userform with all fields mandatory. There are 5 Pages on this userform. I need to set focus on validation to the field which is empty. I tried doing that with an IsError statement
Dim i As Integer
  For Each ctrl In Controls 'loop through Controls and search for Control with the right name

 i = 0
            If ctrl.Value = "" Then
                MsgBox ctrl.Name, vbExclamation, "Input Data"


                While IsError(ctrl.SetFocus)
                UserForm1.MultiPage1.Value = i
                i = (i + 1) Mod 5
                Wend
                ctrl.SetFocus
                Exit Sub

            End If
            Next

I also tried doing the same with an error handler with no success
Dim i As Integer

      For Each ctrl In Controls 'loop through Controls and search for Control with the right name
            i=0

                If ctrl.Value = "" Then
                    MsgBox ctrl.Name, vbExclamation, "Input Data"
                    On Error GoTo ErrHandler:

                    ctrl.SetFocus
                    ErrHandler:
                    UserForm1.MultiPage1.Value = i
                    i = (i + 1) Mod 5
                    Resume
                    Exit Sub
                End If
      Next

Any Help will be appreciated

A: Take a look at the below. It is one way of doing it.
Sub example()
Dim pageIndx As Integer
Dim firstPage As String
pageIndx = 0 'leave at 0, first page
CurrentPage = "Page1" 'Make sure all pages are named the same,
                  'with an increasing number at the end(it is like this by default)

For Each ctrl In UserForm1.Controls
    On Error Resume Next
    Debug.Print ctrl.Name
    Debug.Print ctrl.Parent.Name
    If (ctrl.Parent.Name Like "Page*" And ctrl.Parent.Name > CurrentPage) Then
         pageIndx = pageIndx + 1
    End If
    If ctrl.Value = "" Then
        UserForm1.MultiPage1.Value = pageIndx: UserForm1.Show
        MsgBox ctrl.Name, vbExclamation, "Input Data"
        ctrl.Value = "Input Data"
        Exit Sub
    End If
 Next ctrl

 End Sub



*

*Then use the below code in each textBox _MouseDown sub, the userform code section (In this example, it's TextBox3). This will clean the textBox when the user clicks on it to type.
If (Me.TextBox3.Value = "Input Data") Then
    Me.TextBox3.Value = ""
End If


A: Here's one way - it assumes that any Listboxes/comboboxes require a selection from their lists:
Private Sub CommandButton1_Click()
    Dim ctl                   As MSForms.Control
    Dim pg                    As MSForms.Page
    Dim bFoundOne             As Boolean

    For Each pg In Me.MultiPage1.Pages
        For Each ctl In pg.Controls
            Select Case TypeName(ctl)
                Case "TextBox"
                    If ctl.Value = vbNullString Then
                        bFoundOne = True
                        FlagInvalid pg.Index, ctl
                        Exit For
                    End If
                Case "ListBox", "ComboBox"
                    If ctl.ListIndex = -1 Then
                        FlagInvalid pg.Index, ctl
                        bFoundOne = True
                        Exit For
                    End If

            End Select
        Next ctl
        If bFoundOne Then Exit For
    Next pg
End Sub
Sub FlagInvalid(lngIndex As Long, ctl As MSForms.Control)
    MsgBox "Please fill out ALL controls"
    Me.MultiPage1.Value = lngIndex
    ctl.SetFocus
End Sub


A: You can only SetFocus to an Enabled control that is on the currently selected MultiPage.  If you check MultiPage.Value before attempting to SetFocus, you can choose to set the focus only when the relevant MultiPage page is in focus, or you could change MultiPage.Value to the page on which the control is, then use SetFocus. You may also have to SetFocus first to any frame in which the control lives.
You might also want to consider getting the name of the first control which fails validation whilst you're performing the validation and then simply using
Controls(<nameofcontrol>).SetFocus

Here's some example code - I use a Public Enum for the MultiPage values and mvarlMeasuresControlSets is a counter for Measures Control Sets, which are created dynamically every time the User needs to enter a new Measure:
' Ensure that fraMeasures has focus and then SetFocus to txtName:
If MultiPage.value = VMT_DASHBOARD_PAGE_MEASURES Then
  If Controls("txtMeasureName" & mvarlMeasuresControlSets).Enabled Then
    Controls("fraMeasures").SetFocus
    Controls("txtMeasureName" & mvarlMeasuresControlSets).SetFocus
  End If
End If

