18
A COMPREHENSIVE GUIDE TO PROGRAMMING USERFORMS IN EXCEL 2010 Allan Tan FIRST EDITION 2013

Comprehensive Guide to Excel Userforms FREE

Embed Size (px)

DESCRIPTION

A free sample of my book on programming Excel Userforms, written in simple layman terms for those who already know how to write programs in VBA.

Citation preview

Page 1: Comprehensive Guide to Excel Userforms FREE

ACOMPREHENSIVE GUIDE

TOPROGRAMMING

USERFORMSIN

EXCEL 2010

Allan TanFIRST EDITION

2013CONTENTS

INTRODUCTION (2)USERFORMS (3)LABEL (12)

Page 2: Comprehensive Guide to Excel Userforms FREE

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)

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:

VB FOR EXCEL - USERFORMS by Allan Tan Page 1

Page 3: Comprehensive Guide to Excel Userforms FREE

Happy learning!!

Allan [email protected]

VB FOR EXCEL - USERFORMS by Allan Tan Page 2

Page 4: Comprehensive Guide to Excel Userforms FREE

USERFORMSBesides 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

Page 5: Comprehensive Guide to Excel Userforms FREE

A new userform looks like this:

VB FOR EXCEL - USERFORMS by Allan Tan Page 4

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

Userform1use the handlesto change it size.

Userform toolboxIf this disappears,select userform to display it

Page 6: Comprehensive Guide to Excel Userforms FREE

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

VB FOR EXCEL - USERFORMS by Allan Tan Page 5

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.

Page 7: Comprehensive Guide to Excel Userforms FREE

Backcolor Property (Background Color)

Caption PropertyThis 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

Select BackcolorClick dropdown arrowSelect Palette tabSelect color

Code example:Userform1.backcolor = vbblue

You can use the vb colors or theRGB() function to specify the desired color.

Page 8: Comprehensive Guide to Excel Userforms FREE

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

Code example:UserForm1.Font = "Impact"

Forecolor PropertyThis 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

Page 9: Comprehensive Guide to Excel Userforms FREE

Picture PropertyThis 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

Page 10: Comprehensive Guide to Excel Userforms FREE

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 PropertyThis 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

Page 11: Comprehensive Guide to Excel Userforms FREE

Form Width and Height PropertiesUse 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 PropertySet 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

Page 12: Comprehensive Guide to Excel Userforms FREE

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 WindowStudy 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

Page 13: Comprehensive Guide to Excel Userforms FREE

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

Page 14: Comprehensive Guide to Excel Userforms FREE

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 WorksheetOne 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

Page 15: Comprehensive Guide to Excel Userforms FREE

To get the full version, email [email protected]

Upon payment using an agreed medium, the file will be sent to you via email.

VB FOR EXCEL - USERFORMS by Allan Tan Page 14