This document provides an overview of using userforms in VBA. It discusses named ranges and how to reference specific columns and rows. It demonstrates how to insert controls on a userform, modify their properties, and code events for each control. Procedures are used to run code when the userform initializes or controls are interacted with. Common controls like labels, textboxes, combo boxes and command buttons are described along with how to populate their values. Understanding these concepts is necessary to build interactive userforms using VBA.
4. What if I have a large range, with…
Many rows and
Many columns?
But I want do calculations with…
Just ONE column, OR
Just ONE row?
NAMED RANGES
5. We can use code to refer to any specific
Column, or
Row
NAMED RANGES
Range(“StockPrices”)
6. We can use code to refer to any specific
Column, or
Row
NAMED RANGES
Range(“StockPrices”).Columns(1)
7. We can use code to refer to any specific
Column, or
Row
NAMED RANGES
Range(“StockPrices”).Columns(2)
8. We can use code to refer to any specific
Column, or
Row
NAMED RANGES
Range(“StockPrices”).Columns(3)
9. Let’s find the average of Stock 2
USING FUNCTIONS WITH RANGES
Cells(13, 2) = Application.Worksheetfunction.Average(Range(“StockPrices”).Columns(2))
The cell I want to put
the average in
10. Let’s find the average of Stock 2
USING FUNCTIONS WITH RANGES
Cells(13, 2) = Application.Worksheetfunction.Average(Range(“StockPrices”).Columns(2))
I want VBA to evaluate
the Average using
Excel’s built-in function
11. Let’s find the average of Stock 2
USING FUNCTIONS WITH RANGES
Cells(13, 2) = Application.Worksheetfunction.Average(Range(“StockPrices”).Columns(2))
The range I want to find
the average of:
Column 2 (Stock 2)
Of “StockPrices”
12. Download Lecture 2 Student Example.xlsm
Use Module 1
Use procedure “AverageStockPrices”
Use built-in functions to calculate the average of
stock 1 and stock 3 and output the result in row
13, just below the prices for each stock.
EXERCISE. FIND THE AVERAGE OF STOCK 3
15. Put the following 4 controls on your userform
Label
Textbox
Combobox
Command button
INSERT A USERFORM
16. Single click on the userform
The properties window should be
visible (bottom left of screen).
Single click on your controls
The properties window will change
PROPERTIES WINDOW
17. Can’t see the properties window?
SHOW THE PROPERTIES WINDOW
18. The userform has its own properties
Each control has its own properties
We can modify these properties…
Using the Properties window, or
Using code in VBA
PROPERTIES
For example
To set the Height property of
UserForm1 to 250, either…
• Change the height in the
properties window (right)
to 250, or
• Write this in your code:
UserForm1.Height = 250
20. PROPERTIES WINDOW
Properties Window for UserForm1
The (Name) of the userform
within your VBA Code
Change the (Name) of your
useform to Userform_Lec2
21. MODIFY THE DESIGN
Change the
BackColor.
Change the BackColor of:
• Your userform, or
• One of your controls
23. Run your userform (F5)
Click on your controls. Do they do anything?
No.
Nothing will happen until you write code to tell VBA what
to do with each control.
This is important! VBA does not know what you want to
do. It cannot read your mind. Just because you label a
command button ‘Calculate’ does not mean it will calculate
anything. You have to tell VBA what to do.
Exit the userform
RUN A USERFORM
24. Choose one control
Disable it using the Enabled property
Choose a different control
Make it invisible using the Visible property
Run your userform again.
Check the controls that were disabled or made
invisible. Did it work?
Enable and make Visible all controls
MODIFY ATTRIBUTES OF CONTROLS
25. A FEW CONTROLS
Label
• Label elements on the
Userform.
• Insert instructions for
the user.
28. A FEW CONTROLS
CommandButton
• Write code to execute
when clicked.
• Use for Ok, Next, Cancel,
Exit, back, Clear form, etc.
29. To access the Form Module for a userform:
Double click on the userform, OR
Double click ANY control that’s on the userform
PROCEDURES FOR A USERFORM
I double clicked a
UserForm
30. To access the Form Module for a userform:
Double click on the userform, OR
Double click ANY control that’s on the userform
PROCEDURES FOR A USERFORM
Any code I put here will run when
the userform is clicked
31. Enter the code below into this procedure
Run your userform
Click on the userform. What happened?
PROCEDURES FOR A USERFORM
32. To access the Form Module for a userform:
Double click on the userform, OR
Double click ANY control that’s on the userform
PROCEDURES FOR A USERFORM
I double clicked
CommandButton1
33. To access the Form Module for a userform:
Double click on the userform, OR
Double click ANY control that’s on the userform
PROCEDURES FOR A USERFORM
Any code I put here will run when
CommandButton1 is clicked
34. Enter the code below into this procedure
Run your userform
Click on the command button. What happened?
PROCEDURES FOR A USERFORM
35. To access the Form Module for a userform:
Double click on the userform, OR
Double click ANY control that’s on the userform
PROCEDURES FOR A USERFORM
I double clicked
Label1
36. To access the Form Module for a userform:
Double click on the userform, OR
Double click ANY control that’s on the userform
PROCEDURES FOR A USERFORM
Any code I put here will run when
Label1 is clicked
37. To access the Form Module for a userform:
Double click on the userform, OR
Double click ANY control that’s on the userform
PROCEDURES FOR A USERFORM
I double clicked
Combobox1
38. To access the Form Module for a userform:
Double click on the userform, OR
Double click ANY control that’s on the userform
PROCEDURES FOR A USERFORM
Any code I put here will run when
ComboBox1 is changed
39. To access the Form Module for a userform:
Double click on the userform, OR
Double click ANY control that’s on the userform
PROCEDURES FOR A USERFORM
Any code I put here will run when the drop-
down menu of ComboBox1 is clicked
40. To access the Form Module for a userform:
Double click on the userform, OR
Double click ANY control that’s on the userform
PROCEDURES FOR A USERFORM
Any code I put here will run when the drop-
down menu of ComboBox1 is clicked
41. Enter the code below into this procedure
Run your userform
Click on the userform. What happened?
PROCEDURES FOR A USERFORM
MsgBox (“You clicked drop-down menu”)
42. Select any control from the Control List
Select under what condition you want the code to
run from the Even List
CHOOSING EVENTS
The
The
43. For example,
If I want code to run when the userform starts, then I
choose:
Userform from the Controls List
Initialise from the Event List
If I want code to run when a button is clicked, then I
choose:
The specific command button from the Controls List
Click from the Event List
Most common mistake:
I want to add items to my combo box, so I choose:
Combobox from the Controls List
Change from the Event List
CHOOSING EVENTS
44. For example,
If I want code to run when the userform starts, then I
choose:
Userform from the Controls List
Initialise from the Event List
If I want code to run when a button is clicked, then I
choose:
The specific command button from the Controls List
Click from the Event List
Most common mistake:
I want to add items to my combo box, so I choose:
Combobox from the Controls List
Change from the Event List
CHOOSING EVENTS
WRONG!
45. Most common mistake:
I want to add items to my combo box, so I choose:
Combobox from the Controls List
Change from the Event List
Why is this wrong?
The combobox won’t show the items until AFTER the user
clicks on it and changes it’s value…
But, the user can’t do this because it will be empty!
Correct way to do it:
Adding items should be done before the user sees the
userform (so they can make a selection)
Userform from the Controls List
Initialise from the Event List
CHOOSING EVENTS
WRONG!
47. Prepare the Userform for the user to use:
Clear TextBoxes.
Fill ComboBoxes (the drop-down menu)
Give ComboBoxes an initial value (e.g. “<Select One>”)
Make sure labels have captions (i.e., they say something)
Chose an initial OptionButton
Deselect all CheckBoxes (or choose an initial one)
Populate ListBoxes
Put the cursor in the first box you want the user to enter
information.
Etc…
INITIALISE A USERFORM
48. Code goes in a specific procedure
USERFORM INITIALISE
Choose Userform from
the
Select Initialise from
the
51. Give a textbox a value
TextBox1.value = “Hello”
TextBox2.value = 17
ASSIGNING VALUES WITHIN VBA
Use .value after
the (Name) of
the textbox
52. Give a Label a value
Label1.caption = “Hello”
Label2.caption = 17
ASSIGNING VALUES WITHIN VBA
Use .caption
after the (Name)
of the Label
53. Add items to a combo box
ComboBox1.AddItem “Item ONE”
ComboBox1.AddItem “Item TWO”
ASSIGNING VALUES WITHIN VBA
BEFORE running the code AFTER running the code AFTER clicking drop-down
menu
Use .AddItem
after the (Name)
of the combobox
54. If you want to add multiple items you can also use
A named range. The range MUST be a column in Excel.
Or a With statment:
ASSIGNING VALUES WITHIN VBA
Type text in “ ” or use items from a named range
55. Add items to a combo box
Use .AddItem property, or
Use .RowSource property
ASSIGNING VALUES WITHIN VBA
BEFORE running the code AFTER running the code AFTER clicking drop-down
menu
Should a this be blank? Will the user know what to do?
56. Show an initial value in a combobox
ComboBox1.value = “<Select One>”
ASSIGNING VALUES WITHIN VBA
BEFORE running the code AFTER running the code AFTER clicking drop-down
menu
57. Which combobox item was selected?
ComboBox1.ListIndex
ASSIGNING VALUES WITHIN VBA
AFTER clicking drop-down
menu
-1
0
1
VBA counts the initial value as item -1 in the list
If no item was selected the value of
Combobox1.ListIndex would be -1
58. Which combobox item was selected?
ComboBox1.ListIndex
ASSIGNING VALUES WITHIN VBA
AFTER clicking drop-down
menu
-1
0
1
VBA counts the 1st list item as item 0
If “ItemONE” was selected the value of
Combobox1.ListIndex would be 0
59. Which combobox item was selected?
ComboBox1.ListIndex
ASSIGNING VALUES WITHIN VBA
AFTER clicking drop-down
menu
-1
0
1
VBA counts the 2nd list item as item 1
If “ItemTWO” was selected the value of
Combobox1.ListIndex would be 1
60. Which combobox item was selected?
ComboBox1.ListIndex
ASSIGNING VALUES WITHIN VBA
AFTER clicking drop-down
menu
AFTER selecting an item
from the drop-down
The value of
Combobox1.ListIndex
is 1
-1
0
1
61. Which combobox item was selected?
ComboBox1.ListIndex
ASSIGNING VALUES WITHIN VBA
AFTER clicking drop-down
menu
What would be the value of
ComboBox1.ListIndex if “ItemFOUR”
was selected?
62. You are ready to move on when…
LO6: You can write code to use a specific row or column of a named
range. E.g., apply a built-in function to a specific row or column of
named range rather than the entire range.
LO7: You can add controls to a userform and edit their properties via
the properties window. You also understand that properties of a
control or userform can be changed within the code.
LO8: You can describe the purpose and use of the following controls:
label, textbox, combobox and command button. You can also
correctly choose which of these controls should be used on a
userform based on the requirements of the program.
LO9: You understand that code must be written in the correct
procedure. In addition, you can choose the correct control (from the
Control List) and event (from the Event List) when creating a userform
procedure.
These LOs will be practiced in the lab.
LEARNING OUTCOMES (LO)