BLOGS BY TOPIC▼
BLOGS BY AUTHOR▼
BLOGS BY YEAR▼
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
- Creating and Using UserForms in VBA (this blog)
- 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
You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
Creating and Using UserForms in VBA
Creating a Form
It's easy enough to create a user form in VBA - within the code editor, just right-click and choose to add a user form:
Right-click in a project/workbook and choose the option shown to insert a user form.
You'll be shown a rectangular form, which you can resize using the bottom right corner:
Click and drag on the bottom right corner to resize a user form - it will always appear at the top left corner of the code editor window.
Just like macros, a user form is stored as part of a workbook, rather than in a separate file.
The things that you add to a user form are called controls. The following simple form has 3 controls:
This form has 1 label, 1 textbox and 1 command button.
The following drinks order form has more than 10 controls:
This relatively simple form contains:
A later blog in this series shows how to add some of the more esoteric controls - for now, we'll stick to general principles.
Drawing Controls on a Form
You can draw any control on a form using the Toolbox:
The form Toolbox - the simplest controls are shown at the top left.
If you can't see your Toolbox window when you have a form open, try clicking on the Toolbox tool:
Click on the tool shown to display the Toolbox, if it's gone AWOL.
Once you've chosen your control tool, you can click and drag on your form where you want it to go:
Here the mouse pointer shows that you are drawing a Label control.
It's often easier just to click where you want a control to go, and Excel (or any other application) will draw it with a standard size and shape.
Having learnt how to draw controls, let's move on to how to show their properties.