01 Introduction To Excel Macros & VBA - J.A. Gomez
01 Introduction To Excel Macros & VBA - J.A. Gomez
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.
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.
2
Introduction to Excel Macros & VBA
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.
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.
3
Introduction to Excel Macros & VBA
4
Introduction to Excel Macros & VBA
Table of Contents
Introduction ................................................................................................ 9
Macros ................................................................................................ 13
VBA .................................................................................................... 13
Introduction ........................................................................................... 18
Customize the User Interface and User Experience with Macros and VBA ..... 25
Control Other Applications From Excel with Macros and VBA ....................... 25
Additional Reasons to Use Macros and VBA to Automate Manual Tasks .......... 25
5
Introduction to Excel Macros & VBA
Introduction ........................................................................................... 29
Excel Updates and Your Work with Macros and VBA .................................. 34
Introduction ........................................................................................... 36
Several Tools and Features Help You Learn and Work with Macros and VBA . 40
Next Steps................................................................................................ 44
6
Introduction to Excel Macros & VBA
Table of Figures
Figure 1: The relationship between macros and VBA is similar to a chocolate cake
recipe. ..................................................................................................... 14
Figure 2: To hide a worksheet (steps #1 and #2): Right click → select Hide. .... 20
Figure 3: To unhide a worksheet (steps #1 and #2): Right click → select Unhide.
............................................................................................................... 20
Figure 4: To unhide a worksheet (steps #3 and #4): Select worksheet → click OK.
............................................................................................................... 21
Figure 7: The Ctrl+End keyboard shortcut takes you to the last cell in the used
range. ...................................................................................................... 23
Figure 8: The last cell in the used range may be different from the last cell with
data. ........................................................................................................ 23
Figure 9: The introduction of the Ribbon interface was one of the main changes in
Excel 2007................................................................................................ 31
Figure 10: Sparklines and Slicers were introduced in Excel 2010. .................... 31
Figure 11: The Quick Analysis Tool was introduced in Excel 2013. ................... 32
Figure 12: With the Single Document Interface, each Excel workbook has its own
window. ................................................................................................... 32
Figure 13: VBA support for Power Query (Get & Transform) was introduced in Excel
2016. ....................................................................................................... 32
Figure 14: Recent Excel versions have introduced several chart styles. ............ 34
Figure 16: The VLOOKUP function requires you to follow a specific set of
conventions and guidelines. ........................................................................ 39
Figure 17: The Macro Recorder (accessed through the Record Macro button) is one
of the tools and features you can use to start working with macros and VBA. .... 40
7
Introduction to Excel Macros & VBA
Figure 18: The VBE has several tools and features to help you work with VBA. .. 41
8
Introduction to Excel Macros & VBA
Introduction
About this Book
As implied by its title, the main purpose of this Book is to introduce you to the
wonderful world of Excel macros and VBA.
One of the main goals of this Book is to help you kickstart the process of learning
about Excel macros and VBA. By purchasing this Book, you have taken your first
step towards becoming a more powerful and efficient Excel user. As I explain in
Chapter 2, becoming a powerful and efficient Excel user has the potential to help
you in several ways.
The learning techniques and strategies I use in the following Chapters were first
applied to my own process of mastering macros and VBA.
• Spain;
• South Korea;
• Singapore;
• Germany; and
• Canada.
• Automation;
• Excel;
• Macros; and
• VBA.
9
Introduction to Excel Macros & VBA
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.
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.
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.
• In Chapter 1, you learn what macros, VBA and other related terms refer to.
• In Chapter 2, you learn how macros and VBA can help you become a more
efficient and powerful Excel user.
10
Introduction to Excel Macros & VBA
understand some potential pitfalls you may want to avoid while using macros
and VBA.
• In Chapter 4, you learn tips and strategies that help you approach learning
about macros and VBA. These tips and strategies should help you understand
how learning about macros and VBA can be easy and interesting.
More Information
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.
11
Introduction to Excel Macros & VBA
• How some of the terms related to macros and VBA are used in practice.
• What the following terms refer to and how they are different from "macro" and
"VBA":
• Program.
• Application.
• Procedure.
• Sub procedure.
• Routine.
• Sub routine.
A good understanding of the terms you use is essential to learning about, and
working with, macros and VBA. The first step to learning how to work with macros
and VBA is understanding what these and other important terms refer to.
Some of the other terms you learn about in this Chapter are commonly used to
refer to the same (or similar) constructs.
12
Introduction to Excel Macros & VBA
If you work with computers, you probably use computer programs all the time. For
purposes of this Book, a computer program is:
You can usually open or execute programs when you need to. The same occurs
with macros. In other words, after you create a macro, you can command Excel to
execute the macro as, and when, required.
Macros have several uses. However, most macro users like working with macros
because macros allow them to automate processes in Excel. You can, for example,
use these automation capabilities for several purposes, including:
You learn more about the most common uses of macros in Chapter 2.
VBA
In the previous Section, you learned that the commands that compose a computer
program are communicated in a programming language. In the case of Excel
macros, the programming language you work with is known as Visual Basic for
Applications or VBA.
In other words:
13
Introduction to Excel Macros & VBA
VBA is a programming language created by Microsoft. You can use this language
to work with the Microsoft Office Applications that support VBA. Excel is one of such
Applications that supports VBA.
VBA is built into the Applications that support it (such as Excel). In other words,
your version of Microsoft Excel includes VBA and you do not have to download or
purchase any additional software to work with macros and VBA.
At the same time, due to this relationship between Excel and VBA, you must have
Excel open to work with VBA.
• The step-by-step instructions you must follow to bake the cake are the
equivalent of a macro.
• The language in which those instructions are written (for example, English) is
the equivalent of VBA.
Figure 1: The relationship between macros and VBA is similar to a chocolate cake recipe.
In other words:
Strictly speaking, you do not always have to use VBA to create macros. In other
words, you can create (basic) macros without learning VBA. You learn about the
tool that helps you create macros without using VBA in Chapter 4.
14
Introduction to Excel Macros & VBA
The macros you create without using VBA can be helpful and powerful. In practice,
you usually need to learn, and work with, VBA to take full advantage of the power
of macros and VBA. This helps you understand, adjust and create the commands
that power the macro.
• Other times, a single term is used to refer to more than one element.
In my Books, Courses and other training materials, I try to use terms consistently.
For example, I generally use the terms "macro" and "VBA" as defined in previous
Sections. In my opinion, this terminology is the most consistent with that used by
Microsoft.
• The terms "macro" and "VBA" being used in a different way to that in which I
define them in this Book.
The following are some terms that are related to the topic of macros and VBA but
are more commonly used to refer to something else:
• Program.
• Application.
• Procedure.
• Sub procedure.
15
Introduction to Excel Macros & VBA
• Routine.
• Sub routine.
Program
In a previous Section, you learned that macros are small computer programs. The
key word in this definition is "small".
The scope of a regular computer program is usually much broader than that of a
macro. In other words, macros usually have a specific purpose and scope that is
much more limited than that of a full computer program.
Application
In the context of Excel macros and VBA, the term "Application" is commonly used
to refer to:
Excel files containing macros that are designed for other people to use.
Therefore, if you plan to become an Excel Developer, you are likely to spend a
substantial amount of time developing Applications.
These statements are the instructions you give to Excel using VBA.
When working with VBA, you can create the following two types of procedures:
16
Introduction to Excel Macros & VBA
• Return a value.
The terms "routine" and "Sub routine" are sometimes used to refer, respectively,
to:
• Procedures; and
• Sub procedures.
• Visual Basic for Applications (VBA) is the programming language you use to
create Excel macros.
• You can create two types of procedures when working with VBA:
• In practice, some of the terms you learned about in this Chapter may be used
slightly differently. This includes, for example, terms being used:
• Interchangeably; or
17
Introduction to Excel Macros & VBA
• How Excel has certain tools and features you cannot access manually. How
VBA helps you work with these tools and features.
• The difference between repetitive and recurring tasks. How macros and VBA
help you deal with both types of tasks.
• How macros and VBA allow you to customize Excel's user interface and
experience.
• Some of the main reasons for using macros and VBA to automate manual
tasks.
Introduction
In the previous Chapter, you learned that macros and VBA allow you to control
Excel.
This combination (Excel + Macros/VBA) can be very useful. Therefore, macros and
VBA have the potential to help you become a more powerful and efficient Excel
user.
Becoming a more powerful and efficient Excel user has the potential to help you in
several ways, including the following four:
#1. By helping you save time while carrying out certain activities in Excel, so
that you have more time for other activities you really care about.
#2. By obtaining skills that are in high demand in the job market, and whose
demand continues to grow at a fast pace.
18
Introduction to Excel Macros & VBA
#3. By enhancing your professional profile and helping you advance in your
career and obtain good-paying jobs.
At a basic level, macros and VBA allow you to do almost anything you can do
manually. In other words, Excel does not lose any of its functionality when you
work with macros and VBA.
On the other hand, you stand to gain a lot from working with macros and VBA.
Macros and VBA allow you to get Excel to do things that you either:
• Cannot do manually; or
The following are four of the main things Excel macros and VBA can do:
However, there are several actions you cannot (manually) carry out from Excel.
VBA allows you to access some features that would, otherwise, be inaccessible. The
following are three of these features:
19
Introduction to Excel Macros & VBA
Figure 2: To hide a worksheet (steps #1 and #2): Right click → select Hide.
Figure 3: To unhide a worksheet (steps #1 and #2): Right click → select Unhide.
#3. Excel displays the Unhide dialog box containing the hidden worksheets.
20
Introduction to Excel Macros & VBA
Figure 4: To unhide a worksheet (steps #3 and #4): Select worksheet → click OK.
Excel sheets have a third visibility status: Very hidden. In other words, a sheet can
be:
#1. Visible;
#2. Hidden; or
Knowing how to work with very hidden sheets may be helpful for several purposes.
For example:
• The ability to make sheets very hidden allows you to add an extra layer of
protection or coverage to sheets you want to hide; and
• The knowledge about very hidden sheets may give you an extra advantage
when working with workbooks (created by others) that have very hidden sheets.
• Click a button; or
21
Introduction to Excel Macros & VBA
#3. Return data with information about the action taken by the user.
You cannot manually create message nor input boxes from Excel. You must work
with VBA in both cases.
The used range of a worksheet is the range of cells that has been used. This
includes, for example:
Excel has a keyboard shortcut (Ctrl+End) that takes you to the last cell in the used
range. 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.
22
Introduction to Excel Macros & VBA
Figure 7: The Ctrl+End keyboard shortcut takes you to the last cell in the used range.
When using the Ctrl+End keyboard shortcut, you usually want to go to the last cell
with data in the worksheet. However, as I explain above, the used range also
contains cells that used to contain values (were used) even if they no longer contain
values (the value has been deleted). Because of this behavior, the last cell in a
worksheet's used range may not be the last cell with data in the worksheet.
Figure 8: The last cell in the used range may be different from the last cell with data.
You can reset the used range, and avoid problems like the one I describe above,
by working with VBA. You cannot manually reset the used range from Excel.
When you create an appropriate macro, Excel can carry out some repetitive or
recurring tasks automatically. The degree of automation you can achieve depends
on a variety of factors. These include, for example:
23
Introduction to Excel Macros & VBA
When the appropriate circumstances exist and your knowledge of macros and VBA
is enough, you can fully or partially automate tasks. In such cases, those tasks are
executed with very little or none input from you.
Recurring Tasks
• Time-consuming;
• Cumbersome; or
• Simply annoying.
Consider your own work responsibilities. You may have to prepare certain periodic
reports that require you to always repeat the same (or very similar) steps. These
frequently-repeated steps may include, among others:
• Organizing data;
If you do not want to carry out these tasks manually every single time, you can
use macros and VBA to automate a part of, or the whole, process.
Repetitive Tasks
Repetitive tasks are those where you must carry out the same action several times.
This may be, for example, a task you must repeat for several:
• Charts;
• Worksheets; or
• Workbooks.
Repetitive tasks are very common, among others, when you clean up data.
24
Introduction to Excel Macros & VBA
Customize the User Interface and User Experience with Macros and
VBA
In the previous Sections, you learned how macros and VBA allow you to:
• Automate tasks.
When you do either of these, you are (from a broader perspective) customizing the
user interface and improving the user experience. The ability of macros and VBA
to customize the user interface and experience includes, among others, the
following possibilities:
• Displaying:
• Message boxes;
• Input boxes; or
Although a relatively advanced topic, you can use VBA to communicate with, and
control, other Applications (for example, Word, PowerPoint, Outlook or Windows
itself). This allows you to both:
25
Introduction to Excel Macros & VBA
However, even in the case of tasks you can carry out manually, macros and VBA
may be useful. There are three main reasons for this:
#1. Speed;
#3. Reliability.
#2. By analyzing the impact macros and VBA have on the speed and
performance of your Excel files.
#1. Macros are significantly faster than you or me, and pretty much any other
person. Macros can usually complete tasks in a few seconds.
• Does not have a big influence on the file size or speed of an Excel workbook.
In other words, adding VBA code to your Excel workbooks is unlikely to have a
negative impact on their performance.
By appropriately using macros and VBA, you may experience efficiency gains that
are comparable to somebody helping you carry out certain tasks. The savings you
can experience by reducing the number of man-hours required to complete certain
tasks may be enough to justify the upfront time investment in both:
26
Introduction to Excel Macros & VBA
If your macros are appropriately built and coded, and contain no errors, they can
help you reduce the risk of errors and mistakes. This is because VBA always follows
the set of instructions you provide. It does this step-by-step and without any
deviation.
#2. Obtaining skills that are in high demand in the job market.
• Carrying out a task with macros and VBA (instead of manually) has the
following three main advantages:
#1. Speed:
#2. Macros are stored as text. Therefore, they (usually) do not have a big
influence on the file size or speed of a workbook.
27
Introduction to Excel Macros & VBA
#2. Cost: Considering the capabilities of macros and VBA, they are cheap.
#3. Precision: When appropriately used, macros and VBA help you reduce the
risk of errors and mistakes.
28
Introduction to Excel Macros & VBA
• That, despite their power and usefulness, macros and VBA may fail to produce
the results you expect.
• The two most common pitfalls to consider when working with macros and VBA.
• The main reason why macros may fail to work as you expect. How to reduce
the risks associated with your macros failing.
• Some of the main changes introduced in the most recent versions of Excel.
Introduction
In Chapter 2, you learned that macros and VBA are very useful and powerful.
However, macros and VBA are not perfect. There are a few potential pitfalls to
consider when working with them.
The following are two of the most common pitfalls to consider when working with
Excel macros and VBA:
However, from time-to-time, your instructions and the way VBA follows them
without deviation may result in unexpected errors or results.
29
Introduction to Excel Macros & VBA
This is a risk you can usually manage. Generally, the more you know about macros
and VBA, the lower the risk of your macros failing. In other words, you can learn
strategies and techniques that can help you manage and reduce the risk of losing
data or suffering other damages when a macro does not work as expected.
VBA seeks to reflect, and allow you to work with, the tools and features available
in Excel. Therefore, every time Microsoft releases a new Excel version, VBA must
reflect the changes.
Despite Microsoft's efforts, some Excel features work differently depending on the
Excel version you use. This creates problems both ways:
• Sometimes, macros fail on Excel versions that are older to that in which they
were created.
• Other times, macros fail on Excel versions that are newer to that in which they
were created.
Further compatibility issues exist between VBA for Windows and Mac.
There are several examples of features that Microsoft has introduced or updated in
the most recent versions of Excel.
Excel 2007
Excel 2007 modified some basic and essential characteristics of Excel. In other
words, if you are using Excel 2003 or earlier, Excel has changed significantly.
Even though the basic concepts and structure of VBA may not have changed much,
the changes introduced by Microsoft after Excel 2003 have an important influence
on the constructs you can use when working with macros and VBA.
The following are some of the main changes introduced with Excel 2007:
30
Introduction to Excel Macros & VBA
Figure 9: The introduction of the Ribbon interface was one of the main changes in Excel 2007.
If you are using Excel 2007 or newer, there are less changes. However, since
Microsoft is always adding new features and updating existing ones, you must still
consider some modifications.
Excel 2010
The following are two useful features introduced in Excel 2010:
#1. Sparklines, which are small charts you can insert in a single cell.
#2. Slicers (originally created for PivotTables), which make data filtering easier.
Slicers have, themselves, gone through further updates after their introduction. For
example, since Excel 2013, Slicers work with both:
• PivotTables; and
• Tables.
Excel 2013
The following are three useful features introduced in Excel 2013:
31
Introduction to Excel Macros & VBA
Figure 11: The Quick Analysis Tool was introduced in Excel 2013.
The Single Document Interface is the feature (out of the three I mention above)
that is likely to have a bigger influence on your macros and VBA code.
• Under the Multiple Document Interface that Excel used previously, all
workbooks were housed in a single Excel Application window.
• Under the Single Document Interface, each workbook is housed in its own
window. The Excel window of each workbook is separate from the Excel window
of any other workbook.
Figure 12: With the Single Document Interface, each Excel workbook has its own window.
The change from Multiple Document Interface to Single Document Interface has an
important influence on how you work with, and identify, workbooks and windows
when creating macros and working with VBA.
Excel 2016
One of the main changes introduced by Microsoft in Excel 2016 is support for Power
Query (Get & Transform). This allows you to automate the creation and
refreshment of Power Query queries.
Figure 13: VBA support for Power Query (Get & Transform) was introduced in Excel 2016.
32
Introduction to Excel Macros & VBA
#1. With each update, there are other less visible updates and modifications
that may influence the behavior of your macros and VBA code.
#2. There are some tools and features that are constantly updated. Charts and
PivotTables are two examples of such tools and features. Both have gone
through several updates over the years. For example:
• Excel 2013 introduced a new chart interface. These changes resulted in the
creation of a new VBA command to add charts. This is known as the
AddChart2 method.
• Treemap;
• Sunburst;
• Histogram;
• Pareto;
• Waterfall.
Later Excel versions continue to introduce new chart styles, such as:
• Funnel; and
• Map.
33
Introduction to Excel Macros & VBA
Figure 14: Recent Excel versions have introduced several chart styles.
#3. With the introduction of Office 365, the Office 365 version of Excel (Excel
365) is constantly updated.
Office 365 is a subscription service. Office 365 subscribers get regular updates.
In other words, users of Excel 365 are constantly getting new features.
The most recent stand-alone Excel version (at the time of writing) is Excel 2019.
Office 2019 (which includes Excel 2019) is sold as a one-time purchase (no
subscription). Users of Excel 2019 do not have access to the regular updates
that Office 365 subscribers receive.
• As more users subscribe to Office 365, the compatibility issues you learned
about in this Chapter are expected to become less of an issue.
• In the meanwhile, however, consider the fact that Microsoft will continue to
(regularly) introduce new features that influence what you can or cannot
manipulate with macros and VBA.
#2. If you plan to create macros to be used across different Excel versions.
From a broader perspective, however, the basic structure and logic of macros and
VBA remains the same across versions.
In other words, by understanding the basic principles behind macros and VBA, you
are likely to be in a better position to use these tools in different Excel versions.
34
Introduction to Excel Macros & VBA
Because of these reasons, my Books, Courses and several training materials focus
on these broadly applicable principles and frameworks, instead of specific tools and
features that vary from one Excel version to the next.
• Macros can fail to work as you expect. Generally, the more you know about
macros and VBA, the lower this risk.
• Macros may fail on Excel versions that are older or newer to that in which they
were created.
• Microsoft is constantly updating and modifying Excel. This may influence the
behavior of your macros and VBA code.
• Despite the changes in Excel, the basic structure and logic of macros and VBA
remains the same across versions. By understanding the basic principles behind
macros and VBA, you are likely to be in a good position to work with the different
versions of Excel.
35
Introduction to Excel Macros & VBA
• Why learning about macros and VBA can be easy and enjoyable.
• User-friendly; and
• Easy to understand.
• The main Excel and VBA tools and features that help you learn about, and work
with, macros and VBA.
Introduction
At first glance, it may sound that learning how to work with macros and VBA is
difficult. However, this does not have to be the case.
Learning about macros and VBA does not have to be difficult nor complicated. I am
convinced that, by adopting the appropriate strategies and mindset, you can make
the learning process easier and more enjoyable.
In my opinion, you should not let the word "programming" stress you. The following
are the four main reasons for this:
• Intuitive; and
• User-friendly.
#2. If you use Excel, you are (in a way) already programming.
36
Introduction to Excel Macros & VBA
#3. The association several people make between macros and programming is
not always correct. You can work with basic macros without programming.
#4. Excel and VBA have a set of tools and features that are designed to help
you work with, and learn about, macros and VBA.
Therefore, if you can use Excel, I am confident you can work with macros and VBA.
In fact, I believe you may find that working with macros and VBA is easier than
you expected.
VBA is User-Friendly
VBA is user-friendly. It allows anyone like you and me to code.
This user-friendliness is perhaps most evident when you consider the statement
structure and vocabulary used by VBA. The following two characteristics stand out:
#1. VBA:
#2. The statement structure and punctuation you use when creating a VBA
statement is relatively simple and straightforward.
• Borrows words from the English language (active, cell, columns, auto and fit);
and
In fact, you probably do not need to know VBA to get a rough idea of what this
statement does. You can, probably, infer that the constructs in this statement do
the following:
#2. Columns refers to a column (that where the active cell is located).
37
Introduction to Excel Macros & VBA
#3. AutoFit gives Excel the instruction to autofit a column (that where the
active cell is located) to its contents.
Therefore, by analyzing the entire statement, you can probably conclude that it
autofits the width of the column where the current active cell is located.
In other words, the point I am trying to make is not that you should always try to
guess what a VBA statement means. My purpose is to show you that the structure
and vocabulary of the VBA language is:
• User-friendly; and
• Easy to learn.
When you work with Excel, you are constantly communicating commands. This may
be, for example:
38
Introduction to Excel Macros & VBA
• A two-column table (cells A1 to B6) contains the names of five salespeople and
the number of units they sold.
• Cell E2 contains a worksheet formula that uses the VLOOKUP function. This
formula (=VLOOKUP(E1,A2:B6,2,FALSE)) does the following:
#1. Looks for the name displayed in cell E1 (for example, Suzanne Wilkins)
in the first column of the table.
#2. Returns the corresponding number of units sold from the second column
in the table.
Figure 16: The VLOOKUP function requires you to follow a specific set of conventions and guidelines.
In other words, just like VBA, worksheet formulas (as the example above) are a
set of conventions and guidelines you follow to communicate commands to Excel.
These conventions and guidelines differ from those of regular English.
For example, a person that is not familiar with Excel is unlikely to understand the
VLOOKUP formula in the example above.
You, as an Excel user, may have a good idea of this VLOOKUP formula's meaning.
Even if you are not familiar with the VLOOKUP worksheet function, you know there
are certain tools, features and information resources you can use to understand
the instructions given by the formula.
From this perspective, creating worksheet formulas is not that different from
programming in VBA.
39
Introduction to Excel Macros & VBA
Several Tools and Features Help You Learn and Work with Macros
and VBA
You can rely on a variety of tools and features during your journey towards
becoming a powerful macro and VBA user.
You do not have to be an advanced Excel user to start creating macros. Excel has
several tools that:
Perhaps the most important tool to start working with, and learning about, macros
and VBA is the Macro Recorder.
Figure 17: The Macro Recorder (accessed through the Record Macro button) is one of the tools and features you can
use to start working with macros and VBA.
For simple and basic macros, the Macro Recorder may be all you need. If a recorded
macro works well, you may not even be interested in the VBA code behind it.
Additionally, if the macro is not working properly or needs to be adjusted, you can
record it again.
The Macro Recorder, therefore, allows you to start creating macros without using
VBA. You can learn how to work with the Macro Recorder and start creating macros
without programming in Create Excel Macros Without Programming.
40
Introduction to Excel Macros & VBA
The next step, once you are comfortable working with the Macro Recorder, is to
work with VBA code. This is because, even though the Macro Recorder can be
extremely useful, you must usually work with VBA to fully unleash the power of
macros.
The programming environment you use to work with VBA is known as the Visual
Basic Editor or VBE. The VBE has several tools and features that help you work with
VBA code and create macros. For example, the VBE can:
Figure 18: The VBE has several tools and features to help you work with VBA.
You can learn the basics of working with the VBE in Visual Basic Editor (VBE) Basics.
During your learning process, the tools and features of Excel and the VBE can be
of substantial help.
Consider, for example, the Macro Recorder. Once you know how to use the Macro
Recorder and have a basic grasp of VBA, you can start:
41
Introduction to Excel Macros & VBA
The more you learn, the better you will become at automating tasks with macros
and VBA.
• See what happens and how the results change depending on the adjustments
you make.
• Try to understand the original code and why the changes you make lead to
different results.
• Use the macro examples and knowledge you gain every day to create macros
that help you in your day-to-day work.
In practice, you may find that (most of the time) you do not have to create very
complex macros. In some cases, such as when you are facing a repetitive task, a
one-time quick-and-dirty macro may be all you need.
While practicing, you may encounter errors. My last suggestion is to not let these
errors stop you. Making errors is part of any learning process.
• The following are four of the main reasons why learning about macros and VBA
can be easy:
#1. VBA has several characteristics that make it intuitive and user-friendly.
#2. If you use Excel, you are (in a way) already programming.
#3. You can work with basic macros without knowing about programming.
#4. Excel and VBA have a set of tools and features to help you work with,
and learn about, macros and VBA.
• VBA:
42
Introduction to Excel Macros & VBA
• Borrows a lot of words from the English language or uses common variations
of such words; and
• When working with regular Excel, you follow a set of conventions and
guidelines to communicate commands to Excel. VBA is just a different set of
conventions and guidelines you follow to communicate commands to Excel.
• The Macro Recorder allows you to create macros without programming and is,
perhaps, the most important tool to start working with, and learning about,
macros and VBA.
• The next step after becoming comfortable when working with the Macro
Recorder is to work with VBA code.
• The Visual Basic Editor (VBE) is the programming environment you use to work
with VBA. The VBE has several tools and features that help you work with VBA
code and create macros.
• Trying to understand the code examples and why the changes you make
lead to different results.
43
Introduction to Excel Macros & VBA
Next Steps
If you have made it this far into the Book, I hope you have found it useful and
enjoyable.
• What the following terms refer to and how they relate to, and differ from, each
other:
• Program.
• Macro.
• Application.
• Procedure.
• Sub procedure.
• Routine.
• Sub routine.
• What macros and VBA can do. Why macros and VBA are useful.
• Some of the main reasons for using macros and VBA to automate manual
tasks.
• The two most common pitfalls to consider when working with macros and VBA.
44
Introduction to Excel Macros & VBA
• The main reasons why the VBA language is user-friendly and easy to
understand.
• The main Excel and VBA tools and features that help you learn about, and work
with, macros and VBA.
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.
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:
• Any other wise advice you are willing to share with me.
45
Introduction to Excel Macros & VBA
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.
I:
• Take them into consideration when creating or updating books (such as this
one) or the rest of the training materials at Power Spreadsheets.
Thanks again for your support. I wish you much success with Excel, macros and
VBA. Until next time,
46