Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Learning Unit 8

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

S T U DY U N I T 8

Macros and relevant


25

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.

The learning outcomes of this study unit are as follows:

• Differentiate between the macro recorder versus manually generating VBA


code.
• Apply the macro recorder.
• Use relative references in macro recording.
The following icons are included in this study unit:

ICON DESCRIPTION

This icon indicates reading material you must study.

This icon indicates the documents used in the process.

This icon indicates that you need to self-reflect.

This icon indicates that you need to do some critical thinking.

This icon illustrates an activity you must complete.

This icon indicates that you need to click on the link to view a brief
video file.

This icon indicates that you need to refer to AIN1501.

8.2 The Macro Recorder vs Manually generating VBA code

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.

8.3 The Macro Recorder

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.

Computer activity 8.1:

You will need to perform the following task to assist you with basic practising of
recording a macro.

• You will firstly need to open an Excel workbook.


• Under the Developer tab, click on “Record Macro”.

Figure 8.1: Record Macro


As soon as you click on the “Record Macro” button, a new window will open where
you will be required to provide a name for your macro.

Figure 8:2: Record Macro pop-up window


Give an appropriate name to the macro. It should not contain any space characters
and special characters. You can give underscore (_) as the word separator, then
click on “OK” to start the recording.
You can assign a “Shortcut key”, but be sure that it is unique and does not conflict
with existing shortcut keys that can be found in Excel. E.g., Issuing a shortcut key of
Ctrl + c will not work, as this current shortcut actually means the following: Ctrl + c =
copy. In this example, the shortcut key conflicts with an existing shortcut key, and
will therefore not work when trying to assign a shortcut key for your macro.
You can determine where you would like to store the macro, either in the current
workbook, a new workbook or a personal macro workbook.
It would also be advisable to add a relevant and appropriate description about the
macro that you can easily refer to at a later stage; however, if you choose to leave
the description field blank, it will not prevent the macro from recording.
Figure 8:3: Providing a Macro Name and Description
Once you click “OK”, the macro is stored and from now onwards, the macro recorder
keeps recording all the activities you do in the Excel sheet until you click “ Stop
Recording” on the macro recorder.
• Firstly, select cell A1.

Figure 8:4: Selecting a cell

• Now type “Welcome to VBA” in A1 cell.

Figure 8:5: Entering a text in the selected cell


• Now stop the recording by clicking on “Stop Recording” under the developer
tab.

Figure 8:6: Stopping the recording


Excel will now stop recording the activities that we have done in Excel. Now let’s see
how Excel recorded the activities.

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.

Figure 8:7: Record Macro pop-up window


As soon as you click on “Visual Basic”, you will see the below window.
Figure 8:8: The VBE to review the code
• Double-click on “Modules”, which will open up another “ modules” item to click
on.

Figure 8:9: The VBE window


• Double-click on “Modules” as indicated in the image below.

Figure 8:10: Selecting the modules item


Now we will see the code on the right-hand side.

Figure 8:11: Viewing the code


Macro code started with the word SUB.

All the macros have two parts to it: one is Head, and another one is Tail. Every
macro has a name.

Figure 8:12: The beginning and end of the macro code


In between the head and tail of the macro, Excel recorded all the activities that we
have done.
Relating our action to the output of the macro code.
1. The first thing we did after we started recording the macro, is to have selected
cell A1; Excel recorded it as Range (“A1”).Select.

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.

Figure 8:13: Deleting the text in cell A1


• After deleting the word, once again, navigate to the VBE and click the “ Macros”
button.

Figure 8:14: Selecting the Macros option


The next window will now open, where you will be able to identify the macro that you
created. Any other macros that you’ve recorded and named will be included in this
list of macro names.
You will also be able to “Run” the macro, which should provide you with the same
result.

Figure 8:15: The selected Macros option


• There click on the “Run” button to once again enter the same text value to cell
A1.

Note: The shortcut key to run the code is F5.

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.

Click on the following link:


• Instructional Video Link

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.

Click on the following link:


• Macro_recorder_v2

Computer activity 8.3


Now that you’ve watched this short video, which refers to what we’ve covered
previously and also considering additional elements such as formatting, you are
ready to take on the next 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.

8.4 Using Relative References

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.

Computer activity 8.4

For this exercise, we will work on a new worksheet:


• You would need to activate the “Developer” tab on your ribbon and click the
“Use Relative References” option.

Figure 8.17: Activating the Relative References option


• Now click on the “Record Macro” as we are about to record the activity.
Remember to name the macro (without spaces in between the text) as we’ve
learnt previously.
• Thereafter you can click “OK”.

Figure 8.18: Record Macro, after activating the Relative References


• Now select cell B3 and insert the name “Duwayne” in it.
• In cell B4, insert the text “will always”.
• In cell B5, insert the text “Excel”.
• Thereafter, you can press stop.

This figure below is what you expect to see

Figure 8.19: Recorded macro using relative references


Let us now test the macro and the principle of relative references by doing the
following:
• Delete the text in the cell range B3:B5.
• Select the empty cell B3.
• Now execute the macro as you have done in computer activity 8.2, by referring
to Figure 8.14.
• The text that you created should now reappear in cells B3:B5.
• Now select cell D7.
• Execute the macro again as you’ve done previously.
• The same text should now appear in cells D7:D9 (see Figure 8.20a and b).
Figure 8.20a

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

Macros can be used for different purposes and can be used


in the absolute mode or relative mode, depending on what
you would like to achieve with the macro.

(a) What type of tasks would you use a macro for?


(b) Identify a scenario where you will use a macro
• in absolute mode
• in relative reference mode

Go to Discussion Forum 8.1 and discuss your findings with your fellow students.

Guidelines for participating in forums:


• Compile your post offline and keep a record of it.
• Use an academic writing style for referencing and citing the sources you used.
• Post your answer on the forum.
Reply to the contributions of at least two of 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.

You might also like