You are on page 1of 32

Basic VBA

-Designing UserForms-
Objectives
• To be able to understand what UserForms are for

• To be able to understand the uses and importance of the tools used in

UserForms.

• To be able to create a UserForm

• To be able to create a button that would show a userform from a spreadsheet.


User Forms
User Form is the most useful function in VBA that will help you create
your own program. This allows you to create forms that interact with
your Excel File.

User Forms can be accessed through the Insert > User Form. Upon
clicking, you will be shown a User Form window and a Toolbox
User Forms - Properties
The properties of the User Form can be seen in the properties window.

The basic properties are the following: Name, Caption, and Font.

The Name property displays what VBA will remember the userform to be
called during coding.

The Caption property displays the name on the Title Bar of the user form

You could change the background settings through “Back Color” property or
even add a picture as a background with the “Picture” property.

The other functions are self explanatory, feel free to explore the properties.
User Forms - Toolbox

ComboBox: This creates a dropdown list of items


depending on what the programmer includes

TextBox: This creates a box where the user can


type a value. The User Form can harvest that
value to do other functions.

Label: This is used in putting text into the User


Form

Pointer: This is the pointer.


User Forms - Toolbox

ComboBox: This creates a dropdown list of items


depending on what the programmer includes

TextBox: This creates a box where the user can


type a value. The User Form can harvest that
value to do other functions.

Label: This is used in putting text into the User


Form

Pointer: This is the pointer.


User Forms - Toolbox
Toggle Button: This creates a button that can be
pressed, and will stay pressed, until you press it
again.
Option button: This creates a circle that when
pressed, signifies that you have selected this
option. Only one option button can be pressed per
frame available in the User Form
Check Box: This creates a box that a user can click.
A user can check more than one check box unlike
the option box.

List Box: List box is like a combo box but it displays


more than one entry. It is also not in the form of a
dropdown list.
User Forms - Toolbox
Multi Page: This creates a Page strip where pages
can be selected. This is useful in separating a group
of content from another.

Tab Strip: This creates a tab strip where tabs can be


selected. This is useful in separating a group of
content from another.

Command Button: This creates a button that when


pressed, something will immediately happen.
Frame: A frame isolates a part of the User Form. As
you can see on the image on the right, 2 option
buttons can be clicked since frame 1 and frame 2
are independent of each other.
User Forms - Toolbox

Image: This inserts an image

Spin Button: this creates a spin button that you can


press to go up or down
Scroll Bar: Inserts a scroll bar
Example 1
Situation:
We need to design a userform where a person
needs to enter his First Name, Last Name, Age
and Gender. This form will be used for new
participant record for an event

Step 1: Create a userform by going to


Insert>UserForm

Step 2: In the properties Window, change the


Name of the UserForm to “NewParticipant”

Step 3: in the properties window, change the


Caption of the NewParticipant User Form to
“Add New Participant”
Example 1
Step 4: In the Toolbox, select the “Label” icon

Step 5: Draw the label into the UserForm

Step 6: Click on Label 1. Change the caption of


Label1 in the Properties window. Type “First
Name”
Example 1
Step 7: Click on the TextBox icon in the Toolbox

Step 8: Draw the TextBox under the First Name


label.

Step 9: Follow steps 4 to 6 in creating the label


for the Last Name. (click icon, draw, change
caption)

Step 10: Follow Steps 7 and 8 in drawing the text


box under “Last Name”

Step 11: Follow steps 4 to 8 in creating the Label


and textbox for “Age”
Example 1
Step 12: Create the Label for Gender:

Step 13: click the “Option button” icon in the


tool box.

Step 14: Draw two option buttons below


gender:

Step 15: Change the caption of optionbutton1


to “Male” and optionbutton2 “Female:
Example 1
Step 16: Click the command button icon in the
toolbox

Step 17: Draw the command button at the


bottom right of the userform

Step 18: Change the caption of the command


button1 to “Add Participant”
Step 19: Press F5 or click the Play button
Congratulations, you have just designed your
first userform!
Example 2
Situation:
We need to design a UserForm where a person
can choose a certain value from a set of
options. This kind of designing technique is
useful for avaoiding a user to be typing just
whatever values into a TextBox.

Step 1: Create a userform by going to


Insert>UserForm

Step 2: In the properties Window, change the


Name of the UserForm to “NewEmployee”

Step 3: in the properties window, change the


Caption of the NewParticipant User Form to
“New Employee Data”
Example 2
Step 4: Create a label and caption it “Title”

Step 5: Click the “Combobox” button in the


toolbox.

Step 6: Draw the combobox beside the “Title”


label

Step 7: On Sheet 1, cells A1:A6 type the


following:
Example 2
Step 8: click on the combobox beside “Title”
and look for the “RowSource” in the properties
window.

Step 9: In the “RowSource” type: A1:A6

Step 10: Run the UserForm (press f5) and try to


click the combobox.

This is how you put options into a combobox


without coding them.

Step 11: Close the userform. Add the Label and


TextBox for “First Name” & “Last Name”
Example 2
Step 12: Create a label and caption it
“Department:” and create a combobox below
it.

Step 13: On cells B1:B6: type to following

Step 14: Click on the combobox below


department. On the properties window, look
for “RowSource” and type B1:B6

Step 15: on the bottom right part of the


