You are on page 1of 58

Advanced knowledge of spreadsheets

Unit III: Representation of data and


information.
T3.2 User forms and applications

Advanced knowledge of Spreadsheets


Gonzalo Vázquez Vilar
2018/2019

1
Advanced knowledge of Spreadsheets (T3.2)

Teaching Unit III

TU I TU II

A first contact Designing and


understanding data

TU III
Representation of data
and information

2
Advanced knowledge of Spreadsheets (T3.2)

Unit 3: User Forms and applications


Structure and Basic
Operations

Working with cells


Block I: First contact and sheets, importing
data and references

Task automation*

Formulas and
functions

Tables and Pivot


Block II: Building, Tables
Course Syllabus interpreting and
exploiting data Data analysis: solver
and scenarios

Task automation*

Visualization and
dynamic charts

Block III: Representing User forms and


data and information applications

Task automation*

3
Advanced knowledge of Spreadsheets (T3.2)

Overview

3.2.1.- Macros
o Tasks automation

3.2.2.- User Forms


o What are user forms useful for?
o Design and customization of user forms
o Kinds of user forms

3.2.3.- Applications
o Mail merge
o Import and export in other formats

4
Advanced knowledge of Spreadsheets (T3.2)

3.2.1.- Macros

• What is a macro?
The macro scripts allow the user to perform repetitive tasks with minimum
effort. For example macros can be used for task automation, loading and
processing data, creating automatic reports, or presenting data in a
dynamic or adaptative fashion.
In Excel, macros are usually programmed in the language Visual Basic
Script (VBS) and allow to perform advanced tasks within the spreadsheet.

5
Advanced knowledge of Spreadsheets (T3.2)

3.2.1.- Macros

Can be accessed from the Developer menu

6
Advanced knowledge of Spreadsheets (T3.2)

3.2.1.- Macros

If the Developer menu is not available, it can be enabled in


File > Options

7
Advanced knowledge of Spreadsheets (T3.2)

3.2.1.- Macros

For example, to add a button with an associated script:

1
2

8
Advanced knowledge of Spreadsheets (T3.2)

3.2.1.- Macros

9
Advanced knowledge of Spreadsheets (T3.2)

3.2.1.- Macros
The macro script can be edited in the VB Editor (Alt+F11)

10
Advanced knowledge of Spreadsheets (T3.2)

3.2.1.- Macros
The macro script can be run by pressing the associated button

11
Advanced knowledge of Spreadsheets (T3.2)

Overview

3.2.1.- Macros
o Tasks automation

3.2.2.- User Forms


o What are user forms useful for?
o Design and customization of user forms
o Kinds of user forms

3.2.3.- Applications
o Mail merge
o Import and export in other formats

12
Advanced knowledge of Spreadsheets (T3.2)

3.2.2.- User Forms

• What are user forms useful for?


“Emerging window that allows users to input information”

13
Advanced knowledge of Spreadsheets (T3.2)

3.2.2.- User Forms

• Why should we use user forms?


1. They enable more powerful and useful macro scripts,
as they allow users to interact with the script.
2. They can be used for data input automation.
3. Allow to create customized reports and graphs.

14
Advanced knowledge of Spreadsheets (T3.2)

3.2.2.- User Forms

• Types of user forms in Excel

MsgBox InputBox UserForm

15
Advanced knowledge of Spreadsheets (T3.2)

3.2.2.- User Forms

• MsgBox
Shows a personalized message to the user, and enables some limited
interaction via Yes/No or Accept/Cancel buttons.

16
Advanced knowledge of Spreadsheets (T3.2)

3.2.2.- User Forms

• InputBox
Shows an input box that allows the user to enter information in addition to
the Accept/Cancel interaction.

17
Advanced knowledge of Spreadsheets (T3.2)

3.2.2.- User Forms

• UserForm
The fields and buttons of this form can be fully customized for a given task.

18
Advanced knowledge of Spreadsheets (T3.2)

3.2.2.- User Forms

The different user forms can be created using the scrips in the
VB Editor (Alt+F11)

19
Advanced knowledge of Spreadsheets (T3.2)

