0% found this document useful (0 votes)
23 views21 pages

Excel VBA UserForms Guide

The document provides a comprehensive guide on creating and programming UserForms in Visual Basic for Applications (VBA) within Microsoft Excel 2016. It outlines the steps for designing forms, inserting controls, setting properties, and programming buttons to handle user input and data management. Additionally, it includes exercises for practical application, such as creating forms for data entry and login functionalities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views21 pages

Excel VBA UserForms Guide

The document provides a comprehensive guide on creating and programming UserForms in Visual Basic for Applications (VBA) within Microsoft Excel 2016. It outlines the steps for designing forms, inserting controls, setting properties, and programming buttons to handle user input and data management. Additionally, it includes exercises for practical application, such as creating forms for data entry and login functionalities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Session

7
Visual Basic for Applications
III
Forms Objects
Microsoft Excel 2016

INTRODUCTION
VBA forms are windows composed of controls, and designed to create systems.
to measure.

VBA forms, also called custom dialog boxes, in these windows,


we can associate all the commands reviewed earlier.

The form is a UserForm object and, like any other VBA object, it has its own
events, properties, and methods with which we can control its appearance and behavior.

The steps to design a form are as follows:

Insert the form


Insert the controls to design the window
Change the properties of the form and the controls
Encode the form
Link the form with a button in the spreadsheet.

Prof. Christian Montoya 2


Microsoft Excel 2016

INSERT A USERFORM
Enter the Visual Basic editor (press ALT + F11)

Click on the project I want to work on

After this procedure, the following object must be presented:

PROFESSOR CHRISTIAN MONTOYA 3


Microsoft Excel 2016

. A toolkit is activated in parallel, which is used to insert others.


objects to the form.

. If the toolbox is not active, we could activate it with the following


we can activate the procedure:

Prof. Christian Montoya 4


Microsoft Excel 2016

OBJECTS OF THE TOOLBOX

BUTTON NAME DEFINITION

It is useful to provide the user with a logical option: YES


Checkbox
TRUE or FALSE.

It is a drop-down list that presents only


Combined Panel
one element at a time.

It is the control that performs the actions in a


Command Button form, as well as close, validate, store
data among other activities.

It is useful for grouping controls, it can be used for


Frame Button aesthetic reasons or to logically group a
set of controls.

It is applied to insert an image of a product,


Image
of a user or the company logo.

Present a list of items, where the user


List Box
you can select one or multiple options.

It is useful for labeling what data we will insert in the


Label
text boxes.

Allows you to create a board with cards. To add


more chips than the already predetermined ones, click
Multiple Page
right-click on one of the cards and select New
Page.

It applies when the user only selects one.


Option Button
option of a set of alternatives.

It is one of the most used in a form, already


Text Box that these controls allow the user to enter
data.

PROF. CHRISTIAN MONTOYA 5


Microsoft Excel 2016

NAMING CONVENTIONS FOR OBJECTS


Objects should have names with a coherent prefix that facilitates identification of the
type of object. Below is a list of recommended conventions for
Some of the objects allowed by Visual Basic Application:

SUGGESTED PREFIXES FOR CONTROLS

TYPE OF CONTROL PREFIX EXAMPLE


Text box TXT Txtname
Combined frame CBO Cbdistrict
List box LST Product List
Option button OPT Optgenero
Checkbox CHK Chkaporte
Label LBL Lblcommission
Command button BTN Blnsalir
Image IMG Imglogo
Multiple page PGM Pgmdetalle
Frame button GRP Group shift

PROF. CHRISTIAN MONTOYA 6


Microsoft Excel 2016

PROPERTIES OF CONTROLS
Remember that each object has its properties, just like an object in real life.
We will see next how those properties are shown.

EXAMPLE
If you already have a form on screen, then click on the label control and draw it.
about the form as if it were a form in a Word 2013 document.
If you do not see the properties window, then follow these steps:

Inserted label control

PROF. CHRISTIAN MONTOYA 7


Microsoft Excel 2016

Below is the properties window, with all the


characteristics of the selected object (in this case, the label):

