0% found this document useful (0 votes)
27 views

02 Create Excel Macros Without Programming - J.A. Gomez

Visual Basic

Uploaded by

uday_kendhe9005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

02 Create Excel Macros Without Programming - J.A. Gomez

Visual Basic

Uploaded by

uday_kendhe9005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 76

Create Excel Macros Without Programming

Create Excel Macros Without Programming


The Step-by-Step Guide to Start Automating Excel Without
Programming in VBA
by J.A. Gomez

Copyright © 2018 by Jorge Alberto Gomez Soto.

All rights reserved.

This publication is licensed for your personal enjoyment only. If you would like to
share this publication with another person, please purchase an additional copy for
each recipient. If you are reading this publication and did not purchase it, or it was
not purchased for your use only, then please return to Amazon.com and purchase
your own copy. Thank you for respecting the hard work of the Publisher and Author.

No part of this publication may be reproduced, stored in a retrieval system,


transmitted, translated, reprinted, reused, recited, broadcasted, sold or similar, in
whole or in part, in any form or by any means, electronic, mechanical, including
without limitation photocopying, microfilms, scanning, electronic adaptation,
recording, computer software, by any information or storage retrieval system, or
otherwise by any similar or dissimilar methodology now known or hereafter
developed without the prior written consent from the Publisher.

Trademarked names, logos and images may appear in this publication. Rather than
use a trademark symbol with every occurrence of a trademarked name, logo or
image, the names, logos and images are used only in an editorial fashion to the
benefit of the trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar
terms, even if they are not identified as such, is not to be taken as an expression
of opinion as to whether or not they are subject to proprietary rights. Additionally,
use of a term in this publication should not be regarded as affecting the validity of
any trademark.

Excel is a registered trademark of Microsoft Corporation. All other trademarks and


registered trademarks appearing in this publication are the property of their
respective owners.

2
Create Excel Macros Without Programming

The Publisher and the Author make no representations or warranties with respect
to the accuracy or completeness of the contents of this publication and specifically
disclaim all warranties, including without limitation warranties of fitness for a
particular purpose. No warranty may be created or extended by sales or
promotional materials.

The information provided in this publication is for information purposes only and is
provided on an "as is" basis. The advice and strategies contained herein may not
be suitable for every situation. Users of this publication are advised to carry out
their own due diligence when it comes to making decisions related (directly or
indirectly) to the contents of this publication.

This publication is sold with the understanding that the Publisher and the Author
are not engaged in rendering legal, accounting, technical or other professional
services. If professional assistance is required, the services of a qualified
professional should be sought. All information, products and services that are
mentioned or provided in this publication should be independently verified by your
own qualified professionals.

The fact that an organization or website is referred to in this publication as a citation


or a potential source of further information does not mean that the Author or the
Publisher endorse the information the organization or website may provide or
recommendations it may make. Further, readers should be aware that internet
websites listed in this publication may have changed or disappeared between the
moment when this publication was initially published and the moment when this
publication is read.

Neither the Author, the Editors nor the Publisher shall be liable nor responsible for
any errors or omissions that may be made in this publication, nor for any damages
arising (directly or indirectly) from this publication.

By using this publication, you agree that the Author, the Publisher and any related
(directly or indirectly) persons or parties are not responsible for the success or
failure of any decisions or actions related (directly or indirectly) to any information
presented in this publication. The Author and Publisher shall have neither liability
nor responsibility to any person or entity with respect to any loss or damages
arising (directly or indirectly) from the information contained in this publication.

Author: J.A. Gomez.

Layout: Walter Gray.

3
Create Excel Macros Without Programming

Copy Editor: Martin Phillips.

Proofreader: Kevin Thomas.

Cover Designer: Anthony Baker.

Published by: Power Spreadsheets.

First Edition: October 2018.

4
Create Excel Macros Without Programming

Table of Contents
Introduction .............................................................................................. 12

About this Book ...................................................................................... 12

About J.A. Gomez.................................................................................... 12

How this Book is Organized ...................................................................... 14

More Information .................................................................................... 14

Chapter 1: Set Up Excel to Work with Macros and VBA ................................... 16

Introduction ........................................................................................... 16

Introduction to the Developer Tab ............................................................. 16

Process to Display the Developer Tab ........................................................ 17

The Developer Tab .................................................................................. 18

The Code Group of Commands ............................................................... 19

The Add-ins, Controls and XML Groups of Commands ................................ 19

Summary and Key Concepts ..................................................................... 20

Chapter 2: Introduction to the Macro Recorder .............................................. 21

How You Create Macros in Excel ................................................................ 21

What the Macro Recorder Is ..................................................................... 21

Why the Macro Recorder is Important ........................................................ 22

Summary and Key Concepts ..................................................................... 22

Chapter 3: Planning and Preparing Prior to Creating an Excel Macro ................. 23

Introduction ........................................................................................... 23

Basics of Macro Recording Planning or Setup .............................................. 23

Appropriate Planning and Setup are Important ........................................... 24

The Impact of Planning and Setup on the Quality of Recorded VBA Code ..... 24

Summary and Key Concepts ..................................................................... 25

Chapter 4: Absolute and Relative References When Recording Macros .............. 26

5
Create Excel Macros Without Programming

The Importance of Understanding Cell References When Recording Macros .... 26

Absolute and Relative References in Excel .................................................. 27

Relative References in Excel ................................................................... 27

Absolute References in Excel .................................................................. 28

Absolute and Relative References When Recording Macros ........................... 29

Absolute References and the Macro Recorder ........................................... 29

Relative References and the Macro Recorder ............................................ 30

The Choice Between Absolute and Relative References When Recording Macros
.......................................................................................................... 30

Cell References in Worksheet Formulas Entered While Recording Macros ........ 34

Choose Between Absolute and Relative References ...................................... 35

The Use Relative References Button While Recording a Macro ..................... 36

Create Absolute References with the Go To Dialog Box .............................. 36

Summary and Key Concepts ..................................................................... 37

Chapter 5: Create Excel Macros Without Programming (Record a Macro) .......... 39

Introduction ........................................................................................... 39

The Macro Example You Create ................................................................. 40

Create Your First Excel Macro ................................................................... 40

Step #1: Work with the Appropriate Cell References ................................. 40

Step #2: Turn the Macro Recorder On ..................................................... 41

Step #3: Specify the Macro's Details with the Record Macro Dialog Box ....... 41

Step #4: Perform Actions to Record ........................................................ 45

Step #5: Turn the Macro Recorder Off ..................................................... 47

Execute a Recorded Macro with Keyboard Shortcuts .................................... 48

Save a Macro-Enabled Excel Workbook ...................................................... 48

Summary and Key Concepts ..................................................................... 50

Chapter 6: When to Create Macros with the Macro Recorder ........................... 52

6
Create Excel Macros Without Programming

Introduction ........................................................................................... 52

The Macro Recorder Does Not Record Everything ........................................ 53

The Macro Recorder May Fail to Capture the Logic Behind Certain Features .... 53

AutoSum ............................................................................................. 53

Quick Analysis Tool Totals ...................................................................... 56

Moving to the Edge of the Current Data Region ........................................ 59

The Macro Recorder is Limited to Manual Actions ........................................ 61

The Macro Recorder Tends to Generate Unnecessary Code ........................... 63

The Macro Recorder and Dialog Boxes ..................................................... 64

Other Common Instances of Inefficient Recorded Code .............................. 67

Despite Some Shortcomings, the Macro Recorder Continues to be Useful ....... 67

Some of the Main Uses of the Macro Recorder ............................................ 68

The Macro Recorder as a Tool to Create Basic Macros ................................ 68

The Macro Recorder as a Learning Tool .................................................... 68

The Macro Recorder as a Tool to Create Advanced Macros ......................... 69

Summary and Key Concepts ..................................................................... 69

Chapter 7: Delete Recorded Macros ............................................................. 71

Introduction ........................................................................................... 71

How to Delete Macros .............................................................................. 71

Delete a Macro from the Excel Interface ..................................................... 71

Summary and Key Concepts ..................................................................... 72

Next Steps................................................................................................ 74

I Want to Hear from You! ......................................................................... 75

7
Create Excel Macros Without Programming

Table of Figures
Figure 1: The Ribbon does not display the Developer tab by default. ................ 17

