Learning Unit 8
Learning Unit 8
Learning Unit 8
references
8.1 Introduction
In study unit 7, we introduced you to the “Developer” tab in the Microsoft Excel ribbon,
where you will find the necessary tools that we will be working with. In this study unit,
we will be applying some of the tools that are in the “Developer” tab, especially relating
to recording macros and coding.
The purpose of this learning unit is to provide you with an understanding of how
macros work. You will also learn how to record a macro by writing text or even when
having to apply formatting to a given dataset. We will be using the macro recorder to
perform these tasks and will be able to view the system-generated code that is
programmed as a result of recording the macro.
We will also be using the macro recorder with relative references and distinguishing
between the absolute and relative references in the context of macros.
ICON DESCRIPTION
This icon indicates that you need to click on the link to view a brief
video file.
Microsoft Office tasks (including Excel) can be automated using the macro recorder
and/or writing VBA code manually. The macro recorder is a self-generating VBA code
tool. You will instruct the recorder to record a task you are performing and assign an
execution control (normally a shortcut key or button) to it. When you press the shortcut
key, the action will be re-performed. The main disadvantage of the macro recorder is
that it cannot follow rule-based decisions, including selections and loops. In contrast,
VBA code can be created manually in the VBE. You can also assign an execution
control to it.
We will cover the macro recorder in section 8.3, and generating VBA code manually
will then follow in the remaining sections.
The macro recorder is designed to assist you in doing repetitive tasks. Once you click
on the “Record Macro” option under the Developer tab, Excel will automatically start
recording the user’s actions in the program. Clicking the “Record Macro” again will
stop recording the macro, and save it for later use.
Consider the example of you performing a task whilst a video camera records what
you do. Once you have completed the task, you simply stop the video recording. Every
time you press “play”, you will be able to watch the same task being performed.
Similarly, this is how the macro recorder works.
Having used this example, you also need to understand that should you have made
any mistakes whilst performing the activity, the macro will record it. You must therefore
have a clear plan on what you want to achieve, before recording your process with a
macro, to avoid repeating the same error on other worksheets.
You will need to perform the following task to assist you with basic practising of
recording a macro.
Note: Earlier, we said to you that a macro is a self-generating VBA coding tool.
That means that as you are recording a particular activity, the macro is actually
generating/programming a VBA code in the background.
All the macros have two parts to it: one is Head, and another one is Tail. Every
macro has a name.
2. The second activity was when we entered the value “Welcome to VBA.” As
soon as we selected it, then it became an active cell, so Excel recorded the
activity as ActiveCell.FormulaR1C1 = “Welcome to VBA.”
Note: R1C1 is row 1, column 1.
3. A third activity is after typing the word “Welcome to VBA,” we hit enter, and
Excel selected the A2 cell.
Like this, Macro Recorder recorded all the activities we have done in the Excel
sheet.
In the next computer activity, we will test to see if our macro was recorded correctly,
as we expect to see the same result as when we initially created the macro.
Computer activity 8.2
• Navigate back to the Excel spreadsheet where you had initially typed the text
“Welcome to VBA” in cell A1.
• Now delete the word in cell A1.
So the macro code is executed, and we get the same value again. Like this, we can
automate our daily routine work to save a lot of time and get rid of everyday tasks
every day.
Figure 8:16: The selected Macros option
8.3.1 The Macro Recorder - continued
These aspects simply require practice in order to be comfortable with macros and
how we record and use them.
In the next exercise, we will be downloading an Excel file that we will need to work
from, but before we do this, please watch this brief instructional video on how to go
about doing this.
Now that you have watched the instructional video, we will be attempting another
activity, but this time, we will be using the file, which is applicable to the exercise we
will be looking at.
Before we begin with the required activity, please also take a look at the following
video, which refers to Macro_recorder_v2, where you can see the application of the
macro recorder in action. This video uses the same principles, but at a slightly more
detailed level, which will assist in solidifying your understanding of how to run a
macro. Thereafter, you may proceed with the activity.
• Please download the following Excel file by clicking here: File Download.
• The last tab is called Report. The other tabs, Jan, Feb, Mar, Apr, May, and Jun,
are system-generated downloads for actual, budgeted and variance between
the two for the Bread Division.
• Use the Macro recorder to generate this report automatically, which will be
applied for all the months.
In study unit 7, you were introduced to the concept of relative references when using
macros. You also learnt that macros are recorded in absolute references (mode) as
its default setting and that you would need to select the “use relative references”
application, when needed.
This begs the question, “When will I use relative references when recording a
macro?”. Well, we’ve seen that macros are used to perform repetitive tasks on a
number of worksheets; however, the recorded macro was recorded in absolute mode
and you would have noticed that the macro will always perform the task at the same
place all the time. This means that I cannot repeat the same task in different places
within the same worksheet, because macros default to the same position all the time.
If I wanted to repeat a task in different places within the same worksheet, then I would
“use relative references” to achieve this purpose.
Let us look at an example of how this works.
Figure 8.20b
If you’ve achieved this outcome, then your macro worked
Now try a couple of different scenarios for yourself and then attempt activity 8.5.
Activity 8.5
Go to Discussion Forum 8.1 and discuss your findings with your fellow students.
8.5 Summary
In this study unit, we looked at macros from the context of a self-generating program
and how to use the macro recorder. We provided a name and description as well as
obtained an understanding of the purpose of running a macro within our environment
as students. We also looked at the difference between the absolute mode versus the
relative reference mode when recording a macro and when we would use which mode.
We ran through a couple of examples and even tried some of our own examples.
In the next study unit, we will look at VBA by learning how to write some code.