Top Banner
Creating a User Form
23

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.

Dec 14, 2015

Download

Documents

Ashton Cluett
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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.

Creating a User Form

Page 2: 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.

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

Page 3: 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.

WINDOWS VERSION

Page 4: 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.

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

Page 5: 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.

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

Page 6: 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.

Change the Name of the Form

Name is the first propertyIn the list.

We will refer to the form by its name in our program.

Page 7: 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.

Change the Caption

Captionproperty

Here’s the caption. The user will see it when using the form. The user will not see the name.

Page 8: 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.

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…

Page 9: 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.

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.

Page 10: 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.

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

Page 11: 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.

Here is the code…

Page 12: 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.

My screen, after opening the workbook and enabling macros

Page 13: 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.

MAC VERSION

Page 14: 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.

To Get Started

Go to the Developer tab and then to theVBA editor. Under the InsertMenu, click Userform.

Page 15: 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.

The new userform

Controls you canadd to the userform

Properties window showingUserform properties

Showing itIn the project

Page 16: 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.

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

Page 17: 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.

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

Page 18: 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.

Here is the yellow form after I clicked OK.

Page 19: 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.

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.

Page 20: 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.

I double clicked hereto get the code windowfor this workbook

I typed my code here

Page 21: 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.

The code I typed

Option Explicit'*****************************************' Show form frmExample when the workbook opens'*****************************************Sub Workbook_Open() frmExample.ShowEnd Sub

Page 22: 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.

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

Page 23: 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.

The user form

caption