userform, create a command button and
caption it “Add Employee”
Example 2
Step 16: Run the UserForm, and click on the
Combobox beside Title and choose Mrs.

Step 17: Click on the department combobox


and choose Logistics.

Congratulations, you have created your second


userform using comboboxes.
Example 3
Situation: We want to create a UserForm
where a user can choose between option
buttons in different criteria. Example: (1st
option: Male or Female. 2nd Option: Single or
Married). To do this, we will need to make use
of Frames and option buttons, as only one
option button can be chosen per frame.

Step 1: Create a userform and name it


“ChoosingOptions”. Caption it “Male/Female
and Single/Married”

Step 2: Click the frame icon in toolbox and


draw a frame in the user form.
Example 3
Step 3: Create another Frame below Frame 1

Step 4: Click on Frame 1 and change its caption


to “Sex”. Click on Frame 2 and change its
caption to “Marital Status”
Example 3
Step 5: Create two option buttons inside “Sex”.
One Male and one Female.

Step 6: Create two option buttons inside


“Marital Status”. One Single, One Married.

Step 7: Run the UserForm, Select Male and


Married.
Example 4
Situation: We want to create an ordering
window for a simple Burger and Hotdog Stand
through the use of toggle buttons and
checkboxes.

Step 1: Create a userform and name it


“OrderWindow”. Caption it “New Order”

Step 2: Create two Labels: “Food:” and


“Quantity”

Step 3: Click the toggle button on the toolbox,


and draw one toggle button below “Food”
Example 4
Situation: We want to create an ordering
window for a simple Burger and Hotdog Stand
through the use of toggle buttons and
checkboxes.

Step 1: Create a userform and name it


“OrderWindow”. Caption it “New Order”

Step 2: Create two Labels: “Food:” and


“Quantity”

Step 3: Click the toggle button on the toolbox,


and draw one toggle button below “Food”

Step 4: Create a textbox beside Toggle Button1


Example 4
Step 5: Click the checkbox icon on the toolbox
and draw two checkboxes below toggle button
1.

Step 6: Change the caption of Toggle Button 1


to “Burger”, checkbox1 to “Cheese”, checkbox
2 to “Tomato”

Step 7: Create another set of toggle buttons,


textbox, and checkboxes below the first one.

Step8 : Change the caption of togglebutton2 to


“Hotdog Sandwich”, checkbox3 to “Cheese”,
checkbox4 to “Ketchup”
Example 4
Step 9: Create a Command button at the
bottom part of the userform and caption it
“Create Order”

Step 10: Run the UserForm. Click on Hotdog


and input 3 orders with Cheese.
Making UserForms appear through
buttons
We want to make our userforms appear in just
a single click of a button, rather than opening
VBA and looking for the userform before
running it.

Here are the basic steps to do that:


1. Insert a module in VBA (if there is already
an existing module, this step can be
skipped.
2. Create a macro named “Show(insert name
of userform)
3. And in the macro, type:
NameOfUserForm.Show

4. Create a button in the sheet through the


use of the developer tab.
Making UserForms appear through
buttons
5. Draw the button in the Spreadsheet

6. After drawing, a window “Assign Macro” will


appear. Click the ShowOrderWindow then
press “OK”

Step 7: Change the button1 caption to “New


Order”

Step 8: click the button and see your UserForm.


Exercise 1
Create a User Form named “NewStudent” and
captioned “Add New Student”, wherein new
students can be recorded. The data to be
recorded are:

• First Name
• Last Name
• Age
• Gender (should be in the form of option
buttons)
• Year (1st to 5th Year, should be selected from a
combobox)

Create a button in the spreadsheet and name it


“New Student” that when pressed, will show
the NewStudent userform.
Exercise 1 (Answer)
Create a User Form named “NewStudent” and
captioned “Add New Student”, wherein new
students can be recorded. The data to be
recorded are:

• First Name
• Last Name
• Age
• Gender (should be in the form of option
buttons)
• Year (1st to 5th Year, should be selected from a
combobox)

Create a button in the spreadsheet and name it


“New Student” that when pressed, will show
the NewStudent userform.
Exercise 2
Create a User Form named “NewOrderWindow”
captioned “New Order”, which will capture the
details:

• Day: (to be selected using a combobox)


• Month: (to be selected using a combobox)
• Year: (to be selected using a combobox, until
2020)
• Food Choice made through toggle buttons.
Burrito, and Nachos.
• Burrito should have a checklist of ingredients included
(Jalapeno, Cheese, Onion)
• Nachos should have a checklist of ingredients included
(Lettuce, Cilantro, Jalapeno)
• The amount for each food must also be specified.

Create a button in the spreadsheet called “New


Order” so that when pressed, will show the
NewOrderWindow userform.
Exercise 2 (Answer)
Create a User Form named “NewOrderWindow”
captioned “New Order”, which will capture the
details:

• Day: (to be selected using a combobox)


• Month: (to be selected using a combobox)
• Year: (to be selected using a combobox, until
2020)
• Food Choice made through toggle buttons.
Burrito, and Nachos.
• Burrito should have a checklist of ingredients included
(Jalapeno, Cheese, Onion)
• Nachos should have a checklist of ingredients included
(Lettuce, Cilantro, Jalapeno)
• The amount for each food must also be specified.

Create a button in the spreadsheet called “New


Order” so that when pressed, will show the
NewOrderWindow userform.

You might also like