Userform in Excel VBA (Easy Macros)
Userform in Excel VBA (Easy Macros)
Userform in Excel VBA (Easy Macros)
Excel Easy
#1 Excel tutorial on the net
Userform
Chapter
Add the Controls | Show the Userform | Assign the Macros | Test the Userform
Userform
This chapter teaches you how to create an Excel VBA Userform. The Userform we are going
to create looks as follows:
Learn more, it's easy
Userform and Ranges
Currency Converter
Progress Indicator
Controls Collection
Interactive Userform
2. Click Insert, Userform. If the Toolbox does not appear automatically, click View, Toolbox.
Your screen should be set up as below.
https://www.excel-easy.com/vba/userform.html 1/5
9.11.2022, 09:25 Userform in Excel VBA (Easy Macros)
3. Add the controls listed in the table below. Once this has been completed, the result
should be consistent with the picture of the Userform shown earlier. For example, create a
text box control by clicking on TextBox from the Toolbox. Next, you can drag a text box on
the Userform. When you arrive at the Car frame, remember to draw this frame first before
you place the two option buttons in it.
4. Change the names and captions of the controls according to the table below. Names are
used in the Excel VBA code. Captions are those that appear on your screen. It is good
practice to change the names of controls. This will make your code easier to read. To
change the names and captions of the controls, click View, Properties Window and click on
each control.
Note: a combo box is a drop-down list from where a user can select an item or fill in
his/her own choice. Only one of the option buttons can be selected.
DinnerPlannerUserForm.Show
https://www.excel-easy.com/vba/userform.html 2/5
9.11.2022, 09:25 Userform in Excel VBA (Easy Macros)
End Sub
We are now going to create the Sub UserForm_Initialize. When you use the Show method
for the Userform, this sub will automatically be executed.
2. In the Project Explorer, right click on DinnerPlannerUserForm and then click View Code.
3. Choose Userform from the left drop-down list. Choose Initialize from the right drop-
down list.
'Empty NameTextBox
NameTextBox.Value = ""
'Empty PhoneTextBox
PhoneTextBox.Value = ""
'Empty CityListBox
CityListBox.Clear
'Fill CityListBox
With CityListBox
.AddItem "Oakland"
.AddItem "Richmond"
End With
'Empty DinnerComboBox
DinnerComboBox.Clear
'Fill DinnerComboBox
With DinnerComboBox
.AddItem "Italian"
.AddItem "Chinese"
End With
'Uncheck DataCheckBoxes
DateCheckBox1.Value = False
DateCheckBox2.Value = False
DateCheckBox3.Value = False
CarOptionButton2.Value = True
'Empty MoneyTextBox
MoneyTextBox.Value = ""
NameTextBox.SetFocus
End Sub
Explanation: text boxes are emptied, list boxes and combo boxes are filled, check boxes are
unchecked, etc.
https://www.excel-easy.com/vba/userform.html 3/5
9.11.2022, 09:25 Userform in Excel VBA (Easy Macros)
MoneyTextBox.Text = MoneySpinButton.Value
End Sub
Explanation: this code line updates the text box when you use the spin button.
Sheet1.Activate
'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
'Transfer information
Else
End If
End Sub
Call UserForm_Initialize
End Sub
Explanation: this code line calls the Sub UserForm_Initialize when you click on the Clear
button.
https://www.excel-easy.com/vba/userform.html 4/5
9.11.2022, 09:25 Userform in Excel VBA (Easy Macros)
Unload Me
End Sub
Explanation: this code line closes the Userform when you click on the Cancel button.
Result:
Userform • © 2010-2022
Excel is Awesome, we'll show you: Introduction • Basics • Functions • Data Analysis • VBA
https://www.excel-easy.com/vba/userform.html 5/5