Next, we mention the most important properties:

Property Definition
Name Name used to program the object.
Caption It is used to set the text that will display the
control Label.
Autosize Las dimensiones del Label se ajustarán de acuerdo al
length and height of the text to display.
Backcolor Determine the background color of the label.
Font Change the font format (size and style).
Forecolor Edit the text color.
Height y Width Change the width and height of the Label.

PROF. CHRISTIAN MONTOYA 8


Microsoft Excel 2016

Exercise 1

Please create the following format in the spreadsheet:

What we are trying to achieve in this exercise is something simple, which is that the data from
form passed to the spreadsheet.

They will wonder, it's easier to type it and not make such a fuss, it's true, but when
work with more complex formats and have to store the data one below the other
On the other hand, those forms will fit perfectly there.

It is necessary that they also know that we can use the forms to
search for values in a list or table.

PROF. CHRISTIAN MONTOYA 9


Microsoft Excel 2016

Create the following form design with the different controls:

CHANGING ITS PROPERTIES

Objects Properties Value


Name Former students
Userform1
Caption Student Data
Name Lblname
Label1
Caption Name
Name Last name
Label2
Caption Last name
Name Career Label
Label3
Caption Race
Name Phone label
Label4
Caption Telephone
Text box 1 Name Txtname
Text box 2 Name Last name
Combined Picture Name cbocarrera
Text box 4 Name Txtphone
Name Gripper
Button1
Caption Record
Name Clear button
Button2
Caption Delete
Name Btncancelar
Button3
Caption Cancel

PROF. CHRISTIAN MONTOYA 10


Microsoft Excel 2016

Finally, the form should look like this:

When the form is displayed, the data in the control should already be visible.
circuit

In order for the data to be displayed when the form is executed, we need to program it.
in the eventInitialize.

If we want to document the code, it is advisable to add comments; for


we will use an apostrophe in front of the line, you will see later how the line will have
a green color.

PROF. CHRISTIAN MONTOYA 11


Microsoft Excel 2016

PROGRAMMING

ASSIGN DATA TO THE COMBINED BOX

Private Sub UserForm_Click()


this is a comment because it starts with an apostrophe
[Link] "Law"
[Link] "Accounting"
[Link] "Tourism"
[Link] "Networks"
[Link] "Systems"
[Link] "Mathematics"
[Link] "Administration"
[Link] "Educacion"
End Sub

WE PROGRAMMED THE DELETE BUTTON

By clicking this button, all controls must be blank. Let's begin to


program it by double-clicking on the control:

Private Sub btnborrar_Click()


[Link] = ""
[Link] = ""
[Link] = ""
[Link] = ""
[Link]
End Sub

PROGRAM THE CANCEL BUTTON

Private Sub btncancelar_Click()


End
End Sub

PROF. CHRISTIAN MONTOYA 12


Microsoft Excel 2016

PROGRAM THE RECORD BUTTON

Private Sub btngrabar_Click()


Range("d6").Value = txtnombre
Range("d8").Value = txtapellido
Range("d10").Value = [Link]
Range("d12").Value = txttelefono
End Sub

CONSIDERATIONS TO TAKE INTO ACCOUNT WHEN PROGRAMMING

Are you sure you insist on asking how we know that by typing TEXT I am doing
reference to a property, this is also easy to differentiate:

When you place the point, VBA should display a small window like the one that
follow

This is called a member list.

. The green icons represent the methods.

. The icons that have a hand grabbing a leaf are properties.

IMPORTANT

What happens if while you are typing, you put the period and the list does not display?
members, it is very likely that you have an error in the name of the object.

It is important to know the following: a property or a method go


after the point.

Always before the point goes the name of the control.

PROF. CHRISTIAN MONTOYA 13


Microsoft Excel 2016

Ejercicio 2

Let's create a form that sends the data to the spreadsheet one below the other.
another.

Design the following format on sheet 1

2. Design the following form:

Text box Label

Group frame

Command button
Radio button

PROF. CHRISTIAN MONTOYA 14


Microsoft Excel 2016

1. Assign properties:

