Anda di halaman 1dari 15

A COMPREHENSIVE GUIDE TO PROGRAMMING USERFORMS IN EXCEL 2010

Allan Tan
FIRST EDITION 2013

CONTENTS
INTRODUCTION (2) USERFORMS (3) LABEL (12) TEXTBOX (13) COMMANDBUTTON (15) TEXTBOX PROPERTIES (19) USERFORM TOOLBAR (22) CONTROLLING THE USERFORM (25) MANIPULATING TEXTBOX DATA (26) MANIPULATING LABEL DATA (28) OPTION BUTTONS (30) FRAMES (34) CHECKBOX (36) MULTIPAGE (38) TABSTRIP (41) LISTBOX (46) COMBOBOX (56) SCROLLBARS (58) SPINBUTTON (62) IMAGEBOX (63) REFEDIT (65) MULTIPLE USERFORMS (69) COMMON PROPERTIES OF CONTROLS (70) USERFORM EVENTS (72)

VB FOR EXCEL - USERFORMS by Allan Tan

Page 1

INTRODUCTION
This book was written for those who already know how to write VBA programs in Excel, but do not know how to program Userforms, or are not completely knowledgeable in it. Information and ideas contained herein are my original ideas. No unnecessary details had been added. At times you may not be able to run your program due to a security setting in Excel. Anytime you need to enable macros, do this:

Happy learning!! Allan Tan Singapore 8888allan@gmail.com

VB FOR EXCEL - USERFORMS by Allan Tan

Page 2

USERFORMS
Besides programming macros and controls on the Excel spreadsheet, we can use a form (known as a userform) instead. On this userform, we can add some useful controls, and allow the user to enter data or make choices, and then transfer data and/or process data accordingly.

1. Insert a Userform A userform needs to be inserted. You can insert as many userforms as you need. The first form will be named userform1, the second userform2, etc. You can rename the userform if you wish. Each form will have a listing on the Explorer. Hit Alt-F11 if you are not at the VBE screen, or click this icon at the Developer Tab.

Click [Insert] [Userform].

VB FOR EXCEL - USERFORMS by Allan Tan

Page 3

A new userform looks like this:

Icon added to explorer; Click this to display the userform if it is not shown

Userform1 use the handles to change it size.

Userform toolbox If this disappears, select userform to display it

VB FOR EXCEL - USERFORMS by Allan Tan

Page 4

Properties Window The Userform has many properties shown in the Properties Window.

This name can be changed; it is used in programming codes All the properties here can be set from this window, or can be dynamically changed in your program.

VB FOR EXCEL - USERFORMS by Allan Tan

Page 5

Backcolor Property (Background Color) Select Backcolor Click dropdown arrow Select Palette tab Select color Code example: Userform1.backcolor = vbblue You can use the vb colors or the RGB() function to specify the desired color.

Caption Property This is the Title displayed on the Userform. Change it to your desired texts.

Code example: Userform1.caption = Title

VB FOR EXCEL - USERFORMS by Allan Tan

Page 6

Font Property This allows you to set the font, font type, and font size.

Code example: UserForm1.Font = "Impact" Forecolor Property This allows you to set the color of the font. Steps are the same as for Backcolor Property.

VB FOR EXCEL - USERFORMS by Allan Tan

Page 7

Picture Property This allows you to set an image as the background of the userform. The image must be existing. Take note of the accepted file types.

After an image has been added, the Picture property will show [Bitmap]. To remove the images, delete the word [Bitmap]. Code: Userform1.picture = \path\image.jpg (You must specify the path yourself) After adding a picture, you can set these properties to control some parameters of the image:

There are 4 options for PictureAlignment:

VB FOR EXCEL - USERFORMS by Allan Tan

Page 8

Code Example: Userform1.PictureAlignment = 3 There are 3 options for PictureSizeMode:

Code Example: Userform1.PictureSizeMode = 3 PicutreTiling has a Boolean property True or False:

Code Example: Userform1.PictureTiling = True

Form StartupPosition Property This allows you to specify exactly where on the screen you want the form to be displayed. Choose one of the four options.

Code example: UserForm1.startupposition = 3 (Must be placed at userform Activate event; value can be 0,1,2, or 3)

VB FOR EXCEL - USERFORMS by Allan Tan

Page 9

Form Width and Height Properties Use these to determine or set the width and height of your form. Measurement is in pixels. Code Example: UserForm1.Width = 800 UserForm1.Height = 1000 Form Scrollbars Property Set it to none, horizontal, vertical or both. Useful only when your form is too small to display all the controls in one screen.

Code Example: Userform1.scrollbars = 3 (value can be 0,1,2,3)

VB FOR EXCEL - USERFORMS by Allan Tan

Page 10

LABEL
The first control we will add is known as a label. A label is used to display texts (or words) such as titles, instructions, etc, on the form. It can also accept data sent to it from your programming codes. For instance, you can get data from a cell and display it on a Label. Look for on the toolbox. Click it once. Drag a rectangle anywhere on the form (any size). It should look like this, with the handles visible:

Enter desired texts into the Caption property in the Property Window Study the property window and check out some of the common properties you can change, such as backcolor, forecolor, etc. One important property you need to take note is its name this need to be used in your programming codes. You can add as many Labels as you want. If you intend to have a few labels with a few similar properties (such as font size, color, etc), you can use copy-and-paste. A form with four labels added.

VB FOR EXCEL - USERFORMS by Allan Tan

Page 11

TEXTBOX
Textboxes are usually used for user input, but they can also be used to accept data, such as from a cell in a worksheet. Draw one onto the form. Note that its name by default is Textbox1. The next one will be Textbox2, and so on. You can change this name. These names are used in your programming codes. Change its text property to display the desired texts. Look for on the Toolbox.

A userform with four labels and three textboxes.

Running the Form Click the Play icon, or hit F5, to run the userform.

VB FOR EXCEL - USERFORMS by Allan Tan

Page 12

Once run, a userform looks like this, with data entered.

If on clicking Run you see this screen, close it, click the form, then click Run again.

Transferring Data from Textboxes to Worksheet One most likely objective of having a userform is to capture data from the user, and then transferring these data to a worksheet. For this purpose, we need a Commandbutton, in which we will add our programming codes.

VB FOR EXCEL - USERFORMS by Allan Tan

Page 13

To get the full version, email 8888allan@gmail.com Upon payment using an agreed medium, the file will be sent to you via email.

VB FOR EXCEL - USERFORMS by Allan Tan

Page 14