Form MsgBox

• MsgBox
Shows a personalized message to the user, and enables some limited
interaction via Yes/No or Accept/Cancel buttons.

20
Advanced knowledge of Spreadsheets (T3.2)

Form MsgBox

Shows a personalized message to the user, and enables some limited


interaction via Yes/No or Accept/Cancel buttons.
Title = "Learning Excel"
Config = vbYesNo + vbQuestion
Ans = MsgBox(“Generate the monthly report?", Config, Title)

Usage and options of the MsgBox function can be found at:


https://docs.microsoft.com/es-es/office/vba/language/reference/user-interface-help/msgbox-function
https://docs.microsoft.com/es-es/office/vba/language/reference/user-interface-help/msgbox-constants

21
Advanced knowledge of Spreadsheets (T3.2)

Form InputBox

• InputBox
Shows an input box that allows the user to enter information in addition to
the Ok/Cancel interaction.

22
Advanced knowledge of Spreadsheets (T3.2)

Form InputBox

Shows an input box that allows the user to enter text information.
Title = "Learning Excel"
CName = InputBox("Customer name?", Title)
MsgBox "Customer name: " + CName

Usage and options of the MsgBox function can be found at:


https://docs.microsoft.com/es-es/office/vba/language/reference/user-interface-help/inputbox-
function

23
Advanced knowledge of Spreadsheets (T3.2)

Form UserForm

• UserForm
The fields and buttons of this form can be fully customized for a given task.

24
Advanced knowledge of Spreadsheets (T3.2)

Form UserForm

It can be designed and customized from the VB Editor (Alt+F11) using:


Insert ➪ UserForm
The different controls are then inserted using the toolbox window.

3
1

25
Advanced knowledge of Spreadsheets (T3.2)

Form UserForm

Control properties can be accessed by selecting each control:

26
Advanced knowledge of Spreadsheets (T3.2)

Form UserForm

Some control properties:


• (Name): Name to refer to the control from the VB scripts
• Text: Text that appears in the control by default
• TextAlign: Text alignment
• TabIndex: Determines the focus order when the user press
the Tab key in the keyboard
• Font: Text font type
• ForeColor: Control elements color
• …

27
Advanced knowledge of Spreadsheets (T3.2)

Form UserForm

Once created, it can be shown to the user using the command:


UserForm1.Show
Furthermore, the actions of the different elements and events in the form need to
be programmed using the VB scripting tool (e.g., the events of pressing a button
or updating a field)

28
Advanced knowledge of Spreadsheets (T3.2)

Formulario UserForm

Example:
UserForm1.tNombre.Value = ""

UserForm Content
name
Control name
in the UserForm
(Name)

29
Advanced knowledge of Spreadsheets (T3.2)

Form UserForm
Pressing the Cancel button:
Private Sub BCancel_Click()
Unload UserForm1
End Sub

Pressing the Finish button:


Private Sub BFinish_Click()
SaveFields
Unload UserForm1
End Sub

Pressing the Next Client button:


Private Sub BNext_Click()
SaveFields
UserForm1.tNombre.Value = ""
UserForm1.TApellidos.Value = ""
UserForm1.TEmail.Value = ""
End Sub

30
Advanced knowledge of Spreadsheets (T3.2)

Form UserForm
Private Sub SaveFields()
Range("A2").Select ' Select A2 as the active cell
Do Until IsEmpty(ActiveCell) ‘ We repeat until we find an empty cell
ActiveCell.Offset(1, 0).Select ‘ One row down
Loop
ActiveCell.Value = ActiveCell.Row - 1 ‘ We create the id number
ActiveCell.Offset(0, 1).Select ‘ (one column right)
ActiveCell.Value = UserForm1.tNombre.Value ‘ We copy the name field,
ActiveCell.Offset(0, 1).Select ' (one column right)
ActiveCell.Value = UserForm1.TApellidos.Value ‘ surname field
ActiveCell.Offset(0, 1).Select ' (one column right)
ActiveCell.Value = UserForm1.TEmail.Value ‘ and email.
End Sub

1 2

31
Advanced knowledge of Spreadsheets (T3.2)

UserForm Controls

• Label / group box


• Text field
• Combo box / combo list
• Check box / option button
• Button / Toggle button
• Tab bar
• Scroll bar / spin button
• Image

32
Advanced knowledge of Spreadsheets (T3.2)

UserForm Controls

The controls of the UserForm can be also inserted directly in the spreadsheet:

33
Advanced knowledge of Spreadsheets (T3.2)

UserForm Controls

Form Controls
• Native Excel controls
• Maximum compatibility

ActiveX Controls
• More flexible and customizable
• They allow to include additional controls
from external developers
• Due to security reasons, ActiveX is not
enabled by default by many users
(it should be avoided, if possible)

34
Advanced knowledge of Spreadsheets (T3.2)

UserForm Controls

Form Controls
• Native Excel controls
• Maximum compatibility

ActiveX Controls
• More flexible and customizable
• They allow to include additional controls
from external developers
• Due to security reasons, ActiveX is not
enabled by default by many users
(it should be avoided, if possible)

35
Advanced knowledge of Spreadsheets (T3.2)

UserForm Controls

Toy example: CommandButton

36
Advanced knowledge of Spreadsheets (T3.2)

UserForm Controls

Toy example: CommandButton

Online interface to generate a quote

37
Advanced knowledge of Spreadsheets (T3.2)

UserForm Controls

Once the UserForm controls have been created, they need to be assigned to a macro
script using the contextual menu. This script defines the behavior of the control.

38
Advanced knowledge of Spreadsheets (T3.2)

Task

1. Create an empty spreadsheet with the fields ID, Name and Surname.
2. Using the VB Editor, create a UserForm that looks similar to the one above.
3. Insert also a CommandButton in the spreadsheet that opens this form.
4. Add the VB code such that:
• Pressing “Cancel” closes the window disregarding the info in the text fields.
• Pressing “Finish” closes the window after saving the info in the text fields as a
new entry in the spreadsheet.
• Pressing “Next Client” creates a new entry in the spreadsheet with the info in
the text fields, and then resets the form to show the text fields empty.

39
Advanced knowledge of Spreadsheets (T3.2)

Overview

3.2.1.- Macros
o Tasks automation

3.2.2.- User Forms


o What are user forms useful for?
o Design and customization of user forms
o Kinds of user forms

3.2.3.- Applications
o Mail merge
o Import and export in other formats

40
Advanced knowledge of Spreadsheets (T3.2)

3.2.3.- Applications

• What is an application
Several macro scripts and forms working together to
perform advanced tasks such as:
1. Data acquisition
2. Advanced data processing
3. Automatic reports and customized graphs
4. Interface with other programs
5. …

41
Advanced knowledge of Spreadsheets (T3.2)

3.2.3.- Applications

• Excel scripting and developing tools are quite powerful…


• Example: Coding in Excel allows even elaborated games:
https://www.youtube.com/watch?v=532_BZjDSpY

42
Advanced knowledge of Spreadsheets (T3.2)

3.2.3.- Applications

• We study now in detail two tools that serve as an


interface with external applications:

o Mail merge

o Import and export in external formats

43
Advanced knowledge of Spreadsheets (T3.2)

Mail merge

• A spreadsheet can serve as a data source for a mail merge


process in an external application, such as Microsoft Word.
• Example:

44
Advanced knowledge of Spreadsheets (T3.2)

Mail merge

• The target of the mail merge process is a document which features:


1. The content, which is common to all the entries in the spreadsheet, plus
2. Fields which will be replaced by the actual data from the table.

45
Advanced knowledge of Spreadsheets (T3.2)

Mail merge
1

46
Advanced knowledge of Spreadsheets (T3.2)

Mail merge

47
Advanced knowledge of Spreadsheets (T3.2)

Mail merge

48
Advanced knowledge of Spreadsheets (T3.2)

Mail merge

49
Advanced knowledge of Spreadsheets (T3.2)

Mail merge

• The data from the spreadsheet can be merged with certificates,


letters, invoices, badges, post labels…

50
Advanced knowledge of Spreadsheets (T3.2)

Task

ID Name Family name Affiliation Payment


101 Manuel Iglesias Cobo Acciona 100 €

102 Inés Martínez Gómez Banco Santander 100 €

103 Luis Tomás Ramos City 100 €

104 Julio del Río Pinto Investia 100 €

105 Clara Rodríguez Jiménez ACL 100 €

106 Pedro Rico Blanco SolarTree 150 €

107 Oscar Martín Rey iEnergy 150 €

108 Raúl Arce Villar GreenTea 150 €

1. We are hosting a meeting with the participants listed in this table


2. Use the information of this table and mail merge to generate in Word:
• Attendance certificates for the participants
• Badge labels to identify the participants

51
Advanced knowledge of Spreadsheets (T3.2)

3.2.3.- Applications

• We study now in detail two tools that serve as an


interface with external applications:

o Mail merge

o Import and export in external formats

52
Advanced knowledge of Spreadsheets (T3.2)

Importing data

From
• Text files
• XML file
• Databases
• Online sites
• Other sources
•…

53
Advanced knowledge of Spreadsheets (T3.2)

Exporting data
Output format can be selected from the menu: File > Export

54
Advanced knowledge of Spreadsheets (T3.2)

Import and Export in customized formats


• If existing export formats are not adequate, it is even possible
to use VB script macros to generate a format adapted to a
specific task.

(However, we will not study this option in this course)


55
Advanced knowledge of Spreadsheets (T3.2)

Task
1. Create a text file with the following content:

Especie / Nombre común / Distribución / Hábitat / Longitud (cm)


Otus scops / Autillo / África y Eurasia / Áreas con árboles planifolios / 19-20
Bubo bubo / Búho real / Eurasia y norte de África / Terrenos con riscos o páramos / 60-75
Athene noctua / Mochuelo europeo / Eurasia y norte de África / Terrenos abiertos / 21-23
Strix aluco / Cárabo común / Eurasia y norte de África / Bosques y terrenos arbolados / 37-39
Asio otus / Búho chico / Eurasia y Norteamérica / Bosques y terrenos arbolados / 35-37
Asio flammeus / Lechuza campestre / África, América y Eurasia / Páramos y herbazales / 37-39
Aegolius funereus / Mochuelo boreal / Eurasia y Norteamérica / Bosques de coníferas / 24-26

2. Use “Get data from text file” to import this tabla in Excel. Note that the field
separator symbol is “/”
3. Export this table as a webpage with the title “Búhos de España”:

56
Forms
Applications UserForm
MsgBox
Messages and basic interaction
Advanced tools which allow (Yes/No, Acept/Cancel…) UserForm
the development of (designed in the VB Editor)
interactive tasks InputBox
Text input and buttons
UserForm controls
They require macro (inserted in the spreadsheet)
programming via the UserForm Their events activate
VB scripting tool Customizable forms for
advanced applications macros in VB source code
Interaction with the user via
forms and events

Examples:
• Data acquisition
• Advanced data processing
• Customized reports and
interactive graphs
User forms and applications
• Mail merge
Excel applications enable an advanced
• Import / export tools
interaction with the spreadsheet user.

57
Advanced knowledge of Spreadsheets (T3.2)

References and specific resources


• Quick start: Create a macro
https://support.office.com/en-us/article/inicio-rápido-crear-una-macro-741130ca-080d-49f5-9471-
1e5fb3d581a8
• Assign a macro to a Form or a Control button
https://support.office.com/en-us/article/asignar-una-macro-a-un-botón-formulario-o-control-d58edd7d-cb04-
4964-bead-9c72c843a283
• Visual Basic (VB) Language Reference
https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/visual-basic-language-
reference
• MsgBox function
https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/msgbox-function
https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/msgbox-constants
• InputBox function
https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/inputbox-function
• UserForm object
https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/userform-object
• Mail merge
https://support.office.com/en-us/article/combinar-correspondencia-con-una-hoja-de-cálculo-de-excel-
858c7d7f-5cc0-4ba1-9a7b-0a948fa3d7d3

58

You might also like