MACROS
&
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
and executed as many times as required and whenever desired
◉ By using macros, we are able to automate repetitive tasks associated with data
manipulation and data reporting that must be accomplished repeatedly
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:
◉ On the Developer tab, go to Code group and click Record macro
8
Record a Macro
◉ The Record Macro dialogue box will appear
◉ 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
to run those macros from any workbook
12
Macro Workbook
13
Running Macros
◉ You can run the macro you have recorded◉ Click Macros
any number of times you want
◉ Select View Macros from the dropdown
◉ To run the macro: list
◉ Click on a new worksheet
◉ Note the active cell
◉ Click the VIEW tab on the Ribbon
14
Running Macros
◉Only the macro that you recorded appears in the Macros list
◉Click the macro name
◉Click the Run button
15
Absolute References and Relative
References
◉ While recording a macro, you can use either absolute references or relative
references for the cells on which you are clicking
◉ 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
◉ You may create a button that will run your macro
◉ The steps below:
◉Go to illustrations > Shapes and select any
◉ 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
◉ Right-click the shape and select Assign Macro
◉ 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
your recorded macro
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?
◉Abbreviation of VBA: Visual Basic for Application
◉ VBA is a programming language that was developed by Microsoft
◉ It is integrated into the major Microsoft Office applications, such as Word, Excel, and
Access
25
Are VB and VBA the same?
◉The VBA language is identical to Visual Basic 6.0
◉ If you have experience writing VB6 code, you can write VBA
26
Overview of the VB Editor
◉The VB Editor has several windows
◉ Project Window
◉Properties window
◉Code Window
27
Project Window
◉The Project Window shows each Excel
file as its own project, with all the
objects in that project categorized by
type
◉ Recorded macros will appear in the
“Modules” category, usually in the
object “Module1”
28
Properties Window
◉The Properties Window shows the properties and
associated values of an object
◉ For example, clicking on a worksheet object in the
Project Window will show a list of properties for the
worksheet
◉Property names are on the left, and property values are
on the right
29
Properties Window
◉ Selecting a module in the Project window will show it
has only one property, “(Name)”
◉ You can change the name of a module by double-
clicking the property’s value, typing a new name, and
pressing Enter
◉ Changing a module’s name will rename it in the Project
Window, which is useful if you have a lot of modules
30
Code windows
◉ Code windows are special text
editors in which you can edit your
macro’s VBA code
◉ If you wanted to see the code for
a macro located in Module1, you
would double-click ‘Module1’ in
the Project Window
31
Open a Macro for Editing
◉ You can use the Macro dialogue to open
the code for a macro by selecting the
macro’s name and clicking the Edit button
◉ This will open the macro in the VB Editor
32
Open a Macro for Editing
◉ Alternatively, you can open the VB Editor directly by clicking the Visual Basic button
on the Developer tab
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
open its Code window)
◉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