Professional Documents
Culture Documents
1
Advanced knowledge of Spreadsheets (T3.2)
TU I TU II
TU III
Representation of data
and information
2
Advanced knowledge of Spreadsheets (T3.2)
Task automation*
Formulas and
functions
Task automation*
Visualization and
dynamic charts
Task automation*
3
Advanced knowledge of Spreadsheets (T3.2)
Overview
3.2.1.- Macros
o Tasks automation
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
6
Advanced knowledge of Spreadsheets (T3.2)
3.2.1.- Macros
7
Advanced knowledge of Spreadsheets (T3.2)
3.2.1.- Macros
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.3.- Applications
o Mail merge
o Import and export in other formats
12
Advanced knowledge of Spreadsheets (T3.2)
13
Advanced knowledge of Spreadsheets (T3.2)
14
Advanced knowledge of Spreadsheets (T3.2)
15
Advanced knowledge of Spreadsheets (T3.2)
• 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)
• 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)
• UserForm
The fields and buttons of this form can be fully customized for a given task.
18
Advanced knowledge of Spreadsheets (T3.2)
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
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
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
3
1
25
Advanced knowledge of Spreadsheets (T3.2)
Form UserForm
26
Advanced knowledge of Spreadsheets (T3.2)
Form UserForm
27
Advanced knowledge of Spreadsheets (T3.2)
Form UserForm
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
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
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
36
Advanced knowledge of Spreadsheets (T3.2)
UserForm Controls
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.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
42
Advanced knowledge of Spreadsheets (T3.2)
3.2.3.- Applications
o Mail merge
43
Advanced knowledge of Spreadsheets (T3.2)
Mail merge
44
Advanced knowledge of Spreadsheets (T3.2)
Mail merge
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
50
Advanced knowledge of Spreadsheets (T3.2)
Task
51
Advanced knowledge of Spreadsheets (T3.2)
3.2.3.- Applications
o Mail merge
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)
Task
1. Create a text file with the following content:
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)
58