Search code examples
excelvbaruntime-erroruserform

Hiding pages in two userforms - runtime error 5


I have two different userforms. The first UserForm has a multipage with 10 pages and 56 checkboxes. The second UserForm has one multipage with 56 pages. I have written a code that runs through all the checkboxes in UserForm1 and shows or hides the page in UserForm2, depending on the value of the checkbox.

Briefly explained: If checkbox1 in UserForm1 is true, then page1 is shown in UserForm2. If Checkbox1 in UserForm1 is false, then Page1 in UserForm2 is hidden. The loop should do this for each of the 56 checkboxes. Unfortunately, I keep getting runtime error 5. The debugger shows me the error in the line: UserForm2.MultiPage1("Page" & i).Visible = False. I really appreciate your help.

Private Sub CommandButton1_Click()
UserForm1.Hide
Dim i As Integer
For i = 1 To 56
    If UserForm1.Controls("CheckBox" & i).Value = True Then
        UserForm2.MultiPage1("Page" & i).Visible = True
    ElseIf UserForm1.Controls("CheckBox" & i).Value = False Then
        UserForm2.MultiPage1("Page" & i).Visible = False
    End If
Next i
End Sub

Solution

  • Problem solved: The error was that the index of pages starts counting at 0. In addition, I added the following code to the button that opens UserForm1:

    Private Sub CommandButton2_Click()
    UserForm2.Show vbModeless
    UserForm1.Show vbModeless
    UserForm2.Hide
    End Sub
    

    The loop now looks like this:

    Private Sub CommandButton1_Click()
    UserForm1.Hide
    Dim i As Integer
    For i = 1 To 55
        If UserForm1.Controls("CheckBox" & i).Value = True Then
            UserForm2.MultiPage1.Pages(i - 1).Visible = True
        ElseIf UserForm1.Controls("CheckBox" & i).Value = False Then
            UserForm2.MultiPage1.Pages(i - 1).Visible = False
        End If
    Next i
    End Sub