VBA User Form Demonstration
VBA User Form Demonstration
This demonstration steps 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 going to achieve:
1.
2.
Users must complete Firstname, Surname and choose a Department (which will
be coded as mandatory fields).
3.
When choosing the Add button it will append to the worksheet (called Data) and
always find the next available blank row to populate.
4.
The form will remain open clearing the values ready for the next record input until
the Close button is clicked.
5.
A private macro (from the standard module) calls the user form (via its worksheet
button).
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.
2.
Add the controls to the form and set various basic properties (including names).
3.
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.
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:
Property
Value
CommandButton1
Name
Caption
cmdAdd
Add
Default
True
Height
20
Width
60
Left
132
Top
114
TabIndex
Name
Caption
cmdClose
Close
Cancel
True
Height
20
Width
60
CommandButton2
Label1
Label2
Label3
TextBox1
TextBox2
ComboBox1
CheckBox1
Left
198
Top
114
TabIndex
Caption
Height
Firstname:
18
Left
12
Top
12
Width
72
Caption
Height
Surname:
18
Left
12
Top
36
Width
72
Caption
Height
Department:
18
Left
12
Top
60
Width
72
Name
Height
txtFName
18
Left
84
Top
12
Width
108
TabIndex
Name
Height
txtSName
18
Left
84
Top
36
Width
108
TabIndex
Name
Height
cboDept
18
Left
84
Top
60
Width
108
TabIndex
Name
Caption
chkManager
Manager
Height
18
UserForm1
Left
84
Top
84
Width
108
TabIndex
Name
Caption
frmDataInput
Data Input Example
Height
162.75
Width
267
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.
Private Sub UserForm_Initialize()
Me.cboDept.AddItem "Finance"
Me.cboDept.AddItem "Sales"
Me.cboDept.AddItem "Markerting"
Me.cboDept.AddItem "Human Resources"
Me.txtFName.SetFocus 'position the cursor in this control
End Sub
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).
Private Sub cmdAdd_Click()
Dim i As Integer
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:
Sub Button1_Click()
'load the form
frmDataInput.Show
End Sub
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 Dataworksheet.