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:
Name: OKButton
Caption: OK
Default: True
- Use the Toolbox and add a second CommandButton to the form.
- Change the following properties for this CommandButton:
Name: CancelButton
Caption: Cancel
Cancel: True
- 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.
|