Presentation on theme: "Creating a User Form. A Better Interface Our programs can use input boxes for input and write on the spreadsheet page for output This works ok but is."— Presentation transcript:
Creating a User Form
A Better Interface Our programs can use input boxes for input and write on the spreadsheet page for output This works ok but is not very elegant or flexible VBA gives us a way to create a custom form to use as an interface for our program We’ll look at the Windows version first, then the Mac version
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
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 (highlighted above) Properties of the selected form or tool
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.
Change the Caption Caption property Here’s the caption. The user will see it when using the form. The user will not see the name.
Other Properties 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…
I made it yellow The BackColor property 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.
Showing the Form Unless you make it happen, the form won’t show up in your spreadsheet program The line of code that makes the form show up is formName.Show 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. The close the workbook and reopen it
Here is the code…
My screen, after opening the workbook and enabling macros
To Get Started Go to the Developer tab and then to the VBA editor. Under the InsertMenu, click Userform.
The new userform Controls you can add to the userform Properties window showing Userform properties Showing it In the project
Changing the Properties Use the properties window to change the properties We’ll change the name to frmExample And we’ll make it bigger by changing the height to 450 and width to 600 We’ll also change the caption to “Welcome” And the backcolor to yellow
I couldn’t find a yellow I liked on the color wheel, so I used the “crayons” to select one
Here is the yellow form after I clicked OK.
Showing the form Right now there is nothing that makes the form show up when you are in the workbook. The 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 when you open the workbook.
I double clicked here to get the code window for this workbook I typed my code here
The code I typed Option Explicit '***************************************** ' Show form frmExample when the workbook opens '***************************************** Sub Workbook_Open() frmExample.Show End Sub
Save and quit… I saved my workbook as a Macro-enabled (.xlsm) workbook and then quit Excel The next slide shows what I got when I re- opened the workbook and enabled macros