Objects Properties Value


Name From user
Userform1
Caption DATA RECORD
Name Lblname
Label1
Caption Name
Name The annihilated
Label2
Caption Age
Text box 1 Name Txtname
Text box 2 Name Txtedad
Name Accept
Button1
Caption Accept
Name Btncancelar
Button2
Caption Cancel
Group button Name Gender group
Name Optmale
Option button 1
Caption Male
Name Optfeminine
Option button 2
Caption Female

2. Programming the controls

PROGRAMMING THE CANCEL BUTTON

Private Sub btncancelar_Click()


End
End Sub

Another way to exit a form is through the following statement:

Prof. Christian Montoya 15


Microsoft Excel 2016

PROGRAMMING THE RECORD BUTTON

Private Sub btnaceptar_Click()


Make sure that you are on sheet 1
Activate Sheets("sheet1")
Rem to determine the following empty row
Filavacia = [Link] (Range("a:a")) + 1
Rem transfers the name to the cell
Cells(emptyRow, 1) = txtName
Rem transfers the sex
If optmale then Cells(emptyRow, 1) = 'Male'
If optfemenino thenCells(emptyRow, 2) = 'Female'

Rem delete the control data for the next entry


[Link] = “”
[Link] = “”
Optmasculino = false
Optfemenino = false

End Sub

OBSERVATION

To access a built-in function in the spreadsheet, we write the


sentence:

[Link]

Then we place another point where all the functions will appear in English.

To clear the selection of a radio button, we write the button and it


we equal to FALSE.

PROF. CHRISTIAN MONTOYA 16


Microsoft Excel 2016

3. We need to create a procedure that calls the form.

We insert a module / we write the following code:

Sub show ()
Show from user
End sub

4. In the spreadsheet we do the following

There is an inserted form that says ENTER


We right-click and from the list select ASSIGN
MACRO

In the displayed window, we select SHOW


Then we accepted.
Now every time we press the button, the form should come up.
that we have created.

PROF. CHRISTIAN MONTOYA 17


Microsoft Excel 2016

Exercise 3

Let's create a login form that allows us to insert the username and password.

Assigning the properties:

Objects Properties Value


Name Frmingreso
Userform1
Caption SYSTEM LOGIN
Name User label
Label1
Caption USER
Name Lblclave
Label2
Caption KEY
Text box 1 Name User text
Textbox 2 Name Txtclave
Name Accept
Button1
Caption Accept
Name Btncancelar
Button2
Caption Cancel

2. Programming the cancel button:

Private Sub btncancelar_Click()


Unload Me
End Sub

PROF. CHRISTIAN MONTOYA 18


Microsoft Excel 2016

3. Programming the accept button:

Private Sub btnaccept_Click()


If txtusuario = "admin" And txtclave = "cmmc05" Then
[Link]
Else
The data is not correct
txtusuario = ""
txtclave = ""
[Link]
End If
End Sub

4. If the user is admin and the entered password is cmmc05, then it opens.
another form with the main name, if the data is not correct a appears a
message indicating that the data is not correct.

PROF. CHRISTIAN MONTOYA 19


Microsoft Excel 2016

Exercise 4

Let's create a form that loads without starting the Excel environment.

Assigning the properties:

Objects Properties Value


Name Frmsystem
Userform1
Caption SYSTEM
Name User Label
WELCOME TO
Label1 SYSTEM OF
Caption
BILLING.

2. Program the form:

Private Sub UserForm_Initialize()


[Link] = False
End Sub

PROF. CHRISTIAN MONTOYA 20


Microsoft Excel 2016

3. Upload the form:

On the left side of the screen, double click on ThisWorkbook

Doble clic sobre thisworkbook

Select the form event, in our case it will be on initialize.

Private Sub Workbook_Open()


[Link]
End Sub

4. Save the file as an executable and of type (Excel workbook enabled for
macros). Then we close the file and create a shortcut.

We right-click / properties

We changed the icon for the shortcut. Now double clicking it opens.
the form:

PROF. CHRISTIAN MONTOYA 21

You might also like