|
|
| Back to Excel Homepage | Excel VBA - Reference Guide |
|
Menu
Other links Userform input example
|
This article steps you through a simple user form input which adds record information into a worksheet. The form is a basic design with the emphasis on how to build the form and code it to respond to the functionality we are after. Here's what we are are going to achieve: 1. A user form will load from a button on a worksheet.
2. Users must complete Firstname, Surname and
choose a Department (which will 3. When choosing the Add button it will append to
the worksheet (called Data) and 4. The form will remain open clearing the values ready for
the next record input until 5. A private macro (from the standard module) calls the
user form (via its worksheet
There are properties and code for the form, two and buttons and drop-down combo box which we will need to add the form's private module.
The order in creating such a feature should loosely follow these steps: 1. Create the user form canvas. 2. Add the controls to the form and set various basic properties (including names). 3. Add code to the form's controls 4. Code the interaction to the worksheet (& prepare the worksheet layout too). 5. Add a macro to call the form and attach to a button on the worksheet. 6. Test the process!
Create the user form canvas Add a new blank user form the VBA Project. In the VBE Editor, select Insert, UserForm.
Add the controls to the form You need to add the following controls: 1. Two Command Buttons 2. Three Labels 3. Two TextBoxes 4. One ComboBox 5. One CheckBox
Place the controls roughly where you would like to use these control and resize the form. Don't worry about the exact position for now:
Setting the properties to each control The following controls can be set using the Properties Window (F4 function key). First single click to select the control (so it has the focus) and then from the properties changes their settings.
Here's the table for the above controls (and user form itself):
You can change some of these properties to taste - this is what I'm using in this example.
Adding code to controls The next step is start coding the form and it's important that you have at least named the controls you wish code as it will generate its own event signature.
Starting with the Close button which will simply close and end the user form.
Unload Me refers to itself which is quick and easy. To explicitly close a user form, you refer to actual name of the form. Therefore, using Unload frmDataInput will be the same outcome.
Next, lets add code (run time) to populate the ComboBox control (cboDept) which will dynamically create four fixed options to choose from.
As the form loads (initialises), it adds four items to the cboDept control and then positions the cursor in txtFName ready for the user to start keying in data.
You could of course set this in the properties (RowSource) for cboDept instead which refers to range of cells in a worksheet.
The final piece of code is attached the cmdAdd button control so when users click this event, it will add the details to the worksheet (Data).
The above should be easy to follow (look at the comments). We don't have to tell the system which worksheet to be in as it is going to be called from a control (worksheet button) where the data is held in the same worksheet and then hide this procedure from the Macros dialog box stopping any other way for this form to be called.
Create a worksheet button In the worksheet in Excel, click the Developer tab from the Ribbon Bar and the Insert icon to drop-down a list of controls. Choose the Button control icon from the Forms (section) and draw a button where you wish to place it (top row, frozen pane area). In the assigning macro pop-up dialog box, click the New... button to create a module and signature and add the following code:
Add the keyword Private before the Sub keyword to hide this from the macro dialog box.
TEST IT OUT!
This is how the form looks as it is called from the worksheet button control from the Data worksheet.
|
|
© 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. |