Excel Macros Tutorial
Excel Macros Tutorial
Macros help you to save time on repetitive tasks involved in data manipulation and data
reports that are required to be done frequently.
Audience
This guide targets novice developers and those new to Excel Macros. After completing this
tutorial, your firm foundation in creating macros will allow you to use macros efficiently.
Prerequisites
When you record a macro, Excel stores it as a VBA code. You can view this code in the
VBA editor. You can understand the code and modify it if you have substantial knowledge
of Excel VBA. However, if you do not have sufficient knowledge, then we will suggest you
to go through our short tutorials on VBA.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at [email protected]
i
Excel Macros
Table of Contents
About the Tutorial ............................................................................................................................................ i
Audience ........................................................................................................................................................... i
Prerequisites ..................................................................................................................................................... i
Copyright & Disclaimer ..................................................................................................................................... i
Table of Contents ............................................................................................................................................ ii
ii
Excel Macros
iii
1. Excel Macros – Overview Excel Macros
An Excel macro is an action or a set of actions that you can record, give a name, save and
run as many times as you want and whenever you want. When you create a macro, you are
recording your mouse clicks and keystrokes. When you run a saved macro, the recorded
mouse clicks and keystrokes will be executed in the same sequence as they are recorded.
Macros help you to save time on repetitive tasks involved in data manipulation and data
reports that are required to be done frequently.
VBA stands for Visual Basic for Applications and is a simple programming language that is
available through Excel Visual Basic Editor (VBE), which is available from the DEVELOPER tab
on the Ribbon. When you record a macro, Excel generates VBA code. If you just want to
record a macro and run it, there is no need to learn Excel VBA. However, if you want to modify
a macro, then you can do it only by modifying the VBA code in the Excel VBA editor.
You will learn how to record a simple macro and run it with Excel commands in the chapter -
Creating a Simple Macro. You will learn more about macros and about creating and / or
modifying macros from Excel VBA editor in the later chapters.
You will learn about Personal Macro Workbook in the chapter - Saving all your Macros in a
Single Workbook.
Macro Security
Macros will be stored as VBA code in Excel. As with the case of any other code, macro code
is also susceptible to malicious code that can run when you open a workbook. This is a threat
to your computer. Microsoft provided with the Macro Security facility that helps you in
protecting your computer from such macro viruses.
You will learn more about this in the chapter - Macro Security.
4
Excel Macros
You will learn about these in the chapters - Using Absolute References for a Macro and Using
Relative References for a Macro.
However, you should know how to view the macro code. You can learn how to access VBA
editor in Excel and about the different parts of the VBA editor in the chapter – Excel VBA.
You can learn how to view the macro code in Excel VBA editor and you can understand the
macro code in the chapter - Understanding Macro Code.
Running Macros
Excel provides several ways to run a macro. You can choose the way you want to run a macro.
You will learn about these different possible ways of running a macro in the chapter - Running
a Macro.
Editing a Macro
You can modify macro code in Excel VBA editor. If you want to make extensive changes, you
should have Excel VBA knowledge. But, if you want to make only minor changes to the code
or if you want to copy the VBA code from a recorded macro to another macro, you can refer
to the chapter - Editing a Macro.
You can rename a macro and even delete it. You will learn about this also in the same chapter.
User Forms
5
Excel Macros
A Form is normally used to collect required information. It will be self-explanatory making the
task simple. Excel User Forms created from Excel VBA editor serve the same purpose,
providing the familiar options such as text boxes, check boxes, radio buttons, list boxes,
combo boxes, scroll bars, etc. as controls.
You will learn how to create a User Form and how to use the different controls in the chapter
– User Forms.
6
2. Excel Macros – Creation Excel Macros
You can create a macro with Excel commands by recording the key strokes and mouse clicks,
giving the macro a name and specifying how to store the macro. A macro thus recorded can
be run with an Excel command.
Suppose you have to collect certain results repeatedly in the following format –
Instead of creating the table each time, you can have a macro to do it for you.
Recording a Macro
To record a macro do the following –
7
Excel Macros
Remember that whatever key strokes and mouse clicks you do, will be recorded now.
8
Excel Macros
The first step to click on a particular cell is important as it tells where exactly the macro has
to start placing the recorded steps. Once you are done with the recording, you have to click
Stop Recording to avoid recording of unnecessary steps.
Running a Macro
You can run the macro you have recorded any number of times you want. To run the macro,
do the following -
9
Excel Macros
Only the macro that you recorded appears in the Macros list.
Click the macro name – MyFirstMacro in the Macro dialog box. The description you typed
while recording the macro will get displayed. Macro description allows you to identify for
what purpose you have recorded the macro.
Click the Run button. The same table that you have created while recording the macro will
appear in just a split of a second.
10
Excel Macros
You have discovered the magic wand that Excel provides you to save time on mundane tasks.
You will observe the following –
Though the active cell before running the macro was A1, the table is placed in the cell
B2 as you have recorded.
In addition, the active cell became E2, as you have clicked that cell before you stopped
recording.
You can run the macro in multiple worksheets with different active cells before running the
macro and observe the same conditions as given above. Just keep a note of this and you will
understand later in this tutorial why it has occurred so.
You can also have a macro recording that places your recorded steps in the active cell. You
will learn how to do this as you progress in the tutorial.
Storing a Macro
You might wonder how to save the macros that are created. In this context you need to know-
Storing a macro
Saving a macro enabled file
As and when you create a macro, you can choose where to store that particular macro. You
can do this in the Record Macro dialog box.
Click the box - Store macro in. The following three options are available-
This Workbook.
11
Excel Macros
New Workbook.
Personal Macro Workbook
This Workbook
This is the default option. The macro will be stored in your current workbook from where you
created the macro.
New Workbook
This option, though available, is not recommended. You will be asking Excel to store the macro
in a different new workbook and mostly it is not necessary.
12
Excel Macros
Try to save the workbook. By default, you would be asking Excel to save the workbook as an
.xls file. Excel displays a message saying that an Excel feature VB project cannot be saved in
a macro free workbook, as shown below.
Note: If you click Yes, Excel will save your workbook as a macro free .xls file and your macro
that you stored with This Workbook option will not get saved. To avoid this, Excel provides
you an option to save your workbook as a macro-enabled workbook that will have .xlsm
extension.
13
Excel Macros
You will learn more about these in later chapters in this tutorial.
14
3. Excel Macros – Macros in a Single Workbook
Excel Macros
Excel provides you with a facility to store all your macros in a single workbook. The workbook
is called Personal Macro Workbook - Personal.xlsb. It is a hidden workbook stored on your
computer, which opens every time you open Excel. This enables you to run your macros from
any workbook. There will be a single Personal Macro Workbook per computer and you cannot
share it across computers. You can view and run the macros in your Personal Macro Workbook
from any workbook on your computer.
Select Personal Macro Workbook from the drop down list under the category Store macro
in.
15
Excel Macros
Click OK.
Stop recording.
16
Excel Macros
The macro name appears with a prefix PERSONAL.XLSB! indicating that the Macro is in the
Personal Macro Workbook.
Save your workbook. It will get saved as an .xls file as the macro is not in your workbook and
close Excel.
You will get the following message regarding saving the changes to the Personal Macro
Workbook –
17
Excel Macros
Click the Save button. Your macro is saved in the Personal.xlsb file on your computer.
18
Excel Macros
Now you can view the macros saved in the personal macro workbook.
19
Excel Macros
If the personal macro workbook is hidden, you will get a message saying “Cannot edit a macro
on a hidden workbook”.
Unhide the personal macro workbook and delete the selected macro.
The macro will not appear in the macros list. However, when you create a new macro and
save it in your personal workbook or delete any macros that it contains, you will be prompted
to save the personal workbook just as in the case you saved it first time.
20
Excel Macros
21