PDA

View Full Version : Unload userforms



mike0123m
09-04-2007, 03:02 PM
I have a main menu userform with several sub menu userforms which are accessed from the main menu. On the sub menus are buttons that when clicked on open a specific spreadsheet. Once the spreadsheet, how do I get excel to close all the sub menu userforms and the main menu userform?

malik641
09-04-2007, 03:16 PM
Hi, Welcome to VBAX

The following should do:

Private Sub CommandButton1_Click()
' Open Spreadsheet here
' If spreadsheet opens, close all forms
Unload Me ' Userform3
Unload UserForm2
Unload UserForm1
End Sub

The idea is to unload the topmost userform first, and then go down the list backwards to how you opened the userforms.

Check out the attached file.

If you are using Modeless forms, the code will still work.

rory
09-05-2007, 03:59 AM
If you unload the main menu when you open a sub menu, then all you have to do is put Unload Me at the end of your button code. You can also use variables to instantiate the forms, put all the variables into a collection and just destroy that at the end.