Professional Documents
Culture Documents
Session-1&2b
Recording a macro
Writing simple VBA procedures
Creating event procedures
Assigning macros to Form Control objects in Excel
Recording Macros
Macros
• technically defined as units of VBA code
• can
– be thought of as a way to automate a series of actions in
a spreadsheet application
– either be created directly with VBA code in the VBE, or
recorded in Excel
To record a macro,
• firstly, know exactly the actions to perform, then
• use the Developer > Code > Record Macro
command on the Ribbon
2 2
1 of 28
1.1: Record a macro that copies and pastes data
A dealer wants to place the data highlighted in yellow
in a newspaper advertisement
3 3
5 5
7 7
8 8
4 of 28
1.2
Sub Macro1()
End Sub
1.2 (revised)
Sub Macro1a()
9 End Sub
1.3
Sub Macro2()
End Sub
10
5 of 28
1.3 (revised)
Sub Macro2a()
End Sub
11
1.4
Sub Macro3()
End Sub
1.4 (revised)
Sub Macro3a()
End Sub
12
6 of 28
First Program.xlsx
13
First Program
14
7 of 28
First Program
15
16
8 of 28
A First Program (Revised Version…)
17
18
9 of 28
Budget.xlsx
19
21
23
Sub SideBarHeading()
'
' SideBarHeading Macro
' Merge and Rotate cells Vertically
'
' Keyboard Shortcut: Ctrl+Shift+S
'
With Selection
.Orientation = 90
.MergeCells = True
End With
End Sub
24
12 of 28
Inventory.xlsx
25
Recording
a Macro
26
13 of 28
Saving
a Macro-
Enabled
Workbook
Running
a Macro
27
Editing
a Macro
in the VBE
28
14 of 28
Using Sort and Filter functions in VBA
By first recording a macro and then using the
generated VBA code to create similar macros
29 29
30 30
15 of 28
Filtering
Begin by recording three macros to filter the data for
specific flight destinations
To prepare for recording this macro, review the steps
that should be performed:
• Highlight the entire table (cells B3:E15)
• Select Data > Sort & Filter > Filter command on the Ribbon.
• Click on the filter drop-down button of the “Destination”
column.
• Select Beijing, Hong Kong, or All respectively from the filter
drop-down list:
– 1st Macro: “ViewBeijingFlights.”
– 2nd Macro: “ViewHongKongFlights”
– 3rd Macro: “ViewAllFlights”
31 31
32 32
16 of 28
VBA Code
The first line of code for all three macros, Range(“B3:E15”)
• Select is an example of the Select Method for the Range Object
34
17 of 28
Record a macro to sort the data by
number of stops/ price
To prepare for recording a macro to sort the data
table, review the steps to be performed
• Highlight all data (B3:E15)
• Click on: Data > Sort & Filter > Sort command
• Specify the sorting criteria and order in the Sort dialog box
• SortByStops Macro:
– Select “Stops” from the “Sort by” list, select “Values” from the “Sort
on” list, and select “Largest to Smallest” from the “Order” list, then
press OK to close the Sort dialog box
• SortByPrice Macro:
– Select “Price” from the “Sort by” list, select “Values” from the “Sort
on” list, and select “Largest to Smallest” from the “Order” list, then
press OK to close the Sort dialog box
35 35
36
18 of 28
VBA Code
The first line of code
• selects the range we are interested in sorting
38 38
19 of 28
6
Suppose that we also want to filter the Business or
Economy Class flights
39
40 40
20 of 28
Eg.: Activate event can be used on the
Workbook object
Once an object and event are selected, a sub
procedure will be initiated
41 41
42 42
21 of 28
Form and ActiveX Controls
To create a button in Excel, click on: Developer > Controls >
Insert drop-down list
Several objects can be placed on the spreadsheet:
• command buttons, scroll bars, list boxes, etc.
43 43
Control Properties
Each control has its own set of modifiable properties
• Name
= name of the control, this will be used to identify it in VBA (ex: cmdButton)
• Caption
= text which appears on the control (ex:“Click Here”)
Its font, size, and color can also be changed from the list of
properties
While working with ActiveX Controls,
• the Developer > Controls > Design Mode button is activated
The Click event procedure can now be associated with this new
object by titling the sub procedure
• Sub cmdButton_Click()
44 44
22 of 28
Command Button
To add a command button to
perform one of the filtering
macros we previously wrote in
VBA and set the following
properties
For example:
• Name = cmdHongKong_Click()
• Caption = “View Hong Kong Flights”
45 45
47 47
8
Create similar Command
Buttons for the other macros
previously recorded
This time, use the Form Controls
listed on the Insert drop-down
dialog box
Click on the Button object, and
then click somewhere on the
spreadsheet
The Assign Macro dialog box
appears
48 48
24 of 28
9
Right-click on the Button to activate the shortcut menu. Use the
Edit Text option to edit the caption of this button
Select the Format Control… option on the shortcut menu to
modify the format of this Button
49
50 50
25 of 28
Summary
Macros are technically defined as units of VBA code.
Macros can either be created directly with VBA code in the
Visual Basic Editor, or recorded in Excel.
Steps to Record a Macro:
1. Click on: Developer tab > Code group > Record Macro button.
2. Perform a sequence of actions in Excel.
3. Click on: Developer tab > Code group > Stop Recording button.
Events are actions that can take place in the Excel window that
cause an action to occur in the VBA code
An event procedure is just like a sub procedure, except that it is
associated with a particular action of an Excel object
Creating buttons as a user interface can greatly aid a user
unfamiliar with macros to perform desired functions
51 51
Ex. 1.1
52
26 of 28
Ex. 1.2
53
Ex. 1.3
54
27 of 28
Thanks
55 55
28 of 28