Figure 2: To display the Developer tab (steps #1 and #2): Right-click the Ribbon
→ select Customize the Ribbon.................................................................... 17

Figure 3: To display the Developer tab (steps #3 to #5): Add checkmark to


Developer tab checkbox → click OK. ............................................................ 18

Figure 4: Excel adds the Developer tab to the Ribbon. ................................... 18

Figure 5: The Developer tab. ...................................................................... 19

Figure 6: Relative cell references adjust when copied to another location. ........ 27

Figure 7: Cell C8 contains a relative reference to cell A5. ................................ 27

Figure 8: When you copy the formula in cell C8 to other cells, Excel adjusts the
relative cell references. .............................................................................. 28

Figure 9: Absolute cell references do not adjust when copied to another location.
............................................................................................................... 28

Figure 10: Cell C9 contains an absolute reference to cell A5. ........................... 28

Figure 11: When you copy the formula in cell C9 to other cells, Excel does not
adjust the absolute cell references. .............................................................. 29

Figure 12: Cell A10 is selected prior to recording macro. ................................ 31

Figure 13: Cell A5 is selected while the Macro Recorder is on. ......................... 31

Figure 14: If A10 is the active cell prior to macro execution, both macros select cell
A5. .......................................................................................................... 31

Figure 15: If B10 is the active cell prior to macro execution, the macro recorded
with absolute references selects cell A5. ....................................................... 32

Figure 16: If B10 is the active cell prior to macro execution, the macro recorded
with relative references selects cell B5. ........................................................ 32

Figure 17: Cell A7 is selected prior to recording macro. Cells A5 and A6 contain
values. ..................................................................................................... 35

Figure 18: A worksheet formula is entered in the active cell while the Macro
Recorder is on. .......................................................................................... 35

8
Create Excel Macros Without Programming

Figure 19: The Use Relative References button is in the Code group of commands
of the Developer tab. ................................................................................. 36

Figure 20: When the Use Relative References button is of a darker shade, you are
recording with relative references. ............................................................... 36

Figure 21: You can use the Go To dialog box to create an absolute reference to a
cell while recording a macro. ....................................................................... 37

Figure 22: In this Chapter, you create a macro that enters and formats text in the
active worksheet. ...................................................................................... 40

Figure 23: Use relative references when recording the macro example. ............ 41

Figure 24: Use the Record Macro button in the Developer tab or the Status bar. 41

Figure 25: The Record Macro dialog box. ...................................................... 42

Figure 26: Enter a descriptive and meaningful macro name. ........................... 42

Figure 27: Assigning a keyboard shortcut to a recorded macro is not mandatory.


............................................................................................................... 43

Figure 28: Choose where the recorded macro is stored. ................................. 44

Figure 29: This Workbook is the default macro storage option. ........................ 44

Figure 30: Entering a macro description is not mandatory. ............................. 45

Figure 31: Excel displays the Stop Recording button while you record a macro. . 45

Figure 32: Format the active cell as bold and enter text. ................................ 46

Figure 33: Enter your name. ....................................................................... 46

Figure 34: Enter "Power Spreadsheets". ....................................................... 46

Figure 35: Select the cells containing text and autofit the column width. .......... 46

Figure 36: Select cells and specify their font color. ........................................ 47

Figure 37: Use the Stop Recording button in the Developer tab or the Status bar.
............................................................................................................... 47

Figure 38: Every time you press the assigned keyboard shortcut, Excel executes
the macro. ................................................................................................ 48

Figure 39: Save a Macro-Enabled Workbook (steps #1 and #2): Open the Save As
dialog box → enter the file name. ................................................................ 49

9
Create Excel Macros Without Programming

Figure 40: Save a Macro-Enabled Workbook (steps #3 and #4): Expand the Save
as type drop-down list → select Excel Macro-Enabled Workbook. ..................... 50

Figure 41: Save a Macro-Enabled Workbook (step #5): Click Save. ................. 50

Figure 42: Both tables have a single column, but the table in column C has one
more row. ................................................................................................ 53

Figure 43: Select cell A9 prior to recording the macro. ................................... 54

Figure 44: Enter data in the Record Macro dialog box. .................................... 54

Figure 45: AutoSum identifies the appropriate cells to work with. .................... 55

Figure 46: The recorded macro fails to identify the appropriate cells to work with.
............................................................................................................... 55

Figure 47: The tables have a different number of rows and columns. ............... 56

Figure 48: Select cell A6 prior to recording the macro. ................................... 56

Figure 49: Enter data in the Record Macro dialog box. .................................... 57

Figure 50: While recording the macro: Select the data around the active cell. ... 57

Figure 51: While recording the macro: Click Quick Analysis Tool → select Totals →
select Sum. .............................................................................................. 57

Figure 52: The Quick Analysis Tool identifies the appropriate cells to work with. 58

Figure 53: The recorded macro fails to identify the appropriate cells to work with.
............................................................................................................... 58

Figure 54: The tables are the same size, but the second table contains an empty
cell. ......................................................................................................... 59

Figure 55: Select cell A6 prior to recording the macro. ................................... 60

Figure 56: Enter data in the Record Macro dialog box. .................................... 60

Figure 57: Ctrl+Down Arrow takes you to the bottom edge of the current data
region. ..................................................................................................... 61

Figure 58: The recorded macro does not necessarily take you to the last row. .. 61

Figure 59: VBA allows you to work with additional tools and features. .............. 63

Figure 60: Enter data in the Record Macro dialog box. .................................... 64

10
Create Excel Macros Without Programming

Figure 61: Go to Page Layout → expand the Orientation drop-down → select


Landscape. ............................................................................................... 64

Figure 62: When you work with the Page Setup dialog box, the Macro Recorder
generates code that regulates all the settings in the dialog box. ...................... 67

Figure 63: To delete a macro with the Macro dialog box (steps #1 to #3): Go to
the Macro dialog box → select the macro → click Delete. ................................ 72

Figure 64: To delete a macro with the Macro dialog box (steps #4 and #5): Click
Yes to confirm the macro deletion................................................................ 72

11
Create Excel Macros Without Programming

Introduction
About this Book
As implied by its title, the main purpose of this Book is to help you start creating
macros without programming.

As I explain in Introduction to Excel Macros & VBA, my experience is that the main
reason people shy away from working with macros and VBA is because these tools
are usually associated with programming.

However, as you learn in this Book, the association several people make between
macros and programming is not always correct. You can work with basic macros
without programming.

In other words, Excel has a set of tools and features that allow you start creating,
and working with, macros even if you have no programming knowledge or
experience.

One of the main goals of this Book is to help you during the first stages of learning
how to work with macros and VBA. I am mindful that to learn about, and master,
Excel macros and VBA you need to take the right approach.

In this Book, you learn about certain Excel tools and features that are designed to
help you work with, and learn about, macros. You can apply the principles,
processes and strategies you learn in this Book to create basic but very powerful
macros.

In the short term, this Book should help you start creating basic but useful macros.
In the medium and long term, the tools and features you learn about help build the
knowledge foundation you need to become a powerful Excel macro and VBA user.

About J.A. Gomez


This is a training and reference book on macros and VBA.

The learning techniques and strategies I use in the following Chapters were first
applied to my own process of mastering macros and VBA.

I am originally from Colombia, a country in South America. However, I have been


very lucky and have had the opportunity to live, study and work in several
countries, including:

12
Create Excel Macros Without Programming

• Spain;

• South Korea;

• Singapore;

• Germany; and

• Canada.

Nowadays, I spend most of my time working on and researching about:

• Automation;

• Excel;

• Macros; and

• VBA.

However, this was not always the case.

In fact, my undergraduate degree is in Law. Even though I also studied Economics,


I only started my process of becoming an advanced Excel user when I decided to
stop working as a Lawyer and focus on Finance.

At times, I faced common self-limiting beliefs and doubts. These beliefs ranged
from believing I was not very good at Math to thinking I would never be able to
learn how to program.

I was soon proved wrong. From the moment I decided to focus on Finance, data
analysis, automation and Excel, I have achieved a lot. This includes obtaining a
Masters in Quantitative Finance with Merit and offers for several good-paying jobs
(in a variety of countries and industries) where advanced knowledge of Excel was
a requirement.

These achievements, however, pale in comparison to the work I have done at my


website, PowerSpreadsheets.com. Power Spreadsheets has grown to become one
of the largest and most popular websites about Excel macros and VBA. I am grateful
that (every day) I get to help thousands of Excel users, just like you, become more
powerful and efficient.

The main reason I share my story is because I firmly believe that, if I could
overcome my limitations and self-limiting beliefs to master Excel macros and VBA,
you also can. I did not have any unique gifts, traits or experiences to indicate that
I would be able to become an authority in the field of Excel macros and VBA.

13
Create Excel Macros Without Programming

I decided to learn about these topics. I have invested a lot of time and money
towards achieving this goal. This experience of learning about Excel macros and
VBA has allowed me to organize and structure the information anybody, like you,
needs to learn about these topics.

How this Book is Organized


This Book is divided in seven Chapters.

• In Chapter 1, you learn how to set up Excel to work with macros and VBA.

• In Chapter 2, you learn about the tool (known as the Macro Recorder) you
work with when creating Excel macros without programming.

• In Chapter 3, you learn how to plan and prepare prior to creating a macro
without programming.

• In Chapter 4, you learn about one of the most important choices you make
prior to recording a macro: Whether to use absolute or relative cell references.

• In Chapter 5, you learn the step-by-process you can follow to create Excel
macros without programming.

• In Chapter 6, you learn about:

• The main limitations of the Macro Recorder; and

• How to determine whether a specific situation is suitable for using the Macro
Recorder.

• In Chapter 7, you learn how to delete (recorded) macros from Excel.

More Information
This Book is accompanied by a website where you can download several files
containing the data and macros used in this Book's examples. The example files
inside the Book's website are organized according to the Chapters and Sections of
this Book. This makes it easy for you to find what you need as you read along.

Click here to:

• Visit the Book's website;

• Sign-up to the Power Spreadsheets Newsletter; and

• Get immediate free access to the example files that accompany this Book.

14
Create Excel Macros Without Programming

In addition to authoring this Book, J.A. Gomez is the creator of one of the most
successful Excel macro and VBA websites. You can learn more about J and obtain
free supplemental training materials at PowerSpreadsheets.com.

15
Create Excel Macros Without Programming

Chapter 1: Set Up Excel to Work with


Macros and VBA
In this Chapter, you learn:

• Why you must set up Excel to work with macros and VBA.

• What the Developer tab is.

• How to get Excel to display the Developer tab.

• What commands and command groups are accessed through the Developer
tab.

• What (two) buttons of the Developer tab you work with when creating macros
without programming.

Introduction
If you are planning to work with macros and VBA, you must have access to the
right tools and features. However, Excel does not display by default the set of tools
and features you use when working with macros and VBA.

Therefore, before you start creating macros or working with VBA code, you must
gain access to these tools and features.

For these purposes, you follow a simple process that gets Excel to display an
additional Ribbon tab. You usually do this only once, unless you are constantly:

• Working from different computers; or

• Modifying Excel's settings.

Introduction to the Developer Tab


The Ribbon tab you add to Excel to work with macros and VBA is known as the
Developer tab.

As implied by its name, the Developer tab contains several commands Excel
Developers (like you) use. As you learned in the previous Section, the Developer
tab is hidden by default.

16
Create Excel Macros Without Programming

Figure 1: The Ribbon does not display the Developer tab by default.

Process to Display the Developer Tab


Make Excel display the Developer tab as follows:

#1. Right click the Ribbon.

#2. Select Customize the Ribbon in the context menu displayed by Excel.

Figure 2: To display the Developer tab (steps #1 and #2): Right-click the Ribbon → select Customize the Ribbon.

#3. Excel displays the Customize Ribbon tab of the Excel Options dialog box.
The list box located on the right side of the Excel Options dialog box includes all
the main Ribbon tabs.

• The checkboxes of the tabs that are currently displayed in the Ribbon have
a checkmark.

• The Developer checkbox is located near the end of this list and, by default,
does not have a checkmark.

• If the Developer tab checkbox already contains a checkmark:

• The tab should already be visible in the Ribbon; and

• You do not need to make any changes.

#4. If necessary, add a checkmark by clicking the Developer tab checkbox.

#5. Click the OK button.

17
Create Excel Macros Without Programming

Figure 3: To display the Developer tab (steps #3 to #5): Add checkmark to Developer tab checkbox → click OK.

After you complete this process, Excel adds the Developer tab to the Ribbon.

Figure 4: Excel adds the Developer tab to the Ribbon.

The Developer Tab


The Developer tab behaves like other Ribbon tabs. You can, for example, click it to
see its commands.

The Developer tab contains four main groups of commands:

#1. Code.

#2. Add-ins.

#3. Controls.

#4. XML.

18
Create Excel Macros Without Programming

Figure 5: The Developer tab.

The Code Group of Commands


In this Book, you learn how to work with the following two buttons from the Code
group of commands:

#1. Record Macro; and

#2. Use Relative References.

As you learn in Chapters 4 and 5, these commands are essential to create macros
without programming.

More generally, you use the buttons in the Code group of commands to:

• Create macros;

• Edit macros; and

• Execute macros.

The Add-ins, Controls and XML Groups of Commands


This Book does not cover the commands in the Add-ins, Controls or XML groups of
commands in the Developer tab.

• The Controls group of commands gives you access to several controls you can
use in your worksheets or UserForms. UserForms allow you create custom dialog
boxes.

• The Add-ins group of commands allows you to work with, and manage, add-
ins.

• The XML group of commands allows you to work with XML files. The main
purpose of XML files is to store and transport data.

19
Create Excel Macros Without Programming

Summary and Key Concepts


• When working with macros and VBA, several of the tools and features you
need are in the Developer tab of the Ribbon.

• Excel does not display the Developer tab by default. You can, however, get
Excel to display the Developer tab by customizing the Ribbon with the Excel
Options dialog box.

• The Developer tab contains four groups of commands:

#1. Code;

#2. Add-ins;

#3. Controls; and

#4. XML.

• To create macros without programming, you work with two buttons from the
Code group of commands:

#1. Record Macro; and

#2. Use Relative References.

20
Create Excel Macros Without Programming

Chapter 2: Introduction to the Macro


Recorder
In this Chapter, you learn:

• The two main ways in which you can create macros when working with Excel.

• What the Macro Recorder (the tool you use to create macros without
programming) is.

• How the Macro Recorder helps you create macros.

• The basic process you follow to create macros with the Macro Recorder.

• Why the Macro Recorder is such an important and useful tool for Excel macro
and VBA users of all levels.

How You Create Macros in Excel


When you work with Excel, you can create macros in two ways:

#1. By recording a macro with the Macro Recorder.

#2. By writing VBA code in the Visual Basic Editor (VBE).

In this Book, you learn how to create macros without programming. Therefore, you
focus on learning how to work with the Macro Recorder.

What the Macro Recorder Is


The main idea behind the Macro Recorder is that you can create a macro while
working manually in Excel.

In other words, the Macro Recorder allows you to create macros by following a
three-step process:

#1. Turn on the Macro Recorder.

#2. Manually carry out the tasks you want to record.

#3. Turn off the Macro Recorder.

As you manually carry out the activities you want to record (step #2), the Macro
Recorder works behind the scenes to:

21
Create Excel Macros Without Programming

• Convert your actions into VBA code; and

• Create a macro.

After the macro is recorded, you can command Excel to execute the macro as, and
when, required.

Why the Macro Recorder is Important


Since the Macro Recorder converts your actions into VBA code, you can start
creating macros without knowing VBA.

There are several reasons why you may want to, later, learn to code in VBA.
However, in this Book you learn how to create Excel macros without programming.
The Macro Recorder is the tool that allows you to do this.

As you learn in Chapter 6, the Macro Recorder is not perfect. However, it is an


essential tool for creating macros regardless of your VBA proficiency.

• As a beginner, you may find that the Macro Recorder is great for creating
simple macros.

• At more advanced levels, you are likely to find that the Macro Recorder has
important uses and (usually) continues to play a role in the process of macro
creation.

Because of these reasons, having a good understanding of the Macro Recorder,


how it works, its strengths and weaknesses, is essential for any macro and VBA
user.

Summary and Key Concepts


• When you work with Excel, you can create macros by:

#1. Working with the Macro Recorder; or

#2. Writing VBA code in the Visual Basic Editor (VBE).

• The Macro Recorder allows you to create macros by manually carrying out (and
recording) the activities you want to automate. The Macro Recorder does this by
converting your actions into VBA code.

• The Macro Recorder is commonly used by Excel macro and VBA users of all
levels.

22
Create Excel Macros Without Programming

Chapter 3: Planning and Preparing Prior to


Creating an Excel Macro
In this Chapter, you learn:

• The reasons why you must usually carry out some planning and setup activities
prior to recording a macro.

• The questions you can ask yourself to determine the appropriate setup prior
to recording a macro.

Introduction
In Chapter 2, you learned that recording a macro with the Macro Recorder is the
easiest way to create basic macros.

Although the process of creating a macro with the Macro Recorder is very easy,
you usually do some planning or setup work prior to recording the macro.

Basics of Macro Recording Planning or Setup


The level of planning or setup required prior to recording a macro depends, mostly,
on the complexity of the macro you create.

At a basic level, you usually want to ensure the appropriate conditions for recording,
and later executing, the macro are met. You can usually start by answering the
following questions:

• How should you set up the workbook you are working on?

• Should you select any object prior to recording the macro?

• Should the Macro Recorder record absolute or relative references to the cell
ranges you work with? You learn how to choose between recording absolute and
relative references in Chapter 4.

The answers to these questions usually depend on the specific characteristics of


the situation you are dealing with. For these purposes, you can consider the
following questions:

• What do you want to achieve? What is the purpose of the macro you want to
record?

23
Create Excel Macros Without Programming

• In which context do you expect to use the recorded macro? Should the macro
have a broad or a restricted scope?

• Do the expectations you have influence the setup of the Excel workbook prior
to recording the macro? Should the macro itself carry out part of the set-up
work?

Appropriate Planning and Setup are Important


Appropriate planning and setup prior to creating an Excel macro are important.
Among others, appropriate planning and setup:

• Increase the chances that your macros work properly; and

• Usually result in macros with higher quality VBA code.

The Impact of Planning and Setup on the Quality of Recorded VBA


Code
As you learned in Chapter 2, the Macro Recorder converts your actions into VBA
code. In other words, the Macro Recorder:

• Records virtually everything you do while the Macro Recorder is turned on;
and

• Translates it to VBA.

Therefore, the Macro Recorder records:

• Mistakes;

• Unnecessary movements; and

• Inaccurate indications.

This has the following two main consequences:

#1. Recorded mistakes, unnecessary movements or inaccurate indications are


repeated every time you execute the macro in the future.

#2. Recorded VBA code includes additional statements reflecting any recording
mistakes, unnecessary movements and inaccurate indications.

In other words, mistakes, unnecessary movements and inaccurate indications


result in longer code with unnecessary statements.

24
Create Excel Macros Without Programming

Longer and verbose code is more difficult to work with than short and concise code.
Shorter and concise macros are also easier to edit and fix (if necessary).

In other words, your objective is to have the least amount of VBA code possible to
appropriately handle the task at hand. When recording a macro, you usually
achieve this by:

• Ensuring your movements are accurate and precise;

• Avoiding unnecessary movements or steps; and

• Avoiding mistakes.

Summary and Key Concepts


• Prior to recording a macro, you usually carry out some planning and setup
activities to ensure the appropriate conditions for recording (and later executing)
the macro are met.

• Prior to recording a macro, you should usually answer the following questions:

• How should you set up the Excel workbook you are working on?

• Should you select any object prior to recording the macro?

• Should you use absolute or relative references while recording the macro?

• Appropriate planning and setup:

• Increase the chances that your macros work properly; and

• Usually result in macros with higher quality VBA code.

• When recording a macro, you usually seek to:

• Ensure your movements are accurate and precise;

• Avoid unnecessary movements or steps; and

• Avoid mistakes.

25
Create Excel Macros Without Programming

Chapter 4: Absolute and Relative


References When Recording Macros
In this Chapter, you learn:

• The importance of appropriately using absolute or relative cell references when


recording macros.

• How you work with relative and absolute cell references when dealing with
worksheet formulas.

• How the Macro Recorder keeps track of the cells you select and work with while
recording a macro.

• The main consequences that the choice of absolute or relative cell references
has on recorded macros.

• Why worksheet formulas you enter while recording a macro are not affected
by the absolute or relative cell references setting of the Macro Recorder.

• How you specify whether the Macro Recorder uses absolute or relative
references.

• How to toggle between absolute and relative reference use while recording a
macro.

The Importance of Understanding Cell References When


Recording Macros
In Chapter 3, you learned about the importance of appropriate planning prior to
recording a macro. As you learn in this Chapter, there is a setting that can easily
make or break your macros: The choice between:

• Absolute cell references; and

• Relative cell references.

In other words:

• Understanding the difference between absolute and relative cell references is


essential for recording useful macros.

26
Create Excel Macros Without Programming

• Failure to make the right choice (between absolute and relative references)
usually results in recorded macros that do not work as expected. This may be
the case even if the rest of your setup prior to recording the macro is flawless.

Absolute and Relative References in Excel


To understand absolute and relative cell references in the context of macro
recording, it helps to review how these concepts apply when creating worksheet
formulas.

Relative References in Excel


When you copy and paste worksheet formulas to another location, Excel usually
adjusts cell references. In other words, Excel determines the cell to which reference
is made by moving a certain number of rows and columns away from the cell with
the formula.

Figure 6: Relative cell references adjust when copied to another location.

These cell references are known as relative references.

Consider, for example, cell C8 in the worksheet displayed in Figure 7 below. The
formula in this cell refers to cell A5 using relative references (=A5).

Figure 7: Cell C8 contains a relative reference to cell A5.

If you copy the formula in cell C8 and paste it in cells D8 to G8, Excel adjusts the
cell references. Therefore:

• Cell D8 refers to cell B5;

• Cell E8 refers to cell C5;

27
Create Excel Macros Without Programming

• Cell F8 refers to cell D5; and

• Cell G8 refers to cell E5.

These are all relative references.

Figure 8: When you copy the formula in cell C8 to other cells, Excel adjusts the relative cell references.

Absolute References in Excel


In addition to the default relative references, you can use absolute cell references
when creating worksheet formulas. You do this by adding the Dollar ($) sign before
the column letter and row number.

Figure 9: Absolute cell references do not adjust when copied to another location.

When you copy and paste worksheet formulas to another location, Excel does not
automatically adjust absolute cell references.

Consider, for example cell C9 in the worksheet displayed in Figure 10 below. The
formula in this cell refers to cell A5 using absolute references (=$A$5).

Figure 10: Cell C9 contains an absolute reference to cell A5.

If you copy the formula in cell C9 and paste it in cells D9 to G9, Excel does not
adjust the cell references. Therefore, all cells continue to refer to cell A5.

28
Create Excel Macros Without Programming

Figure 11: When you copy the formula in cell C9 to other cells, Excel does not adjust the absolute cell references.

Absolute and Relative References When Recording Macros


The logic behind absolute and relative cell references you learned about in previous
Sections is also applicable, to a certain extent, to macro recording.

When you record macros, the Macro Recorder keeps track of the cells you:

• Select; and

• Work with.

Absolute References and the Macro Recorder


By default, the Macro Recorder records cell references as absolutes. In other words,
when you select a cell or cell range, the Macro Recorder stores the exact addresses
of those cells (for example, A1 or B1). As implied by their name, these absolute
cell references:

• Are hard-coded; and

• Do not change.

As you learned in a previous Section, absolute references are very:

• Literal; and

• Strict.

Macros that work with absolute references always work with the same cells.

Therefore, macros recorded with absolute references are (usually) not flexible
enough to be used in a variety of circumstances. In other words, recording macros
with absolute references tends to result in macros that do not work appropriately.

Because of these reasons, you usually do not record macros with absolute
references.

29
Create Excel Macros Without Programming

Relative References and the Macro Recorder


Just as when working with worksheet formulas, you have an alternative to absolute
references: Relative references.

When you use relative references while recording, the Macro Recorder keeps track
of the cells you work with by referring to the active cell. In other words:

• The current active cell acts as the starting point; and

• The recorded VBA code moves a specific number of rows and columns from
that starting point (the active cell) to find the new cell.

Relative references are less strict than absolute references. Therefore, they usually
result in more flexible and generic macros that you can use in a wider variety of
circumstances.

The Choice Between Absolute and Relative References When


Recording Macros
The choice of absolute or relative references has several consequences. The
following are three areas where this choice has an important impact:

#1. The behavior of your recorded macro when executed.

#2. The VBA code generated by the Macro Recorder.

#3. Your set-up prior to recording a macro.

Consequences of Absolute and Relative References on Macro Behavior

As you learned in previous Sections, the results of executing a recorded macro may
vary substantially depending on whether the macro was recorded with absolute or
relative references.

• When you record a macro with absolute references, the macro always works
with the same cells.

• When you record a macro with relative references, Excel determines the cell(s)
to work with by reference to the current active cell. Therefore, the cell(s) the
macro works with depend on the active cell prior to executing the macro.

Consider two different macros:

• Recorded by following the same process; and

30
Create Excel Macros Without Programming

• Whose only difference is the use of absolute or relative references.

Assume the process to record the macros is as follows:

#1. Cell A10 is selected.

Figure 12: Cell A10 is selected prior to recording macro.

#2. The Macro Recorder is turned on.

#3. Cell A5 is selected.

Figure 13: Cell A5 is selected while the Macro Recorder is on.

#4. The Macro Recorder is turned off.

In other words, both macros select a cell. The cell that is selected upon macro
execution depends on two main factors:

#1. The macro version (with absolute or relative references) you execute; and

#2. The active cell prior to the macro being executed.

The following two examples illustrate these differences:

#1. If the active cell prior to macro execution is cell A10, both macros select cell
A5.

Figure 14: If A10 is the active cell prior to macro execution, both macros select cell A5.

#2. If the active cell prior to macro execution is cell B10, the macros select
different cells, as follows:

31
Create Excel Macros Without Programming

• The macro that works with absolute references selects cell A5.

Figure 15: If B10 is the active cell prior to macro execution, the macro recorded with absolute references selects cell
A5.

• The macro that works with relative references selects cell B5.

Figure 16: If B10 is the active cell prior to macro execution, the macro recorded with relative references selects cell
B5.

The differences in the selected cells are the result of how each macro determines
the cell to select.

• The macro that is recorded with absolute references always selects cell A5.

• The macro that is recorded with relative references selects the cell five rows
above the current active cell. Therefore:

• If the active cell is cell A10 (as when the macro is recorded), the macro
selects cell A5 (five rows above) when executed.

• If the active cell is cell B10, the macro selects cell B5 (five rows above)
when executed.

Consequences of Absolute and Relative References on VBA Code

The differences in behavior between macros with absolute and relative references
are a consequence of the differences in the recorded VBA code. In other words, the
Macro Recorder creates different VBA code depending on whether you record
macros with absolute or relative references.

In the previous Section, you analyzed the behavior of two example macros that
select a cell. These macro examples:

32
Create Excel Macros Without Programming

• Are recorded by following the same process; and

• Only differ on the type of cell references (absolute vs. relative) they use.

The following are the VBA statements generated by the Macro Recorder:

• When the macro is recorded with absolute references: Range("A5").Select.

• When the macro is recorded with relative references: ActiveCell.Offset(-


5, 0).Range("A1").Select.

For purposes of this Book, you do not need to understand all details of these
statements. Simply notice the following:

• When working with absolute references, the Macro Recorder hard-codes the
cell address. In this example, Range("A5") refers to cell A5 of the active
worksheet.

• When working with relative references, the Macro Recorder does the following:

• Sets the active cell (ActiveCell) as base.

• Moves five rows up while remaining in the same column as the active cell
(Offset(-5, 0)).

• Includes a reference to Range("A1"), although this is a quirk caused by the


way the Macro Recorder translates your actions into VBA code. In practice
you can usually delete this reference (the statement becomes
ActiveCell.Offset(-5, 0).Select) and the macro continues to work
properly.

To summarize:

• When the macro is recorded with absolute references, the Macro Recorder
hard-codes a cell address.

• When the macro is recorded with relative references, the Macro Recorder
determines the cells to work with by reference to a base, which is initially the
active cell.

Consequences of Absolute and Relative References on Macro Recording


Setup

The differences between absolute and relative references influence how you set
Excel up prior to recording a macro.

33
Create Excel Macros Without Programming

When recording a macro with absolute references, you usually select the cells you
work with after you turn the Macro Recorder on. This is because these macros
always work with the same cells. By selecting the cells you work with after you
begin recording, you ensure the appropriate cell addresses are hard-coded in the
resulting VBA code.

When recording a macro with relative references, you usually select the first cell(s)
you work with before turning the Macro Recorder on. This is because these macros
use the active cell as the base from which they determine the cells to work with
and manipulate.

Cell References in Worksheet Formulas Entered While


Recording Macros
In the previous Sections, you learned that the logic behind absolute and relative
references is similar for both:

• Worksheet formulas; and

• The Macro Recorder.

Despite this, worksheet formulas entered by recorded macros are not affected by
the absolute or relative cell references setting of the Macro Recorder. In other
words, if you enter a worksheet formula while recording a macro:

• You enter it as usual and without any consideration for the macro recording
setting (absolute or relative references).

• The worksheet formula stored in the recorded VBA code is the same regardless
of the macro recording setting (absolute or relative references).

Consider two different macros:

• Recorded by following the same process; and

• Whose only difference is the use of absolute or relative references.

Assume the setup and process to record the macros is as follows:

#1. Cells A5 and A6 contain certain values.

#2. Cell A7 is selected.

34
Create Excel Macros Without Programming

Figure 17: Cell A7 is selected prior to recording macro. Cells A5 and A6 contain values.

#3. The Macro Recorder is turned on.

#4. A worksheet formula (=SUM(A5:A6)) is entered in the active cell.

Figure 18: A worksheet formula is entered in the active cell while the Macro Recorder is on.

#5. The Macro Recorder is turned off.

When cell A7 is the active cell and the macros are executed, both macros enter the
same worksheet formula (=SUM(A5:A6)).

In fact, the VBA statement generated by the Macro Recorder is the same regardless
of the use of relative or absolute cell references: ActiveCell.FormulaR1C1 =
"=SUM(R[-2]C:R[-1]C)". Notice the following:

• Both macros work with the active cell (ActiveCell).

• Both macros enter the same worksheet formula that uses the SUM function
(=SUM(R[-2]C:R[-1]C)).

The structure of the worksheet formula entered by these example macros


(=SUM(R[-2]C:R[-1]C)) is probably different to the one you are used to work with.
For purposes of this Chapter, just notice how both recorded macros:

• Contain the same VBA statement; and

• Use the same worksheet formula syntax.

Choose Between Absolute and Relative References


When recording macros, you can toggle the use of relative references on and off
by clicking the Use Relative References button in the Code group of commands of
the Developer tab.

35
Create Excel Macros Without Programming

Figure 19: The Use Relative References button is in the Code group of commands of the Developer tab.

• If the button is of the same color as the other Ribbon buttons (the default),
you are recording with absolute references.

• If the button is of a darker shade of gray than the other Ribbon buttons, you
are working with relative references.

Figure 20: When the Use Relative References button is of a darker shade, you are recording with relative references.

The Use Relative References Button While Recording a Macro


You can use the Use Relative References button while recording a macro. This
allows you to switch between absolute and relative references while recording. You
can use this, for example:

• When you are recording with relative references but, for a part of the macro,
need to create absolute references; or

• Vice-versa, when you are recording with absolute references but, for a part of
the macro, need to create relative references.

Create Absolute References with the Go To Dialog Box


If you need to create an absolute reference while recording with relative references,
you can also use the Go To dialog box. To create a cell reference with the Go To
dialog box, do the following:

#1. Press the F5 key.

This keyboard shortcut works for English (United States) language settings.
Depending on your language settings, you may have to use a different keyboard
shortcut to achieve the same result.

#2. Excel displays the Go To dialog box.

36
Create Excel Macros Without Programming

#3. Enter the address of the cell you want to go to in the Reference field.

#4. Click the OK button.

Figure 21: You can use the Go To dialog box to create an absolute reference to a cell while recording a macro.

When you create absolute cell references by using the Go To dialog box, the Macro
Recorder usually hard-codes the cell address.

Strictly speaking, the VBA construct used by the Macro Recorder to create absolute
references differs depending on whether you create it by:

• Working with the Go To dialog box; or

• Recording the macro with absolute references.

However, the practical effect is materially the same. In both cases:

• When executed, the macro selects the same cell always regardless of what the
current active cell is; and

• The Macro Recorder generates VBA code where the cell address is hard-coded.

Summary and Key Concepts


• The choice between absolute and relative references has important effects on
the behavior of recorded macros.

• When you work with worksheet formulas, cell references are relative by
default. When you copy worksheet formulas with relative references to another
location, Excel adjusts cell references automatically.

• You can create worksheet formulas that use absolute cell references. When
you copy worksheet formulas with absolute references to another location, Excel
does not adjust those cell references.

37
Create Excel Macros Without Programming

• The Macro Recorder keeps track of the cells you select and work with while
recording a macro.

• By default, the Macro Recorder uses absolute references. The resulting


macros:

• Always work with the same cells; and

• Are (usually) not flexible enough to be used in a variety of circumstances.

• You can record macros that use relative cell references. The resulting
macros are usually flexible enough to be used in a wide variety of
circumstances.

• The VBA code of recorded macros usually reflects your use of absolute or
relative references while recording.

• Absolute references usually result in hard-coded cell addresses.

• Relative references usually result in cell references with respect to a base


cell, which is usually the active cell. The macro moves a certain number of
rows and columns away from that base cell.

• When recording a macro with absolute references, you usually select the cells
you work with after turning the Macro Recorder on. This ensures the appropriate
cell addresses are hard-coded in the VBA code.

• When recording a macro with relative references, you usually select the first
cell(s) you work with before turning the Macro Recorder on. This ensures the
macro uses the active cell as the initial base to determine the cells to work with.

• The worksheet formulas you enter while recording a macro are not affected by
the absolute or relative cell references setting of the Macro Recorder.

• You specify whether the Macro Recorder uses absolute or relative references
by working with the Use Relative References button.

• You can use the Use Relative References button while recording a macro. This
allows you to toggle between absolute and relative references while recording a
single macro.

• You can also create an absolute cell reference while recording a macro by using
the Go To dialog box.

38
Create Excel Macros Without Programming

Chapter 5: Create Excel Macros Without


Programming (Record a Macro)
In this Chapter, you learn:

• The step-by-step process you follow to create macros without programming.

• How to turn the Macro Recorder on and off.

• How to specify a recorded macro's details, including its:

• Name; and

• Description.

• How to:

• Assign a keyboard shortcut to a recorded macro; and

• Use an assigned keyboard shortcut to execute a recorded macro.

• The two basic rules to follow when choosing the name of a recorded macro.

• How to specify the workbook in which a recorded macro is stored.

• Which workbooks you can store macros in.

• How to appropriately save a workbook that contains macros.

Introduction
In Chapter 2, you learned the following:

• The Macro Recorder allows you to create macros without having to code.

• When working with the Macro Recorder, you create macros by following a
three-step process:

#1. Turn on the Macro Recorder.

#2. Manually carry out the tasks you want to record.

#3. Turn off the Macro Recorder.

In Chapters 3 and 4, you learned that, although working with the Macro Recorder
is the easiest way to create a macro, you usually do some planning or setup work
prior to recording a macro.

39
Create Excel Macros Without Programming

In this Chapter, you:

• Learn more details about the process you follow when creating a macro with
the Macro Recorder; and

• Create your first macro.

The Macro Example You Create


In this Chapter, I show you the step-by process you follow to create a macro that
enters and formats certain text in the active worksheet. More precisely, this macro
does the following:

#1. Enters certain text in:

• The active cell; and

• The two cells below the active cell.

#2. Makes the text in the active cell bold.

#3. Changes the font color of the two cells below the active cell.

Figure 22: In this Chapter, you create a macro that enters and formats text in the active worksheet.

This is a simple macro. However, you can apply the same principles and process
you learn in this Chapter to create other basic but powerful macros.

Create Your First Excel Macro


To create a macro with the Macro Recorder, follow the steps I describe below. For
illustration purposes, I show how to create the macro example I describe in the
previous Section.

Step #1: Work with the Appropriate Cell References


In Chapter 4, you learned about the importance of appropriately choosing between
absolute and relative cell references when recording a macro.

Therefore, before you start working with the Macro Recorder, ensure Excel is
working with the appropriate cell references (absolute or relative).

The macro example you create in this Chapter always works with:

40
Create Excel Macros Without Programming

• The active cell; and

• The two cells below it.

In other words, the recorded macro must always determine the cells it works with
by reference to the current active cell. These are relative references.

Therefore, enable relative reference recording by clicking the Use Relative


References button in the Developer tab.

Figure 23: Use relative references when recording the macro example.

Step #2: Turn the Macro Recorder On


To turn the Macro Recorder on, click the Record Macro button in:

• The Developer tab; or

• The left side of the Status bar.

Figure 24: Use the Record Macro button in the Developer tab or the Status bar.

Step #3: Specify the Macro's Details with the Record Macro Dialog
Box
After you turn the Macro Recorder on, Excel displays the Record Macro dialog box.
This dialog box allows you to enter the following four details:

#1. Macro name.

#2. Shortcut key.

#3. Store Macro in.

41
Create Excel Macros Without Programming

#4. Description.

Enter the appropriate data (as explained in the Sections below) in the Macro dialog
box and click the OK button.

Figure 25: The Record Macro dialog box.

Macro Name

By default, Excel suggests a generic macro name (Macro1, Macro2, etc.).

You usually want to enter a more descriptive and meaningful name. Macro names
must comply with several rules. For purposes of this Book, ensure your macro name
complies with the following two requirements:

#1. Starts with a letter.

#2. Does not contain any spaces.

For purposes of the example you create in this Chapter, use "enterIdData".

Figure 26: Enter a descriptive and meaningful macro name.

Shortcut Key

When recording macros, you have the option to assign a shortcut key combination
that (later) launches the macro. This field is not required.

42
Create Excel Macros Without Programming

Shortcut keys must be of the form:

• Ctrl+Key; or

• Ctrl+Shift+Key.

When you specify the key combination in the Record Macro dialog box, you omit
the Ctrl key. In other words, you only type the:

• Key; or

• Shift+Key combination.

For purposes of the example you create in this Chapter, assign the keyboard
shortcut Ctrl+Shift+E. Therefore, press Shift+E.

Figure 27: Assigning a keyboard shortcut to a recorded macro is not mandatory.

Store Macro In

Use the Store macro in drop-down list to select the workbook in which your macro
is stored. You have the following three options:

#1. This Workbook, which refers to the current workbook.

#2. New Workbook, which refers to a new workbook.

#3. Personal Macro Workbook, which refers to the Personal Macro Workbook.

43
Create Excel Macros Without Programming

Figure 28: Choose where the recorded macro is stored.

This Workbook is the default option and results in the VBA code of the recorded
macro being stored in the current active workbook. Therefore, the macro is
available to you and other users that work with that workbook.

New Workbook, as implied by its name, results in Excel creating a new workbook
where the recorded VBA code is stored.

The Personal Macro Workbook is a special workbook you can normally use as
storage for macros you constantly use in different files. You can learn more about
the Personal Macro Workbook in Introduction to Excel Macro Security and Storage.

For purposes of the example you create in this Chapter, leave the default option:
This Workbook.

Figure 29: This Workbook is the default macro storage option.

Description

The last field of the Record Macro dialog box allows you to enter a description of
the macro. This helps you and other users:

• Identify what the macro does; or

• Remember the conditions under which the macro should be executed.

44
Create Excel Macros Without Programming

The Description field is optional.

For purposes of the example you create in this Chapter, type "Enters and formats
identification text, starting in the active cell."

Figure 30: Entering a macro description is not mandatory.

Step #4: Perform Actions to Record


After you click the OK button in the Record Macro dialog box:

• Excel turns the Macro Recorder on; and

• The Record Macro button is replaced by a Stop Recording button.

Figure 31: Excel displays the Stop Recording button while you record a macro.

While the Macro Recorder is on, you carry out the actions you want to record.

For purposes of the example you create in this Chapter, do the following:

#1. Make the text in the active cell bold by, for example, pressing the Ctrl+B
keyboard shortcut.

#2. Type "Create Excel Macros Without Programming".

#3. Press the Ctrl+Enter keyboard shortcut to confirm the entry while
remaining in the same cell.

45
Create Excel Macros Without Programming

Figure 32: Format the active cell as bold and enter text.

#4. Press the Down Arrow key to move one cell down.

#5. Type your name. In my case, that is "J.A. Gomez".

#6. Press the Ctrl+Enter keyboard shortcut to confirm the entry while
remaining in the same cell.

Figure 33: Enter your name.

#7. Press the Down Arrow key to move one cell down.

#8. Type "Power Spreadsheets".

#9. Press the Ctrl+Enter keyboard shortcut to confirm the entry while
remaining in the same cell.

Figure 34: Enter "Power Spreadsheets".

#10. Press the Up Arrow key twice to select the cell containing "Create Excel
Macros Without Programming".

#11. Press the Shift key and, while keeping it pressed, press the Down Arrow
key twice to expand the selection to include the cells containing your name and
"Power Spreadsheets".

#12. Autofit the column width by, for example, using the Alt, H, O, I keyboard
shortcut.

Figure 35: Select the cells containing text and autofit the column width.

#13. Press the Down Arrow key to move one cell down and select the cell
containing your name.

46
Create Excel Macros Without Programming

#14. Press the Shift+Down Arrow keyboard shortcut to expand the selection to
include the cell containing "Power Spreadsheets".

#15. Expand the font-color gallery in the Home tab of the Ribbon and select a
gray color.

Figure 36: Select cells and specify their font color.

The keyboard shortcuts I use in the process described above work for English
(United States) language settings. Depending on your language settings, you may
have to use different keyboard shortcuts to achieve the same results.

Step #5: Turn the Macro Recorder Off


To complete the process of recording a macro, click the Stop Recording button on:

• The Developer tab; or

• The left side of the Status bar.

Figure 37: Use the Stop Recording button in the Developer tab or the Status bar.

After you click the Stop Recording button, it is replaced by the Record Macro button.

47
Create Excel Macros Without Programming

Execute a Recorded Macro with Keyboard Shortcuts


You can execute macros in several ways. You can learn more about macro
execution in Excel Macro Execution Basics.

However, as you learned in a previous Section, you can easily assign a keyboard
shortcut to any recorded macro you create. Keyboard shortcuts are, therefore, a
very common way to execute recorded macros.

You can execute the macro example created in this Chapter by pressing the
Ctrl+Shift+E keyboard shortcut as required.

Figure 38: Every time you press the assigned keyboard shortcut, Excel executes the macro.

Each time you execute the macro example, it works with the active cell and the
two cells below it. This is a result of you:

• Recording the macro using relative references; and

• Following the appropriate process when recording the macro.

Save a Macro-Enabled Excel Workbook


Not all Excel files can contain macros. This is an important security feature with
wide-reaching implications.

• At a basic level, the fact that not all Excel files can contain macros helps you
control your own security.

• At a broader level, this feature influences the file formats you can use when
working with macros and VBA.

Therefore, this security feature has direct consequences on the way in which you
save workbooks that contain macros (like the one you created in this Chapter).

The topic of macro security and Excel file types is:

48
Create Excel Macros Without Programming

• Relatively extensive; and

• Very important.

Therefore, I suggest you learn more about macro security after completing this
Book. You can learn about this topic in Introduction to Excel Macro Security and
Storage.

For purposes of this Book, is enough for you to understand that (generally) Excel
workbooks containing macros should be saved as Macro-Enabled Workbooks (.xlsm
extension). If you save your workbook using the default Excel Workbook file type
(.xlsx extension), Excel removes your macros prior to saving the file.

To save a workbook as a Macro-Enabled Workbook, follow these steps:

#1. Open the Save As dialog box by, for example, pressing the F12 key.

This keyboard shortcut works for English (United States) language settings.
Depending on your language settings, you may have to use a different keyboard
shortcut to achieve the same result.

#2. Specify the file name. For purposes of the example you created in this
Chapter, enter "Recorded Macro".

Figure 39: Save a Macro-Enabled Workbook (steps #1 and #2): Open the Save As dialog box → enter the file name.

#3. Expand the Save as type drop-down list.

#4. Select Excel Macro-Enabled Workbook.

49
Create Excel Macros Without Programming

Figure 40: Save a Macro-Enabled Workbook (steps #3 and #4): Expand the Save as type drop-down list → select
Excel Macro-Enabled Workbook.

#5. Click the Save button.

Figure 41: Save a Macro-Enabled Workbook (step #5): Click Save.

After you complete this process, Excel saves your file as a Macro-Enabled Workbook
containing the macros you created and stored in that workbook.

Summary and Key Concepts


• Follow this step-by-step process to record a macro:

#1. Ensure you are working with absolute or relative references, as required.

#2. Click the Record Macro button.

#3. Specify the macro's details in the Record Macro dialog box.

• Name: For the moment, specify a macro name that starts with a letter
and contains no spaces.

• Keyboard shortcut (optional): Keyboard shortcuts must be of the form


Ctrl+Key or Ctrl+Shift+Key. When you specify the keyboard shortcut,
omit the Ctrl key.

50
Create Excel Macros Without Programming

• Workbook where macro is stored: For the moment, store macros in the
workbook you are working with (This Workbook). This is the default option.

• Description (optional).

#4. Carry out the actions to record.

#5. Turn the Macro Recorder off.

• If you assign a keyboard shortcut to a recorded macro, you can use this
keyboard shortcut to execute the macro when required.

• Save workbooks containing macros as Excel Macro-Enabled Workbooks (.xlsm


extension). You can specify this extension with the Save as type drop-down list
of the Save As dialog box.

51
Create Excel Macros Without Programming

Chapter 6: When to Create Macros with the


Macro Recorder
In this Chapter, you learn:

• The importance of understanding the strengths and weaknesses/limitations of


the Macro Recorder.

• The four main limitations of the Macro Recorder.

• How the Macro Recorder may fail to capture and reflect the logic behind certain
Excel features.

• Why the Macro Recorder tends to generate inefficient code.

• Why inefficient code generated by the Macro Recorder may be an issue.

• Why, despite its limitations, the Macro Recorder continues to be useful for both
beginner and advanced macro and VBA users.

• Some of the main uses of the Macro Recorder.

Introduction
As you learned throughout this Book, the Macro Recorder can be extremely useful.

However, the Macro Recorder is not perfect. In fact, there are some important
limitations. A better understanding of the limitations of the Macro Recorder, and its
common uses, helps you:

• Decide when to work with the Macro Recorder; and

• Understand why you:

• Cannot rely solely on the Macro Recorder; and

• Should consider learning VBA.

In this Chapter, you learn about the following four main limitations of the Macro
Recorder:

#1. The Macro Recorder does not record absolutely everything you do manually.

#2. The Macro Recorder sometimes fails to adequately capture or reflect the
logic behind certain Excel features or operations.

52
Create Excel Macros Without Programming

#3. The Macro Recorder is limited to manual actions and cannot reflect or work
with several VBA features.

#4. The Macro Recorder generates VBA code that is usually inefficient and long.

The Macro Recorder Does Not Record Everything


As you learned in Chapter 2, the Macro Recorder records most of what you do
manually.

There are, however, cases where the Macro Recorder fails to generate the code
that reflects a specific command or action. This is the case, for example, with some
actions you carry out with the Ribbon.

The Macro Recorder May Fail to Capture the Logic Behind


Certain Features
One of the most common challenges when relying on the Macro Recorder to create
macros is the fact that the Macro Recorder sometimes fails to adequately capture
and reflect the logic of certain features. This includes, for example:

• AutoSum;

• The Quick Analysis Tool Totals; or

• The keyboard shortcut that takes you to the edge of the current data region.

AutoSum
Consider, for example, the two tables (cells A5 to A8 and C5 to C9) displayed in
Figure 42 below. These tables have a single column, but a different number of rows.

Figure 42: Both tables have a single column, but the table in column C has one more row.

These tables illustrate the inability of the Macro Recorder to adequately capture the
logic behind Excel's AutoSum feature.

For these purposes, record a macro by following these steps:

53
Create Excel Macros Without Programming

#1. Enable relative references by clicking the Use Relative References button in
the Developer tab.

#2. Select cell A9.

Figure 43: Select cell A9 prior to recording the macro.

#3. Click the Record Macro button.

#4. Excel displays the Record Macro dialog box.

#5. Enter a macro name (for example, "useAutoSum") and assign a keyboard
shortcut (for example, Ctrl+Shift+A).

#6. Click the OK button.

Figure 44: Enter data in the Record Macro dialog box.

#7. Execute the AutoSum command by, for example, pressing the Alt+=
keyboard shortcut followed by the Enter key. AutoSum appropriately
determines that the cells containing the values to be used are the three cells
immediately above cell A9 (A6 to A8).

54
Create Excel Macros Without Programming

Figure 45: AutoSum identifies the appropriate cells to work with.

#8. Click the Stop recording button.

The keyboard shortcuts I use in the process described above work for English
(United States) language settings. Depending on your language settings, you may
have to use different keyboard shortcuts to achieve the same results.

To confirm that the Macro Recorder has failed to adequately capture the logic
behind AutoSum, do the following:

#1. Select cell C10.

#2. Execute the recorded macro by pressing the assigned keyboard shortcut
(Ctrl+Shift+A).

Analyze the formula Excel creates in cell C10.

Figure 46: The recorded macro fails to identify the appropriate cells to work with.

Notice that the macro fails to reflect the logic behind AutoSum. In other words, the
macro:

• Is not capable of adequately identifying the cells that should be used as


arguments in the worksheet formula; and

• Simply works with the three cells immediately above cell C10 (cells C7 to C9).

This behavior is appropriate when working with the table in column A (which you
used to record the macro and contains three values). However, the behavior is
inappropriate when working with the table in column C (which contains four
values).

55
Create Excel Macros Without Programming

Quick Analysis Tool Totals


AutoSum is not the only Excel feature the Macro Recorder struggles to adequately
capture. It faces similar challenges when working with the Quick Analysis Tool
Totals feature.

Consider, for example, the two tables (cells A5 to C8 and E5 to H9) displayed in
Figure 47 below. These tables have a different number of rows and columns.

• The first table has three rows and three columns (3x3).

• The second table has four rows and four columns (4x4).

Figure 47: The tables have a different number of rows and columns.

These tables illustrate the inability of the Macro Recorder to adequately capture the
logic behind Excel's Quick Analysis Tool Totals feature.

For these purposes, record a macro by following these steps:

#1. Enable relative references by clicking the Use Relative References button in
the Developer tab.

#2. Select cell A6.

Figure 48: Select cell A6 prior to recording the macro.

#3. Click the Record Macro button.

#4. Excel displays the Record Macro dialog box.

#5. Enter a macro name (for example, "quickAnalysisTotals") and assign a


keyboard shortcut (for example, Ctrl+Shift+Q).

#6. Click the OK button.

56
Create Excel Macros Without Programming

Figure 49: Enter data in the Record Macro dialog box.

#7. Select the data around the active cell by, for example, pressing the
Ctrl+Shift+* keyboard shortcut.

This keyboard shortcut works for English (United States) language settings.
Depending on your language settings, you may have to use a different keyboard
shortcut to achieve the same result.

#8. Excel displays the Quick Analysis Tool.

Figure 50: While recording the macro: Select the data around the active cell.

#9. Click the Quick Analysis Tool.

#10. Select Totals.

#11. Select Sum.

Figure 51: While recording the macro: Click Quick Analysis Tool → select Totals → select Sum.

57
Create Excel Macros Without Programming

#12. The Quick Analysis Tool appropriately:

• Adds the totals below the last row with data; and

• Formats those totals in bold.

Figure 52: The Quick Analysis Tool identifies the appropriate cells to work with.

#13. Click the Stop recording button.

To confirm that the Macro Recorder has failed to adequately capture the logic
behind the Quick Analysis Tool Totals feature, do the following:

#1. Select cell E6.

#2. Execute the recorded macro by pressing the assigned keyboard shortcut
(Ctrl+Shift+Q).

In this case, the macro's failure is even more evident than when working with
AutoSum. There are several issues.

Figure 53: The recorded macro fails to identify the appropriate cells to work with.

The main problem with the recorded macro is that it continues to consider a table
with three rows and three columns. This behavior is appropriate when working with
the first table in the worksheet (which you used to record the macro and has three
rows and three columns). However, the behavior is inappropriate when working
with the second table (which has four rows and four columns).

This results in the macro:

• Overwriting the data in the first three cells of the fourth table row;

• Replacing that data with the Totals; and

58
Create Excel Macros Without Programming

• Formatting the Totals as bold.

Moving to the Edge of the Current Data Region


A final example of how the Macro Recorder may fail to adequately reflect the logic
behind an action you take is when you want to go to the edge of the current data
region using the Ctrl+Arrow Key keyboard shortcut. You usually rely on this
feature to, for example, find the last row with data in a table or worksheet.

The Ctrl+Arrow Key keyboard shortcut works for English (United States) language
settings. Depending on your language settings, you may have to use a different
keyboard shortcut to achieve the same result.

Consider, for example, the two tables (cells A5 to B10 and D5 to E10) displayed in
Figure 54 below. These tables have the same size. However, a cell in the first
column of the second table (D8) is empty.

Figure 54: The tables are the same size, but the second table contains an empty cell.

These tables illustrate the challenges related to identifying the last row or column
with data in a table or worksheet. This topic is surprisingly complex and exceeds
the scope of this Book.

To understand the limitations of the Macro Recorder when dealing with this type of
situation, record a macro by following these steps:

#1. Enable relative references by clicking the Use Relative References button in
the Developer tab.

#2. Select cell A6.

59
Create Excel Macros Without Programming

Figure 55: Select cell A6 prior to recording the macro.

#3. Click the Record Macro button.

#4. Excel displays the Record Macro dialog box.

#5. Enter a macro name (for example, "goToLastRow") and assign a keyboard
shortcut (for example, Ctrl+Shift+L).

#6. Click the OK button.

Figure 56: Enter data in the Record Macro dialog box.

#7. Press the Ctrl+Down Arrow keyboard shortcut to go to the last row of the
table.

This keyboard shortcut works for English (United States) language settings.
Depending on your language settings, you may have to use a different keyboard
shortcut to achieve the same result.

The applicable keyboard shortcut correctly takes you to the last row of the table
in columns A and B.

60
Create Excel Macros Without Programming

Figure 57: Ctrl+Down Arrow takes you to the bottom edge of the current data region.

#8. Click the Stop recording button.

To understand the potential pitfalls of this approach for finding the last row with
data, do the following:

#1. Select cell D6.

#2. Execute the recorded macro by pressing the assigned keyboard shortcut
(Ctrl+Shift+L).

The macro takes you to cell D7. In the case of this table, this is not the last row.
However, since the cell below D7 (D8) is empty, the macro stops here.

Figure 58: The recorded macro does not necessarily take you to the last row.

The Macro Recorder is Limited to Manual Actions


When creating macros with the Macro Recorder, you can only record what you can
do manually.

At a basic level, this gives you access to most of the tools and features in Excel.
These tools and features may cover most of the day-to-day work you (manually)
carry out in Excel.

However, VBA has additional tools and features. You cannot access some of these
manually. Therefore, you cannot use those tools and features in a recorded macro.

In other words, to use those tools and features that help you unleash the full power
of macros, you must use VBA.

61
Create Excel Macros Without Programming

The Table in Figure 59 below lists some examples of such tools and features whose
treatment differs depending on whether you are working manually or with VBA.
Some of these tools and features are more advanced and, therefore, their
consequences may not be completely clear to you right now. I cover (in more detail)
some of these tools and features in Introduction to Excel Macros & VBA.

In any case, this Table may give you an idea of the Macro Recorder's limitations
and the power of VBA.

When you work manually… But when you work with VBA…

You can only work with the object that You can work with objects that are
is currently active or selected. neither active nor selected.

For example, if the current active cell is


A1, you can only manipulate this cell.
To manipulate another cell (such as
A2), you must first select it.

You cannot: You can both:

• Declare or create variables; nor • Declare or create variables; and

• Specify a variable's data type. • Specify a variable's data type.

You cannot create macros that receive You can create macros that receive and
or work with arguments. work with arguments.

You cannot work with certain features You can work with certain features or
or settings (for example, very hidden settings (for example, very hidden
sheets). sheets) that are not manually
accessible.

You cannot create custom worksheet You can create custom worksheet
functions. functions (also known as User-Defined
Functions).

62
Create Excel Macros Without Programming

When you work manually… But when you work with VBA…

You cannot create macros with You can create macros with decision-
decision-making capabilities making capabilities. At a basic level,
these macros:

• Check whether a condition is met;


and

• Depending on the result of the


conditional test, follow a certain
course of action or another.

You cannot create macros with loops. You can create macros that, by using
loops, repeat an action (or set of
actions) several times.

You cannot work with user interface You can work with certain user interface
tools and features (for example, tools and features (for example,
message boxes or input boxes). message boxes or input boxes) that are
not manually accessible.
Figure 59: VBA allows you to work with additional tools and features.

The Macro Recorder Tends to Generate Unnecessary Code


Recorded macros tend to be inefficient.

From a coding perspective, this is usually reflected in unnecessary code. This is


notoriously evident when you manipulate dialog boxes while recording a macro.

How important of an issue this is depends mostly on your objectives. If you just
want to record and use some basic recorded macros, this may not be a problem.

This is because, although inefficient, the Macro Recorder tends to generate


syntactically and structurally correct code. If you are satisfied with how a recorded
macro works, the issue of efficiency may not be a priority. In such cases, you may
not even be interested in how the macro's VBA code looks like.

However, as you become a more advanced macro and VBA user, these inefficiencies
in recorded VBA code may become apparent and create challenges.

The macro recorder tends to generate code that is:

• Long; and

63
Create Excel Macros Without Programming

• Verbose.

The resulting macros are usually more difficult to work with.

From a performance viewpoint, inefficient VBA code usually results in slower


macros.

The Macro Recorder and Dialog Boxes


Consider, for example, a macro that sets the page orientation to landscape. You
can record such a macro by following these steps:

#1. Click the Record Macro button.

#2. Excel displays the Record Macro dialog box.

#3. Enter a macro name (for example, "setLandscapeOrientation").

#4. Click the OK button.

Figure 60: Enter data in the Record Macro dialog box.

#5. Go to the Page Layout tab in the Ribbon and use the Orientation split button
to select Landscape.

Figure 61: Go to Page Layout → expand the Orientation drop-down → select Landscape.

#6. Click the Stop Recording button.

64
Create Excel Macros Without Programming

This is a very simple macro. However, the Macro Recorder generates the following
code:
Sub setLandscapeOrientation()
'
' setLandscapeOrientation Macro
'

'
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
Application.PrintCommunication = True
ActiveSheet.PageSetup.PrintArea = ""
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.7)
.RightMargin = Application.InchesToPoints(0.7)
.TopMargin = Application.InchesToPoints(0.75)
.BottomMargin = Application.InchesToPoints(0.75)
.HeaderMargin = Application.InchesToPoints(0.3)
.FooterMargin = Application.InchesToPoints(0.3)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 600
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperA4
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = 100
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.ScaleWithDocHeaderFooter = True
.AlignMarginsHeaderFooter = True

65
Create Excel Macros Without Programming

.EvenPage.LeftHeader.Text = ""
.EvenPage.CenterHeader.Text = ""
.EvenPage.RightHeader.Text = ""
.EvenPage.LeftFooter.Text = ""
.EvenPage.CenterFooter.Text = ""
.EvenPage.RightFooter.Text = ""
.FirstPage.LeftHeader.Text = ""
.FirstPage.CenterHeader.Text = ""
.FirstPage.RightHeader.Text = ""
.FirstPage.LeftFooter.Text = ""
.FirstPage.CenterFooter.Text = ""
.FirstPage.RightFooter.Text = ""
End With
Application.PrintCommunication = True
End Sub
A lot of this code is not necessary to set the page orientation to landscape.
Consider, for example, the following statements:

•.LeftHeader = "", .CenterHeader = "" and .RightHeader = "", which refer


to headers;

•.LeftFooter = "", .CenterFooter = "" and .RightFooter = "", which refer


to footers; and

•.LeftMargin = Application.InchesToPoints(0.7), .RightMargin =


Application.InchesToPoints(0.7), .TopMargin =
Application.InchesToPoints(0.75), .BottomMargin =
Application.InchesToPoints(0.75), .HeaderMargin =
Application.InchesToPoints(0.3) and .FooterMargin =
Application.InchesToPoints(0.3), which refer to margins.

You can, in fact, identify the line of code that sets the landscape page orientation:
.Orientation = xlLandscape.

The amount of code generated by the Macro Recorder may be puzzling. The Macro
Recorder does this because, when you work with a dialog box, the Macro Recorder
usually considers the complete status of the dialog box.

The macro example above is created by selecting the landscape page orientation
through the Ribbon.

This command, however, is part of the settings in the Page Setup dialog box. When
you set the page orientation to landscape, the Macro Recorder creates code that
regulates virtually every setting in this dialog box. Therefore, settings that do not

66
Create Excel Macros Without Programming

change (for example, headers, footers or margins) are also included in the recorded
VBA code.

Figure 62: When you work with the Page Setup dialog box, the Macro Recorder generates code that regulates all the
settings in the dialog box.

Other Common Instances of Inefficient Recorded Code


In other circumstances, the Macro Recorder creates code that is not long and
verbose but is still inefficient. In such situations, the Macro Recorder uses
constructs and structures that are not as efficient as the alternatives.

An example of this is copying and pasting. When you manually copy and paste, the
process requires two separate steps:

#1. You copy an item to the Clipboard.

#2. You paste the Clipboard contents to their end destination.

Recorded code reflects this two-step process.

However, VBA allows you to bypass the Clipboard. In other words, you can tell VBA
to copy an element directly to its destination. Therefore, you can obtain the same
result (copy an item to a destination) in a single step.

Despite Some Shortcomings, the Macro Recorder Continues


to be Useful
The limitations you learn about in this Chapter do not mean that you should avoid
the Macro Recorder. Being aware of these limitations, however, helps you
distinguish between:

• The situations where it is appropriate to use the Macro Recorder; and

• The situations where you probably should not fully rely on the Macro Recorder.

67
Create Excel Macros Without Programming

These limitations also underscore the importance of learning VBA.

In any case, you are likely to use the Macro Recorder when learning about, and
working with, macros and VBA. This is particularly the case if you are just beginning
your journey with VBA and have little or no programming experience.

As you become a more experienced VBA user, you may use the Macro Recorder
less frequently. In practice, you are likely to continue using the Macro Recorder
even as an advanced macro and VBA user.

Some of the Main Uses of the Macro Recorder


The following are three of the main uses of the Macro Recorder:

#1. To create basic macros.

#2. As a learning tool.

#3. To create basic macros that, later, become the basis of more powerful
macros.

The Macro Recorder as a Tool to Create Basic Macros


The most common use of the Macro Recorder is, probably, to create basic macros.

As you learned throughout this Book, the Macro Recorder allows you to create
simple but powerful macros that can:

• Automate certain tasks; and

• Make you significantly more efficient.

In practice, the Macro Recorder may meet most of your needs regarding simple
and basic macros. If a basic recorded macro helps you achieve your objectives, you
may not even be interested in how the macro's VBA code looks like.

The Macro Recorder as a Learning Tool


The Macro Recorder is one of the main tools you can use to learn VBA. In other
words, one of the best ways to learn about VBA constructs and syntax is to:

• Record macros; and

• Review the resulting code.

You can take this a step further and, for example:

68
Create Excel Macros Without Programming

• Modify the recorded VBA code to test and experiment; and

• Analyze the consequences of those different changes.

The Macro Recorder as a Tool to Create Advanced Macros


As you learned throughout this Chapter, the VBA code generated by the Macro
Recorder usually requires some level of edition. Nonetheless, recorded VBA code is
commonly used as the basis of more sophisticated and powerful macros.

The process for using the Macro Recorder as a tool to create an advanced macro
may look, roughly as follows:

#1. Begin the process by using the Macro Recorder.

#2. Work with, and amend, the VBA code generated by the Macro Recorder to:

• Make the macro work exactly as you want it to; and

• Handle the limitations of the Macro Recorder (several of which you learned
about in previous Sections).

You can, for example:

• Add additional instructions that the Macro Recorder cannot generate; or

• Delete unnecessary sections of VBA code.

Summary and Key Concepts


• Despite its usefulness and power, the Macro Recorder has some limitations.

• The Macro Recorder records most, but not all, of your actions.

• The Macro Recorder may fail to adequately capture the logic of certain features,
including:

• AutoSum;

• The Quick Analysis Tool; and

• The keyboard shortcut that takes you to the edge of the current data region.

• The Macro Recorder only records what you can do manually. VBA has several
tools and features you cannot access manually.

• The Macro Recorder tends to generate inefficient code. This may not always
be an issue. However, if you work with the VBA code generated by the Macro

69
Create Excel Macros Without Programming

Recorder, these inefficiencies tend to result in macros that are more difficult to
work with.

• When you work with a dialog box while recording a macro, the Macro Recorder
usually generates code that specifies virtually every setting in the dialog box.

• Despite its limitations, the Macro Recorder continues to be useful for both
beginner and advanced macro and VBA users. The following are three of its main
uses:

#1. To create basic macros.

#2. As a learning tool.

#3. To create basic macros that, later, become the basis of more complex
macros.

70
Create Excel Macros Without Programming

Chapter 7: Delete Recorded Macros


In this Chapter, you learn:

• Some of the main reasons for deleting macros.

• How to delete macros by working with the Macro dialog box.

Introduction
From time-to-time, you may have to delete macros from your workbooks. There
are several reasons why you may want to do this. The following are two common
reasons for deleting macros:

#1. You no longer use the macro.

#2. A recorded macro is not usable because it:

• Contains mistakes or errors; or

• Has been replaced by a newer version.

How to Delete Macros


You can (generally) delete macros in two main ways:

#1. From Excel, by working with the Macro dialog box. This is the process you
learn in this Chapter.

#2. From the Visual Basic Editor (VBE). You can learn the basics of working with
the VBE, including how to delete macros from the VBE, in Visual Basic Editor
(VBE) Basics.

Delete a Macro from the Excel Interface


Delete a macro (by working with the Macro dialog box) by following these steps:

#1. Go to the Macro dialog box by, for example, pressing the Alt+F8 keyboard
shortcut. This keyboard shortcut works for English (United States) language
settings. Depending on your language settings, you may have to use a different
keyboard shortcut to achieve the same result.

#2. Select the macro you want to delete.

#3. Click the Delete button.

71
Create Excel Macros Without Programming

Figure 63: To delete a macro with the Macro dialog box (steps #1 to #3): Go to the Macro dialog box → select the
macro → click Delete.

#4. Excel displays a dialog box asking you to confirm whether the macro should
be deleted.

#5. Click the Yes button to confirm the macro deletion.

Figure 64: To delete a macro with the Macro dialog box (steps #4 and #5): Click Yes to confirm the macro deletion.

After you complete this process, Excel deletes the macro.

Summary and Key Concepts


• There are several reasons why you may have to delete macros from your
workbooks.

• You can (generally) delete macros in two main ways:

#1. From Excel, by working with the Macro dialog box.

#2. From the Visual Basic Editor (VBE).

• Delete a macro (by working with the Macro dialog box) by following these
steps:

#1. Go to the Macro dialog box.

#2. Select the macro.

#3. Click the Delete button.

72
Create Excel Macros Without Programming

#4. When Excel displays a dialog box asking you to confirm whether the
macro should be deleted, click the Yes button.

73
Create Excel Macros Without Programming

Next Steps
If you have made it this far into the Book, I hope you have found it useful and
enjoyable.

This Book is accompanied by a website where you can download several files
containing the data and macros used in this Book's examples. The example files
inside the Book's website are organized according to the Chapters and Sections of
this Book. This makes it easy for you to find what you need as you read along.

Click here to:

• Visit the Book's website;

• Sign-up to the Power Spreadsheets Newsletter; and

• Get immediate access to the example files that accompany this Book.

After reading this Book, you probably know how to start creating basic but powerful
Excel macros without programming. This includes, among others:

• How to get Excel to display the Developer tab.

• What buttons (in the Developer tab) you use when creating macros without
programming.

• What the Macro Recorder is.

• How to use the Macro Recorder to create macros without programming.

• How to:

• Appropriately plan and prepare prior to recording a macro; and

• Determine the appropriate setup prior to recording a macro.

• The importance of appropriately using absolute or relative cell references when


recording a macro.

• How to:

• Determine whether to use absolute or relative cell references when


recording a macro; and

• Toggle between using absolute and relative cell references when recording
macros.

74
Create Excel Macros Without Programming

• The step-by-step process you use to create macros without programming.

• How to execute recorded macros with keyboard shortcuts.

• How to appropriately save a workbook that contains macros.

• The four main limitations of the Macro Recorder.

• Why, despite its limitations, the Macro Recorder is useful for both beginner and
advanced Excel macro and VBA users.

• Some of the main uses of the Macro Recorder.

• How to delete macros by working with the Macro dialog box.

Although this Book has ended, our conversation about Excel macros and VBA has
only begun. I am constantly creating training materials to help you become an
Excel Power User.

There are numerous other places we can continue our work together.

You can find my other Books about Excel and VBA (including the other Books in the
Excel Macros for Beginners Series) at Amazon.com.

You can join my Free Course for Excel macro beginners, Introduction to Excel
Macros, by subscribing to the Power Spreadsheets Newsletter.

I also provide a Premium Course for Excel users who have little or no
experience/knowledge about macros or VBA: Excel Macros for Beginners. If you
would like to be the first to know when the doors to Excel Macros for Beginners
open again, you can learn more about the Course and join the waitlist here.

PowerSpreadsheets.com contains Tutorials, links and other resources to help you


become an Excel Power User. You can visit the organized Archives here.

I Want to Hear from You!


I loved writing this Book and cannot thank you enough for your support. I
appreciate you for taking time out of your day to read this Book.

If you have a few seconds, I would love to read what you think about it. As the
reader of this Book, you are my most important reviewer and commentator. I value
your opinion and want to know:

• What I am doing right;

75
Create Excel Macros Without Programming

• What I could do better;

• What Excel topics you would like me to write about; and

• Any other wise advice you are willing to share with me.

If you enjoyed this Book, please give it an honest review on Amazon.com. This
helps others learn about Excel macros and VBA, allowing them to become more
efficient and powerful Excel users, positively impacting their own lives and the
broader world.

You can also leave a testimonial at PowerSpreadsheets.com.

I:

• Read all reviews and testimonials; and

• Take them into consideration when creating or updating books (such as this
one) or the rest of the training materials at Power Spreadsheets.

By leaving a review or testimonial, you help me serve you better.

Thanks again for your support. I wish you much success with Excel, macros and
VBA. Until next time,

76

You might also like