Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 106 Computing Fundamentals II Chapter 29 “Creating A User Form” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim from CS 106.

Similar presentations


Presentation on theme: "1 CS 106 Computing Fundamentals II Chapter 29 “Creating A User Form” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim from CS 106."— Presentation transcript:

1 1 CS 106 Computing Fundamentals II Chapter 29 “Creating A User Form” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim from CS 106 material developed by CS professors: Cynthia Brown & Robert Martin

2 2 Syllabus A Better Interface A Better Interface Goal Goal Windows Version... detailed Windows Version... detailed Change Name Change Name Properties Properties Mac Version... Abbreviated, but see video Mac Version... Abbreviated, but see video Showing Form Showing Form Viewing Code Viewing Code

3 3 A Better Interface Our programs can use input boxes for input and write on the spreadsheet page for outputOur programs can use input boxes for input and write on the spreadsheet page for output This works ok but is not very elegant or flexibleThis works ok but is not very elegant or flexible VBA gives us a way to create a custom form, known as the User Form, to use as an interface for your programVBA gives us a way to create a custom form, known as the User Form, to use as an interface for your program We’ll skim the Windows version, then look in detail at the Mac versionWe’ll skim the Windows version, then look in detail at the Mac version

4 4 Goal Presentation “Create_User_Form” is paired with “Add_Control_2_User_Form”Presentation “Create_User_Form” is paired with “Add_Control_2_User_Form” Overall goal is to show how to enter a string and then display the concatenated string in these steps:Overall goal is to show how to enter a string and then display the concatenated string in these steps: 1 A label prompts user to enter a string1 A label prompts user to enter a string 2 A text box reads this string input, e.g. “Cindy”2 A text box reads this string input, e.g. “Cindy” 3 A button, when subsequently pushed, causes the string concatenation and display event3 A button, when subsequently pushed, causes the string concatenation and display event 4 A second text box displays “This is: Cindy!”4 A second text box displays “This is: Cindy!”

5 5 WINDOWS VERSION

6 6 To Get Started… Open a new workbook, go to the Developer tab, and click Visual Basic. In VBA, double click ThisWorkBook in the project window to show the code sheet, and type OptionExplicit. Then click the Insert UserForm icon, and choose UserForm

7 7 Things to Notice Your new user form. Grab the bottom corner and drag to make it bigger. The toolbox has items you can put on the user form. If you don’t see it, click the toolbox icon (highlight ed above) Properties of the selected form or tool

8 8 Change the Name of the Form Name is the first property In the list. We will refer to the form by its name in our program.

9 9 Change the Caption Captio n propert y Here’s the caption. The user will see it when using the form. The user will not see the name.

10 10 Other Properties There are plenty of other properties to play with. You can try them out.There are plenty of other properties to play with. You can try them out. One fun one is the background color. Let’s set it to something other than white…One fun one is the background color. Let’s set it to something other than white…

11 11 User Form Made Yellow The BackC olor propert y The colors are reached through the little down arrow at the right of the property line. System colors are the ones Windows is using for your system. The Palette gives you some other options.

12 12 Showing the Form Unless you make it happen, the form won’t show up in your spreadsheet programUnless you make it happen, the form won’t show up in your spreadsheet program The line of code that makes the form show up isThe line of code that makes the form show up is formName.ShowformName.Show Let’s make it show up when you open the workbook, using the Workbook_Open() event procedure.Let’s make it show up when you open the workbook, using the Workbook_Open() event procedure. Double click in the project window to get back to the ThisWorkbook project, and type the code. Then close the workbook and reopen itDouble click in the project window to get back to the ThisWorkbook project, and type the code. Then close the workbook and reopen it

13 13 Here is the code…

14 14 Screen, after Opening Workbook

15 15 MAC VERSION

16 16 To Get Started Go to the Developer tab and then to the VBA editor. Under the InsertMenu, click Userform.

17 17 The new userfor m Controls you can add to the userform Properties window showing Userform properties Showing it In the project

18 18 Changing the Properties Use the properties window to change the propertiesUse the properties window to change the properties For each item, a separate properties window opens, but only after being clicked = selected!For each item, a separate properties window opens, but only after being clicked = selected! We’ll change the name to frmExampleWe’ll change the name to frmExample And we’ll make it bigger by changing the height to 450 and width to 600And we’ll make it bigger by changing the height to 450 and width to 600 We’ll also change the caption to “Welcome”We’ll also change the caption to “Welcome” And the backcolor to yellowAnd the backcolor to yellow

19 19 I couldn’t find a yellow I liked on the color wheel, so I used the “crayons” to select one

20 20 Here is the yellow form after I clicked OK.

21 21 Showing Form Right now there is nothing that makes the form show up when you are in the workbook.Right now there is nothing that makes the form show up when you are in the workbook. The command frmExample.Show makes it visibleThe command frmExample.Show makes it visible We could put this in a macro that is activated by clicking a button, but let’s put it in the special Workbook_Open() macro that makes it show up automatically, as a side-effect of the opening event, when you open the workbook.We could put this in a macro that is activated by clicking a button, but let’s put it in the special Workbook_Open() macro that makes it show up automatically, as a side-effect of the opening event, when you open the workbook.

22 22 I double clicked here to get the code window for this workbook I typed my code here

23 23 Viewing Code Option Explicit '***************************************** ' Show form frmExample when the workbook opens '***************************************** Sub Workbook_Open() frmExample.Show frmExample.Show End Sub

24 24 Save and quit… Save the workbook as a Macro-enabled (.xlsm) workbook and then quit ExcelSave the workbook as a Macro-enabled (.xlsm) workbook and then quit Excel The next slide shows the re-opened workbook with enabled macrosThe next slide shows the re-opened workbook with enabled macros

25 25 The user form caption


Download ppt "1 CS 106 Computing Fundamentals II Chapter 29 “Creating A User Form” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim from CS 106."

Similar presentations


Ads by Google