Excel VBA: Introduction to UserForms. Excel VBA Level 2 Lesson 1

 

HOME | Back to index | Next> Buy the complete Excel course, Including VBA & Workbook examples for only $9.95 | Ask a question?

This series of x lessons will introduce UserForms as implemented for Visual Basic in Excel.  It is for developers and programmers, progressing from basic topics in the early lessons to more advanced topics in the later lessons.  The viewpoint of the client and user is considered where applicable.

UserForms were first introduced into Excel in Excel 97.  Prior to this Dialog sheets were used.  These are still available by right clicking on any Worksheet name tab and selecting Insert>MS Excel 5.0 Dialog.  This will insert a Dialog sheet that has a Dialog box on it and by default the Forms toolbar will appear.  However, they are only available for backward compatibility with previous versions of Excel and should only be used for this reason.  The UserForms that are now available provide much greater flexibility for both the user and developer.  The UserForm is still at times referred to as a Dialog box. 

UserForms are Visual Basic objects, with properties, methods, and events.

Perhaps the best result of the introduction of UserForms is not so much the Form itself as the ability for it to accept ActiveX controls.  These controls provide far more flexibility than the controls available on the Forms toolbar (OLE custom controls).  They have far superior event handling that allows us to respond to a user’s actions in ways that were previously not possible.  Specific code can be written to be run when the user clicks the control, enters, exits, double clicks, right clicks, enters text, and much more.  Basically, Excel is able to capture and react to any action the user takes.

Controls are also Visual Basic objects, with properties, methods, and events.

When to Use a UserForm?

There is no definitive answer to this question, as the use of the UserForm allows such flexibility that one can be used in a wide range of situations.  It can be for something as simple as creating your own message box, and for something as complex as replacing the entire Excel interface.  You will no doubt find that you will use Excel UserForms more and more as you become more comfortable with them.  The only restriction will be your own imagination and knowledge of the controls that can be used on them.

If you want to give your projects a professional and consistent look and feel, then UserForms will certainly do this.  However, having said this, be careful you don't use a UserForm for this reason only!  While a professional look and feel is great, the professionalism soon dissipates if the code is not there to support it.  So my advice is, if you feel confident enough to use a UserForm, then there is no reason why you shouldn't. 

Having encouraged you to use a UserForm though, you should be aware that UserForms often require a lot more coding than normal.  This is because you must ensure that all user actions are handled, that data are valid, and that data are properly stored in a collection area (data base).  While this probably sounds like a relatively simple task, there are a lot of angles to cover.  Unfortunately, users make random button clicks and enter invalid data!

What to Use a UserForm For?

There is no definitive answer to this question, either.  Common applications are for entering and validating data, for displaying data, for setting conditions, for running programs, for informing the user of errors or exceptions, and for guiding the user through dealing with errors and exceptions.  All of these are accomplished by inserting code into the Private Module of the UserForm.  Code that validates what the user enters is followed by code to save the validated information to a spreadsheet in the correct place, and to format it correctly.  When the UserForm is programmed properly, it is virtually impossible for erroneous data to be entered, while at the same time making data input for the user very easy indeed. 

UserForms and their associated controls can be seen extensively throughout Excel.  Activate any menu item that displays a Dialog box and we are looking at a UserForm.  This can be a useful way for us to decide if a UserForm is suited to the task at hand.  It can also be used to guide us in which control to choose for a particular situation. 

As I have mentioned above, as you become more comfortable with UserForms and their controls, you will start to use them for a variety of tasks as simple as instructing a User, through to creating your own wizards that will guide the user through complex tasks.

Common Controls

To help envision the application in the next step, it is necessary to know the common controls and their functions.  These controls can be located any place on the UserForm.

 

Control

Description

Label

Displays text that cannot be edited by the user, such as field labels, notes, and instructions.

TextBox

Displays a single field of text the user enters and that the user can edit.

ListBox

Displays a list of items from which the user can select, optionally with multiple fields.

ComboBox

Combines the function of a TextBox and a ListBox.

CheckBox

Displays an option that can be turned on or off.  Any number of a list of checkboxes may be selected.

Frame

Used to group other controls, especially OptionButtons.

OptionButton

Similar to a CheckBox, but only one OptionButton in a group can be "on" at one time.  To create a group of OptionButton controls, place a Frame control on the UserForm first, then draw the OptionButton controls on the Frame.

