You are on page 1of 4

MACROS IN DEPTH

Macros help in eliminating some of the drudgery associated with


the repetitious command sequences or data manipulation tasks.
Macros are Excels automation capability.
In this course, we will:
Learn when it is appropriate to use macros.
Get familiarised with features such as
1. Creating macros by recording keystrokes.
2. Expanding a macro for more than one task.
3. Making macros available to all workbooks.
4. Launching macros with keystroke shortcuts and
toolbar buttons.
5. Editing VBA code and making sense of the VBA
environment.
6. Debugging and testing macros via step mode and
split screens.
7. Expanding the power of macros with Do loops and If
commands.
We come across macros in our daily usage of Excel. For example,
if we want to strikethrough some text data, then we could
highlight the cell containing it and go to Format Cells and choose
the Strikethrough option, but there is already a built in shortcut
(macro) for that task Ctrl + 5 (toggles Strikethrough).
The starting point for talking about macros is when you want to
combine a series of steps or actions into a single step or action.
Any sequence of action could be turned into a macro by a process
known as Recording.
An excel file containing macros has the file extension .xlsm .
There are some security concerns associated with macros as they
could act as repositories for viruses. So we get a warning when
we open an excel file containing macros asking whether to enable
or disable the macro content. If the files we access are from a

trusted source or location, we could add the location in File


Tab>Options>Trust Center>Trust Center Settings>Trusted
Locations.
The macro name cannot begin with a number and shouldnt
contain spaces. We could instead use underscore ( _ ).
There are 3 ways to start recording a macros:
1. In the lower left corner of the screen, there is a symbol beside
the status Ready which depicts the state of Macro recording. To
begin recording, click on it.
2. In the View tab on the Ribbon under the Macros group, we can
start recording a macro.
3. We can add an additional tab in the Ribbon called the
Developer tab. We can find the controls related to macro
recording in the Code group in the tab.
When we use macros, we can't use the Undo option to do
undo what we just did.
Although recording macros is the dominant way of creating
macros, viewing and editing the code in VBA editor is equally
effective.
Avoid using the following keystroke combinations for macros so
as to not override their original functions. Generally using them
along with Shift in combination works fine.
Ctrl key combinations in Excel
2010
New
A Select All
N Workbook
Open
B Bold
O Workbook
C Copy
P Print
D Fill Down
Q
E
R Fill Right
F Find
S Save
G Go to
T Create Table
H Replace
U Underline

Italics

J
K
L
M

Create
Hyperlink
Create a
Table

Paste
Close
W Workbook
X
Y
Z

Cut
Repeat or
Redo
Undo

If we want the macros that we create to be available every time


we open any file, then we need to store them in Personal Macro
Workbook instead of the usual This Workbook option.
The macros stored in the Personal Macro Workbook can then be
added to the Quick Access Toolbar by right clicking on the
Quick Access Toolbar and choosing More Commands> Choose
commands from> Macros. You can also modify the default
shortcut icon if needed.
You can assign a macro to an object after creating it by right
clicking on the object and choosing Assign a macro. You can
insert an object from the Insert tab on the Ribbon. To choose or
edit the object containing a macro Ctrl + Click.
Every time you record a macro, you create a small program using
VBA (Visual Basic for Applications). You can access the VBA
code for any macro in the VBA editor by going to View Tab>
Macros group> View Macros> Choose the macro to be edited or
viewed> Edit.
We cant change the keyboard shortcut for the macro in
the VBA editor by deleting and typing in a new shortcut. It has
to be done in the Macros dialog box.
To toggle the VBA environment on or off, press Alt + F11.
We can make use of relative references while recording a
macro so as to ensure that actions take place relative to the
initial selected cell.

We can use the If else condition, For loop and Do loop while
programming in VBA.
Different ways of carrying out a Do loop condition in a macro until
a cell containing no data is reached:
Do While ActiveCell <> . Loop
Do Until IsEmpty(Active Cell) .. Loop
To give a macro even more flexibility, we can introduce the idea
of an interactive macro i.e. a macro that pauses and asks for
input from a user.
We can check if the macro is working properly by making use of
the Step mode wherein the code moves one line at a time
performing the corresponding actions on the worksheet. This
option can be found in Debug> Step Into. We can press F8 to
move through each line of code and Ctrl + Shift + F8 to move
out of the Step mode.
We can create individual macros for separate tasks and then
combine them into a single macro. Often times, this involves just
copying and pasting the macros into a single macro. But at times,
it requires certain modifications to be done.

To prevent the screen from showing the actions being taken in


the worksheet (called Screen Updating) and only seeing the
end result, we need to type the following in the beginning of the
macro : Application.ScreenUpdating = False.

You might also like