Run time Error 402

E

ExcelMonkey

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
 
T

Tom Ogilvy

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top