Importing And Exporting Userforms
If you've worked with UserForms, you've undoubtedly noticed that inserting a new UserForm results in an empty dialog box. You might prefer to add a UserForm that already has some controls on it (for example, an OK button and a Cancel button).
In this document I describe a simple technique that lets you create a new "default" UserForm that contains these two buttons. The procedure can be adapted for other controls.
Creating the default UserForm
Follow these steps to create a UserForm.
- Start with a blank workbook.
- Press Alt+F11 to activate the Visual Basic Editor (VBE)
- In the Project window, select the blank workbook
- Select the Insert UserForm command. An empty UserForm is added to the project.
- Use the Toolbox and add a CommandButton to the form.
- Change the following properties for the CommandButton:
- Use the Toolbox and add a second CommandButton to the form.
- Change the following properties for this CommandButton:
- Double-click the Cancel button to activate the Code window for the UserForm.
- Modify the CancelButton_Click subroutine as follows:
Private Sub Cancel_Button_Click() Unload Me End Sub
Exporting the UserForm
The next step is to export this UserForm.
- Make sure the UserForm is selected in the Project window.
- Select the File Export File command.
- Enter a name for the exported UserForm. Use a descriptive name, like NewDefaultForm
The preceding steps saved the UserForm and code to a file.
Importing the UserForm
When you need to add a new UserForm to a project, you can save time by importing the file you saved.
- Make sure your project is selected in the Project window.
- Select the File Import File command.
- Locate the file you exported in the previous section.
- Use the Properties window to give the new form a descriptive name. This step is necessary if you later decide to import the file again to add another UserForm.
Search for Tips
Browse Tips by Category
Needs tips? Here are two books, with nothing but tips:
Contains more than 200 useful tips and tricks for Excel 2007 | Other Excel 2007 books | Amazon link: John Walkenbach's Favorite Excel 2007 Tips & Tricks