Search code examples
excelvbaruntime-erroruserform

Run-time error 13 and after fix Run-time error 91


Hi I have problem with my program. I´m trying to open UserForm2 from my UserForm1 but I need to Initialize some code for UserForm2 before show.

I used UserForm2.Show in my code and open it from UserForm1 by Show UserForm2 This Worked fine but when closing it gives me error 91.

I tried to remove UserForm2.Show but this caused UserForm2 didn´t load and gives me error 13.

Here is my code

Module1:

Option Explicit
Public ListName As String

UserForm1:

Private Sub CommandButton1_Click()
If CheckBox5.Value = False And CheckBox6.Value = False Then
    MsgBox ("Nebyla zvolena operace.")
    GoTo Ukoncit
End If

If CheckBox1.Value = False And CheckBox2.Value = False And CheckBox3.Value = False And CheckBox4.Value = False Then
    MsgBox ("Nebyl zvolen list.")
    GoTo Ukoncit
End If

If CheckBox5.Value = True Then

End If
If CheckBox6.Value = True Then
    If CheckBox1.Value = True Then
        ListName = "SAP_CZ"
    ElseIf CheckBox2.Value = True Then
        ListName = "SAP_CH"
    ElseIf CheckBox3.Value = True Then
        ListName = "Datab_1"
    ElseIf CheckBox4.Value = True Then
        ListName = "Datab_2"
    End If
    Show UserForm2 ' <------------- Here is Opening UserForm2
End If
Ukoncit:
End Sub

Private Sub CommandButton2_Click()
Unload Me
End Sub

Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
CheckBox2.Enabled = False
CheckBox3.Enabled = False
CheckBox4.Enabled = False
CheckBox2.Value = False
CheckBox3.Value = False
CheckBox4.Value = False
End If
If CheckBox1.Value = False Then
CheckBox2.Enabled = True
CheckBox3.Enabled = True
CheckBox4.Enabled = True
CheckBox2.Value = False
CheckBox3.Value = False
CheckBox4.Value = False
End If
End Sub

Private Sub CheckBox2_Click()
If CheckBox2.Value = True Then
CheckBox1.Enabled = False
CheckBox3.Enabled = False
CheckBox4.Enabled = False
CheckBox1.Value = False
CheckBox3.Value = False
CheckBox4.Value = False
End If
If CheckBox2.Value = False Then
CheckBox1.Enabled = True
CheckBox3.Enabled = True
CheckBox4.Enabled = True
CheckBox1.Value = False
CheckBox3.Value = False
CheckBox4.Value = False
End If
End Sub

Private Sub CheckBox3_Click()
If CheckBox3.Value = True Then
CheckBox2.Enabled = False
CheckBox1.Enabled = False
CheckBox4.Enabled = False
CheckBox2.Value = False
CheckBox1.Value = False
CheckBox4.Value = False
End If
If CheckBox3.Value = False Then
CheckBox2.Enabled = True
CheckBox1.Enabled = True
CheckBox4.Enabled = True
CheckBox2.Value = False
CheckBox1.Value = False
CheckBox4.Value = False
End If
End Sub

Private Sub CheckBox4_Click()
If CheckBox4.Value = True Then
CheckBox2.Enabled = False
CheckBox3.Enabled = False
CheckBox1.Enabled = False
CheckBox2.Value = False
CheckBox3.Value = False
CheckBox1.Value = False
End If
If CheckBox4.Value = False Then
CheckBox2.Enabled = True
CheckBox3.Enabled = True
CheckBox1.Enabled = True
CheckBox2.Value = False
CheckBox3.Value = False
CheckBox1.Value = False
End If
End Sub

Private Sub CheckBox5_Click()
If CheckBox5.Value = True Then
CheckBox6.Enabled = False
CheckBox6.Value = False
End If
If CheckBox5.Value = False Then
CheckBox6.Enabled = True
CheckBox6.Value = False
End If
End Sub

Private Sub CheckBox6_Click()
If CheckBox6.Value = True Then
CheckBox5.Enabled = False
CheckBox5.Value = False
End If
If CheckBox6.Value = False Then
CheckBox5.Enabled = True
CheckBox5.Value = False
End If
End Sub

UserForm2 giving Error 91:

Private Sub CommandButton1_Click()
    Unload Me
End Sub

Private Sub UserForm_Initialize()
    Dim Label1String As String
    Label1String = "Chcete opravdu smazat data z listu " & ListName
    UserForm2.Label1.Caption = Label1String
    UserForm2.Show
End Sub

UserForm2 giving Error 13:

Private Sub CommandButton1_Click()
    Unload Me
End Sub

Private Sub UserForm_Initialize()
    Dim Label1String As String
    Label1String = "Chcete opravdu smazat data z listu " & ListName
    UserForm2.Label1.Caption = Label1String
End Sub

Solution

  • This error is caused by Show UserForm2

    It should be

    UserForm2.Show
    

    And you don't need to have this in your UserForm_Initialize sub