Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 172
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default 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" wrote in message
...
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
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Visual Basic Error Run Time Error, Type Mismatch Meg Partridge Excel Discussion (Misc queries) 12 September 10th 08 06:10 PM
Run time error 1004 General ODCB Error Kevin Excel Programming 3 February 26th 05 12:51 PM
Application Run Time Error 1004 and Stack Error ExcelMonkey[_190_] Excel Programming 9 February 11th 05 04:48 PM
Befuddled with For Next Loop ------ Run - Time Error '13' Type Mismatch Error rdavis7408 Excel Programming 1 August 25th 04 03:54 AM
Code Error - Run Time Error 5 (Disable Cut, Copy & Paste) Tim[_36_] Excel Programming 4 April 23rd 04 02:53 AM


All times are GMT +1. The time now is 10:06 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"