Learn how to create your own custom dialog boxes in VBA, using UserForms. This is the first part of a three-part series (the other parts are on Writing Code for UserForms and Creating Advanced Controls).
- An Introduction to UserForms in VBA (this blog)
- Creating and Using UserForms in VBA
- The Properties Window
- Selecting Forms and Controls
- Formatting Forms and Controls
- Grouping, Aligning, Sizing, Spacing and Arranging
- Controlling How Form Keys Work
Posted by Andy Brown on 28 February 2012 | no comments
An Introduction to UserForms in VBA
What UserForms are
A UserForm (or just form) is a dialog box that you create yourself. You've almost certainly already seen some pre-created forms in Visual Basic for Applications:
However,neither of these can be customised much. A UserForm, by contrast, can have lots of widgets and formatting on it:
This user form allows someone in your office to order a drink.
UserForms aren't limited to Excel: Word, PowerPoint and SharePoint Designer use the same software (although Access and Outlook, typically, do their own thing).
Pros and Cons of UserForms
Should you learn to create and use UserForms? Have a look at the pros and cons below, and make up your own mind! Let's start with the good things:
|Impressiveness||There's nothing like a custom form laden with drop down lists, command buttons, pictures and formatting to impress a client or manager.|
|Ease of use||For people who don't enjoy inputting data directly into Excel, you can create a front-end form instead and make it as easy to use as possible.|
Those are powerful advantages; how about the other side of the coin?
|Validating data||Creating user forms is easy; making sure that users input data correctly takes longer. On a worksheet you can use tools like data validation and protection to ensure users enter the right type of data, but in a form you're going have to work much harder to avoid errors.|
|Flakiness||My experience from many years of training on Excel and writing systems is that user forms occasionally crash - Excel itself rarely does.|
Now that you know what a form is, let's look at how to create one, and how to attach macros to it.