|
|
| Back to Excel Homepage | Excel VBA - Reference Guide |
|
Menu Creating User Forms
Other links
|
Pre-defined Dialog Boxes like InputBox and MsgBox functions are useful and quick to use. However, designing your own Dialog Boxes (or User Form), allows you to add other controls and personalise your application. Creating a new User FormMake sure you are in the Visual Basic Editor and not Excel. Select Insert, UserForm:
Note: To load the Properties Window, press the F4 function key.
In addition to the Properties window, when active on the new form, the Toolbox Toolbar automatically appears. (If this is missing, use the View, Toolbox command to show it.) Also, you may want to display the ‘UserForm’ toolbar to align and rearrange the controls on the form. Select View, Toolbars and choose UserForm. Userform Toolbar
This toolbar is only available for designing and arranging objects when creating or modifying forms (user forms).
Alignments (Button 5)
Vertical/Horizontal Alignments (Button 6)
Sizes (Button 7)
Toolbox Toolbar
Adding Controls to a Custom Dialog Box By using the Toolbox, standard controls (command buttons, text boxes and others) can be added to a user form. Make sure the form is the active window. Click on the required control and then click on the user form roughly where the control is to be positioned or drag and drop the control from the Toolbox to the area on the form. Handlebars appear around the selected control. This allows control(s) to be resized and positioned.
Common Controls
There are many more controls than the standard set which is installed with Microsoft Excel and can be added. Select Tools, Additional Controls....
Tick the required item and choose the OK Button. This updates the Toolbox toolbar. Creating Tabs for UserformsIf a single user form needs to handle a large number of controls that can be sorted into categories, use a Tab Control.
Tab pages can be added and deleted using the properties of the tab (itself) of the control itself by right-mouse clicking the item.
Setting Controls with Properties (Design Time)Some controls are better set during the design side of a user form by using the Properties window. Typical examples of setting these controls:
Initialising Controls with Code (Run Time)Some controls (some included as above) can be set as the userform is running or before the form is displayed by setting properties with code. Typical examples of setting controls with code:
These controls are set to different events of a control. These include:
Example:
Five controls are set when the form (MyDialogBox) is shown (Initialised).
Displaying a UserformOnce the userform has been created, the next stage is to test to see how the user form will look. You can use the Run (F5 function key in design time mode) command when the active form is displayed. But, writing code is ultimately how a user form will be used. Decide where the code is to be stored (in a Module, Worksheet or Workbook). Use the name of the form with the Show method command. Example:
The user form is known, as ‘MyUserForm’ and the Show method will display the user form. There is one optional argument called Modal which can be explicitly defined and has a value of 0 or 1 .
1 = a modal state which means that users have to complete the form and can not click anywhere else (in the background).
0 = a modeless state which allows users to click outside the form area.
The default is 1 if omitted.
MyUserForm.Show 0 MyUserForm.Show 1 or MyUserForm.Show Adding Code to respond with User FormsEach control will have its own set of events. These events store the code and are executed when that event is triggered. For example, a Button recognises the Click_Event, a Form recognises an Intitialize_Event and a Combo Box recognises a Change_Event. To assign code to a control, display the form and double click on that control. This opens the module and the main event allowing code to be written:
Example: When the OK Button is clicked...
When the userform is displayed, if the OK Button is clicked, the above code is executed and checks to see if this Textbox (txtName) is empty or not using the If statement. If false, it displays a message prompt and shows the user form again. If true, it enters the data into a spreadsheet (range - NameResult).
Unload Me is the way to close a form (itself) The Me PropertyThe Me property returns a reference of the form itself that the code is currently running. This is used as shorthand for the full reference of a form.
Example: Suppose you have the following procedure in a module:
You can call this procedure and pass the current instance of the Form as an argument using the following statement:
The ChangeFormColour procedure is passed to the Me property in the current form running which therefore changes the colour of a specified control(s) to the colour defined.
RGB(Rnd * 256, Rnd * 256, Rnd * 256) is a Red, Green and Blue colour function.
To see an example, click on userform example
|
Tip: Press the F5 function key to run and preview a form during the design time environment.VBA Keywords: Show, If...Then...Else, MsgBox, RGB & Unload. |
|
© copyright 2010 TP Development & Consultancy Ltd, All Rights Reserved. All trademarks are copyrighted by their respective owners. Please read our terms of use and privacy policy. |