Basics of VBA / Macros Chapter – 80 (Userform Part-1)

Basics of VBA / Macros Chapter – 80 (Userform Part-1) 1
⏱ 6 min read

This practical VBA exercise explains one small technique at a time. Work in a copy of your workbook, use Alt + F11 to open the Visual Basic Editor, and test the macro on a small sample before running it on live data.

In this article
  1. Before you run the code
  2. What this exercise covers
  3. Test, adapt and continue
  4. Continue learning

Before you run the code

  • Save a macro-enabled copy of the workbook (.xlsm).
  • Use Option Explicit in a standard module to catch misspelled variable names.
  • Read the comments and adjust sheet names, ranges and examples to fit your own file.

What this exercise covers

This chapter teaches you how to create anΒ Excel VBA Userform. The Userform we are going to create looks as follows:

Advertisement

Excel VBA Userform

Add the Controls

To add the controls to the Userform, execute the following steps.

1. Open theΒ Visual Basic Editor. If the Project Explorer is not visible, click View, Project Explorer.

2. Click Insert, Userform. If the Toolbox does not appear automatically, click View, Toolbox. Your screen should be set up as below.

Userform Screen Setup in Excel VBA

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.

Control Name Caption
Userform DinnerPlannerUserForm Dinner Planner
Text Box NameTextBox  
Text Box PhoneTextBox  
List Box CityListBox  
Combo Box DinnerComboBox  
Check Box DateCheckBox1 June 13th
Check Box DateCheckBox2 June 20th
Check Box DateCheckBox3 June 27th
Frame CarFrame Car
Option Button CarOptionButton1 Yes
Option Button CarOptionButton2 No
Text Box MoneyTextBox  
Spin Button MoneySpinButton  
Command Button OKButton OK
Command Button ClearButton Clear
Command Button CancelButton Cancel
7 Labels No need to change Name:, Phone Number:, etc.

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.

Show the Userform

To show the Userform, place aΒ command buttonΒ on your worksheet and add the following code line:

PrivateΒ SubΒ CommandButton1_Click()

DinnerPlannerUserForm.Show

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.

1. Open theΒ Visual Basic Editor.

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.

4. Add the following code lines:

PrivateΒ SubΒ UserForm_Initialize()

‘Empty NameTextBox
NameTextBox.Value = “”

‘Empty PhoneTextBox
PhoneTextBox.Value = “”

‘Empty CityListBox
CityListBox.Clear

‘Fill CityListBox
WithΒ CityListBox
.AddItem “San Francisco”
.AddItem “Oakland”
.AddItem “Richmond”
EndΒ With

‘Empty DinnerComboBox
DinnerComboBox.Clear

‘Fill DinnerComboBox
WithΒ DinnerComboBox
.AddItem “Italian”
.AddItem “Chinese”
.AddItem “Frites and Meat”
EndΒ With

‘Uncheck DataCheckBoxes

DateCheckBox1.Value =Β False
DateCheckBox2.Value =Β False
DateCheckBox3.Value =Β False

‘Set no car as default
CarOptionButton2.Value =Β True

‘Empty MoneyTextBox
MoneyTextBox.Value = “”

‘Set Focus on NameTextBox
NameTextBox.SetFocus

EndΒ Sub

Text boxes are emptied, list boxes and combo boxes are filled, check boxes are unchecked, etc.

Assign the Macros

We have now created the first part of the Userform. Although it looks neat already, nothing will happen yet when we click the command buttons on the Userform.

1. Open theΒ Visual Basic Editor.

2. In the Project Explorer, double click on DinnerPlannerUserForm.

3. Double click on the Money spin button.

4. Add the following code line:

PrivateΒ SubΒ MoneySpinButton_Change()

MoneyTextBox.Text = MoneySpinButton.Value

EndΒ Sub

This code line updates the text box when you use the spin button.

5. Double click on the OK button.

6. Add the following code lines:

PrivateΒ SubΒ OKButton_Click()

DimΒ emptyRowΒ AsΒ Long

‘Make Sheet1 active
Sheet1.Activate

‘Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range(“A:A”)) + 1

‘Transfer information
Cells(emptyRow, 1).Value = NameTextBox.Value
Cells(emptyRow, 2).Value = PhoneTextBox.Value
Cells(emptyRow, 3).Value = CityListBox.Value
Cells(emptyRow, 4).Value = DinnerComboBox.Value

IfΒ DateCheckBox1.Value =Β TrueΒ ThenΒ Cells(emptyRow, 5).Value = DateCheckBox1.Caption

IfΒ DateCheckBox2.Value =Β TrueΒ ThenΒ Cells(emptyRow, 5).Value = Cells(emptyRow, 5).Value & ” ” & DateCheckBox2.Caption

IfΒ DateCheckBox3.Value =Β TrueΒ ThenΒ Cells(emptyRow, 5).Value = Cells(emptyRow, 5).Value & ” ” & DateCheckBox3.Caption

IfΒ CarOptionButton1.Value =Β TrueΒ Then
Cells(emptyRow, 6).Value = “Yes”
Else
Cells(emptyRow, 6).Value = “No”
EndΒ If

Cells(emptyRow, 7).Value = MoneyTextBox.Value

EndΒ Sub

First, we activate Sheet1. Next, we determine emptyRow. The variable emptyRow is the first empty row and increases every time a record is added. Finally, we transfer the information from the Userform to the specific columns of emptyRow.

7. Double click on the Clear button.

8. Add the following code line:

PrivateΒ SubΒ ClearButton_Click()

CallΒ UserForm_Initialize

EndΒ Sub

Explanation: this code line calls theΒ Sub UserForm_InitializeΒ when you click on the Clear button.

9. Double click on the Cancel Button.

10. Add the following code line:

PrivateΒ SubΒ CancelButton_Click()

Unload Me

EndΒ Sub

This code line closes the Userform when you click on the Cancel button.

Test the Userform

Exit the Visual Basic Editor, enter the labels shown below into row 1 and test the Userform.

Result:

Test the Userform

Test, adapt and continue

Run this example on a few known cells first and compare the result with a manual check. Once it works, replace hard-coded references with named ranges or clear input cells so the macro is safer to reuse.

This post is part of the Excel VBA learning series. For formula-led alternatives and related spreadsheet techniques, see our Excel guides.

⚑ Interactive Practice Lab & Formula Tester:

Test formulas dynamically in our web formula laboratory or download the companion Excel workbook.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free Β· AI can be wrong