Excel Developer Tip


Return to The Spreadsheet Page

Excel page

Tip archives

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.

  1. Start with a blank workbook.
  2. Press Alt+F11 to activate the Visual Basic Editor (VBE)
  3. In the Project window, select the blank workbook
  4. Select the Insert UserForm command. An empty UserForm is added to the project.
  5. Use the Toolbox and add a CommandButton to the form.
  6. Change the following properties for the CommandButton:

    Name: OKButton
    Caption: OK
    Default: True
  7. Use the Toolbox and add a second CommandButton to the form.
  8. Change the following properties for this CommandButton:

    Name: CancelButton
    Caption: Cancel
    Cancel: True
  9. Double-click the Cancel button to activate the Code window for the UserForm.
  10. 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.

  1. Make sure the UserForm is selected in the Project window.
  2. Select the File Export File command.
  3. 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.

  1. Make sure your project is selected in the Project window.
  2. Select the File Import File command.
  3. Locate the file you exported in the previous section.
  4. 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.