+ Reply to Thread
Results 1 to 2 of 2

Run time Error 402

  1. #1
    ExcelMonkey
    Guest

    Run time Error 402

    I have a form with two option buttons. If OptionButton1
    is called, it loads a form and clicks its OK button. The
    OK Button is a Public Button_click event. The user never
    sees the form.

    If OptionButton2 is clicked, it shows the form and the
    user then must make some choices and then clicks the Ok
    Button himself. When I click OptionButton2 everythign
    works fine. However when I click optionbutton1 I get a
    run time error 402 "Must close or hide topmost modal form
    first". It seems to fail on the line:

    WorkSheetSelectForm.OKButton_Click



    Sub WorkSheetSummary()
    Dim ws As Worksheet

    If UserForm1.OptionButton1.Value = True Then
    Load WorkSheetSelectForm
    WorkSheetSelectForm.OKButton_Click
    Exit Sub
    End If

    If UserForm1.OptionButton2.Value = True Then
    For Each ws In ActiveWorkbook.Worksheets
    WorkSheetSelectForm.ListBox1.AddItem (ws.Name)
    Next
    End If

    WorkSheetSelectForm.Show

    Public Sub OKButton_Click()
    Dim TotalSheets As Integer
    Dim ListItems1 As Integer
    Dim ListItems2 As Integer
    Dim X As Integer
    Dim Y As Integer
    Dim ExcludeOnlyArray As Variant
    Dim Z As Variant

    ListItems1 = ListBox1.ListCount
    ListItems2 = ListBox2.ListCount

    ReDim ExcludeOnlyArray(0 To ListItems2)
    For X = 1 To ListItems2
    ExcludeOnlyArray(X - 1) = ListBox2.List(X - 1)
    'Debug.Print ExcludeOnlyArray(X - 1)
    Next

    ReDim SheetExcludeArray(0 To 1, 0 To ListItems1)

    For Y = 1 To ListItems1
    SheetExcludeArray(0, Y - 1) = ListBox1.List(Y - 1)
    'Debug.Print SheetExcludeArray(0, Y - 1)
    Next



    For X = 1 To ListItems1
    Z = Application.Match(SheetExcludeArray(0, X - 1),
    ExcludeOnlyArray, 0)

    If Not IsError(Z) Then
    SheetExcludeArray(1, X - 1) = 1
    Else:
    SheetExcludeArray(1, X - 1) = 0
    End If
    Debug.Print SheetExcludeArray(0, X - 1) & " " &
    SheetExcludeArray(1, X - 1)
    Next


    WorkSheetSelectForm.Hide
    End Sub

    End Sub

  2. #2
    Tom Ogilvy
    Guest

    Re: Run time Error 402

    As I recall from previous posts, you are showing the form so it will
    initialize an array. Why not just initialize the array in the OptionButton1
    click event and skip showing the form.

    If the userform can be shown and closed by your code, whatever it does
    should be easily accomplished by your code without showing and closing the
    form.

    --
    Regards,
    Tom Ogilvy



    "ExcelMonkey" <[email protected]> wrote in message
    news:[email protected]...
    > I have a form with two option buttons. If OptionButton1
    > is called, it loads a form and clicks its OK button. The
    > OK Button is a Public Button_click event. The user never
    > sees the form.
    >
    > If OptionButton2 is clicked, it shows the form and the
    > user then must make some choices and then clicks the Ok
    > Button himself. When I click OptionButton2 everythign
    > works fine. However when I click optionbutton1 I get a
    > run time error 402 "Must close or hide topmost modal form
    > first". It seems to fail on the line:
    >
    > WorkSheetSelectForm.OKButton_Click
    >
    >
    >
    > Sub WorkSheetSummary()
    > Dim ws As Worksheet
    >
    > If UserForm1.OptionButton1.Value = True Then
    > Load WorkSheetSelectForm
    > WorkSheetSelectForm.OKButton_Click
    > Exit Sub
    > End If
    >
    > If UserForm1.OptionButton2.Value = True Then
    > For Each ws In ActiveWorkbook.Worksheets
    > WorkSheetSelectForm.ListBox1.AddItem (ws.Name)
    > Next
    > End If
    >
    > WorkSheetSelectForm.Show
    >
    > Public Sub OKButton_Click()
    > Dim TotalSheets As Integer
    > Dim ListItems1 As Integer
    > Dim ListItems2 As Integer
    > Dim X As Integer
    > Dim Y As Integer
    > Dim ExcludeOnlyArray As Variant
    > Dim Z As Variant
    >
    > ListItems1 = ListBox1.ListCount
    > ListItems2 = ListBox2.ListCount
    >
    > ReDim ExcludeOnlyArray(0 To ListItems2)
    > For X = 1 To ListItems2
    > ExcludeOnlyArray(X - 1) = ListBox2.List(X - 1)
    > 'Debug.Print ExcludeOnlyArray(X - 1)
    > Next
    >
    > ReDim SheetExcludeArray(0 To 1, 0 To ListItems1)
    >
    > For Y = 1 To ListItems1
    > SheetExcludeArray(0, Y - 1) = ListBox1.List(Y - 1)
    > 'Debug.Print SheetExcludeArray(0, Y - 1)
    > Next
    >
    >
    >
    > For X = 1 To ListItems1
    > Z = Application.Match(SheetExcludeArray(0, X - 1),
    > ExcludeOnlyArray, 0)
    >
    > If Not IsError(Z) Then
    > SheetExcludeArray(1, X - 1) = 1
    > Else:
    > SheetExcludeArray(1, X - 1) = 0
    > End If
    > Debug.Print SheetExcludeArray(0, X - 1) & " " &
    > SheetExcludeArray(1, X - 1)
    > Next
    >
    >
    > WorkSheetSelectForm.Hide
    > End Sub
    >
    > End Sub




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1