Upcoming SlideShare
×

# Ma3696 Lecture 2

359
-1

Published on

1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total Views
359
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
0
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Ma3696 Lecture 2

1. 1. Getting Started with UserformsLECTURE 2
2. 2. More on using named ranges in VBA Userforms and their… Controls and Events Properties of userform controls Coding controls within VBA SUMMARY
3. 3. NAMED RANGES
4. 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. 5.  We can use code to refer to any specific Column, or Row NAMED RANGES Range(“StockPrices”)
6. 6.  We can use code to refer to any specific Column, or Row NAMED RANGES Range(“StockPrices”).Columns(1)
7. 7.  We can use code to refer to any specific Column, or Row NAMED RANGES Range(“StockPrices”).Columns(2)
8. 8.  We can use code to refer to any specific Column, or Row NAMED RANGES Range(“StockPrices”).Columns(3)
9. 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. 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. 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. 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
13. 13. The Basics USERFORMS
14. 14. INSERT A USERFORM User Interface Use these controls to construct the UserForm
15. 15. Put the following 4 controls on your userform Label Textbox Combobox Command button INSERT A USERFORM
16. 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. 17. Can’t see the properties window? SHOW THE PROPERTIES WINDOW
18. 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
19. 19. PROPERTIES WINDOW Properties Window for UserForm1 The caption is what the user sees. Change the caption on your useform to your name
20. 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. 21. MODIFY THE DESIGN Change the BackColor. Change the BackColor of: • Your userform, or • One of your controls
23. 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. 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. 25. A FEW CONTROLS Label • Label elements on the Userform. • Insert instructions for the user.
26. 26. A FEW CONTROLS Textbox • Allow the user to enter information
27. 27. A FEW CONTROLS Combobox • Drop-down list of choices. • User selects one item from list.
28. 28. A FEW CONTROLS CommandButton • Write code to execute when clicked. • Use for Ok, Next, Cancel, Exit, back, Clear form, etc.
29. 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. 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. 31.  Enter the code below into this procedure  Run your userform  Click on the userform. What happened? PROCEDURES FOR A USERFORM
32. 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. 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. 34.  Enter the code below into this procedure  Run your userform  Click on the command button. What happened? PROCEDURES FOR A USERFORM
35. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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!
46. 46. INITIALISE A USERFORM
47. 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. 48. Code goes in a specific procedure USERFORM INITIALISE Choose Userform from the Select Initialise from the
49. 49. USERFORM INITIALISE Any code I put here will run BEFORE the user sees the userform
50. 50. From within your VBA code CODING CONTROLS
51. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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)
63. 63. THE END