Lecture 8
Lecture 8
&
VBA
LECTURE 8
1
What Is a Macro?
◉ An Excel macro is an action or a set of actions that can be recorded, named, saved
◉ By using macros, we are able to automate repetitive tasks associated with data
2
Turn on Developer Tab
◉ Navigate to Customize the ribbon and place a check on the Developer checkbox
3
T U R N O N
D E V E L O P E R
T A B
4
T U R N O N
D E V E L O P E R
T A B
5
T U R N O N
D E V E L O P E R
T A B
6
T U R N O N
D E V E L O P E R
T A B
7
Record a Macro
◉ Now that we have the developer tab on our ribbon, let’s start recording our first
macro!
◉ Steps:
8
Record a Macro
◉ Give your macro a name and assign the shortcut which will activate the macro
◉ Click Ok!
◉Note: Every step you perform from now on will be recorded by the macro
◉Once you are done, go back to the developer tab and click stop recording
9
Record a Macro
10
Record a Macro
11
Macro Workbook
◉ A macro can be saved in the same workbook from where you recorded it
◉In that case, you can run the macro from that workbook only
◉ In the personal macro workbook, where you can save your macros which enables you
12
Macro Workbook
13
Running Macros
◉ You can run the macro you have recorded◉ Click Macros
14
Running Macros
◉Only the macro that you recorded appears in the Macros list
15
Absolute References and Relative
References
◉ While recording a macro, you can use either absolute references or relative
◉ Absolute references make your macro run at the same cells where you recorded the
macro
◉On the other hand, relative references make your macro run at the active cell
16
Add a Button to Run Your Macro
◉ Or insert a button by going to the Developer tab > Insert > Form Controls
17
Add a Button to Run Your Macro
◉ Add the text to the shape by right-clicking the shape and selecting edit text
◉ Select the corresponding macro from the window and select (by clicking) Ok
◉ Whenever you select (by clicking) that shape, Microsoft Excel will activate and run
18
Add a Button to Run Your Macro
19
Add a Button to Run Your Macro
20
Add a Button to Run Your Macro
21
Add a Button to Run Your Macro
22
Add a Button to Run Your Macro
23
M a c r o C o d e
I n V i s u a l
B a s i c E d i t o r
24
What Is VBA?
◉ It is integrated into the major Microsoft Office applications, such as Word, Excel, and
Access
25
Are VB and VBA the same?
◉ If you have experience writing VB6 code, you can write VBA
26
Overview of the VB Editor
◉ Project Window
◉Properties window
◉Code Window
27
Project Window
type
object “Module1”
28
Properties Window
◉The Properties Window shows the properties and
worksheet
on the right
29
Properties Window
◉ Selecting a module in the Project window will show it
pressing Enter
31
Open a Macro for Editing
◉ You can use the Macro dialogue to open
32
Open a Macro for Editing
◉ Alternatively, you can open the VB Editor directly by clicking the Visual Basic button
33
Running Macros in the VB Editor
◉ Macros can be run directly from the VB Editor, which is useful for testing and
debugging
34
Running a Macro
◉ In the Project Window, double-click the module containing the macro you want to test (to
◉In the Code window, place the cursor anywhere on the macro’s code between “Sub” and
“End Sub”
◉Click the Run button on the toolbar, or press the keyboard shortcut F5
35
Running a Macro
36