CommandButton

Displays a button that's clicked by the user to carry out some action such as Save or Enter.

ToggleButton

A button that can be either "up" or "down," permitting the user to select and deselect items.  It looks like a CommandButton but acts like a CheckBox.

TabStrip

Displays two or more tabs at the top of the control, permitting the user to select one.  It is for displaying small amounts of the same information for each tab.

MultiPage

Displays a large amount of information split into two or more pages.

ScrollBar

Displays a vertical or horizontal scrollbar on the UserForm.

SpinButton

Increments and decrements numbers by clicking with the mouse.

Image

Displays an image.

Many other controls are available, and will be discussed in Advanced Topics.

Gathering Requirements

To design a good UserForm, it is essential that someone must develop a vision for how the application is to look and how it is to be used.  This can be the customer, the programmer, or a system analyst.  The programmer must have this vision in mind when coding and when testing.  Vision and requirements are best established before Excel is even opened.

Everyone has used preprogrammed forms, both for commercial software packages and with web pages.  These are excellent sources for ideas for the designer.  Users become accustomed to finding functions organized in logical fashion, with consecutive actions sequentially located on the form.

These are many of the questions that need to be answered, grouped by topic.

Download User Requirements Checklist in the attached file UserForms Lesson 1A 2007.  This is the above checklist in a standalone file.

Envision the Application

The vision of how the UserForm will be used includes its overall look and some details about how the various controls will look, be arranged, and be used.  A good approach is to use a piece of paper with an old fashioned pencil with an eraser.  A control of some kind will be needed for each item identified in the requirements.  The most user-friendly order of controls will probably be left-to-right or top-to-bottom, but this may not be true if the client is left-handed or of a right-to-left language such as Hebrew or Arabic.  Leave extra room everywhere, both in the sketch and when first creating the layout.  It is far easier to add additional controls, move controls around, and make the layout neat and compact at the end, than it is to enlarge and make more room many times during the design. 

If the client requires an identifying banner or proprietary icon, it must be placed and sized first.  Then place the various controls.  Sketch the biggest items first, such as ListBoxes and ComboBoxes, labeling each above the upper left corner.  Second, sketch in TextBoxes to be used for data entry, with labels above or to the left.  If the data is to be validated, then at the bottom or side of the sheet sketch a MsgBox or another UserForm that will inform the user of the invalid data and what to do to correct the problem.  Then sketch in buttons such as Enter, Submit, OK, or Cancel, and write the function inside the button.  Lastly, sketch any small controls such as CheckBoxes, OptionButtons, or SpinButtons.  Don’t worry about whether you have every detail at this point.  Many details will become apparent as you do the layout, the coding, and the testing.

If the application will have multiple tabs or cascading pages, use one sheet of paper for each tab or page. 

Now Where?

Once all the preparation is complete, the next steps are:

  1. Create and rename the new UserForm (Lesson 2).

  2. Create, size, position, and rename the controls (Lessons 2, 3, and 4).

  3. Submit the layout to the client for preliminary feedback.

  4. Write initialization code to populate ListBoxes and ComboBoxes (Lesson 5).

  5. Write code to load the new UserForm (Lesson 5).

  6. Debug the initialization (Lesson 6).

  7. Write code to handle user actions (Lesson 7).

  8. Test user action handling code

  9. Write code to validate user inputs (Lesson 9).

  10. Test input validation code.

  11. Set the tab order for controls requiring user input and controls offering user selection choices.

  12. Evaluate the application for ease of customer use.  Correct any problems found.

  13. Polish the look by moving and resizing controls and UserForms, and by picking appearance properties.

  14. Evaluate the application execution speed.  Locate and improve performance problems.

  15. Submit the project to the client.

  16. Make corrections as needed.

  17. Celebrate in an acceptable manner.


These steps do not always need to be in this order, and several of the steps involving coding and testing will occur reiteratively.

 

HOME | Back to index | Next> Buy the complete Excel course, Including VBA & Workbook examples for only $9.95 | Ask a question?

 

Microsoft ® and Microsoft Excel ® are registered trademarks of Microsoft Corporation. OzGrid.com is in no way associated with Microsoft.

OzGrid.com accepts no responsibility for any adverse affect that may result from undertaking our training.