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

Module 5

This document provides a student guide on using simple macros in Microsoft Excel. It contains 5 lessons that teach how to record, run, copy, edit, and understand relative macros. The lessons include overviews, activities, and try-its to practice the macro skills being taught.

Uploaded by

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

Module 5

This document provides a student guide on using simple macros in Microsoft Excel. It contains 5 lessons that teach how to record, run, copy, edit, and understand relative macros. The lessons include overviews, activities, and try-its to practice the macro skills being taught.

Uploaded by

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

Student Guide

40571A
Microsoft Excel expert 2019
Module 5: Using simple macros
Using simple macros

Contents
Contents ................................................................ 2 Warm-up ....................................................... 17
Module overview .............................................. 4 Topic 1: Record simple macros ........... 18
Description ...................................................... 4 Activity: Student-and-teacher
Scenario ............................................................ 5 collaboration........................................... 20

Cornerstone .................................................... 5 Try-it: Record simple macros .......... 20

Lesson 1: Understanding macros .............. 6 Try-it 1 ....................................................... 21

Overview .......................................................... 6 Try-it 2 ....................................................... 21

Warm-up .......................................................... 6 Topic 2: Run simple macros ................. 22

Topic 1: Display the Developer tab ..... 7 Add a macro to the Quick Access
Toolbar ...................................................... 23
Activity: Setting the scene ................... 9
Activity: Guess and learn ................... 26
Try-it: Display the Developer tab ..... 9
Try-it: Run simple macros ................. 26
Try-it 1 .......................................................... 9
Try-it 1 ....................................................... 26
Try-it 2 ....................................................... 10
Try-it 2 ....................................................... 27
Topic 2: Get an introduction to macros
........................................................................... 11 Wrap-up ........................................................ 28

Activity: Discuss and learn................ 12 Lesson 3: Using the Visual Basic Editor 29

Try-it: Get an introduction to Overview........................................................ 29


macros ....................................................... 12 Warm-up ....................................................... 29
Try-it 1 ....................................................... 13 Topic 1: Access the Visual Basic Editor
Try-it 2 ....................................................... 13 ........................................................................... 29

Topic 3: Enable macros in a workbook Activity: Show and tell ........................ 31


........................................................................... 14 Try-it: Access the Visual Basic Editor
Activity: Pose a question ................... 15 ...................................................................... 32

Try-it: Enable macros in a workbook Try-it 1 ....................................................... 32


...................................................................... 16 Try-it 2 ....................................................... 32
Wrap up ......................................................... 16 Topic 2: Copy macros between
Lesson 2: Recording and running macros workbooks .................................................... 33
................................................................................ 17 Copy and paste the VBA code for a
Overview ....................................................... 17 macro ......................................................... 34

2
Using simple macros

Copy a macro module ....................... 37 Lesson 5: Understanding relative macros


Activity: Show and learn .................... 38 ................................................................................ 51

Try-it: Copy macros between Overview........................................................ 51


workbooks ............................................... 39 Warm-up ....................................................... 51
Try-it 1 ....................................................... 39 Topic 1: Understand relative vs.
Try-it 2 ....................................................... 39 absolute macros ........................................ 52

Wrap-up ........................................................ 40 Activity: Show and tell ........................ 53

Lesson 4: Editing macros ............................ 41 Try-it: Understand relative vs.


absolute macros .................................... 53
Overview ....................................................... 41
Topic 2: Record a relative macro........ 54
Warm-up ....................................................... 41
Activity: Discuss and learn ................ 55
Topic 1: Name simple macros ............. 42
Try-it: Record a relative macro ....... 55
Activity: Show and tell ........................ 43
Wrap-up ........................................................ 56
Try-it: Name simple macros ............ 43
Glossary .............................................................. 57
Try-it 1 ....................................................... 43
Cornerstone ...................................................... 58
Try-it 2 ....................................................... 44
Overview........................................................ 58
Topic 2: Edit simple macros ................. 45
Objectives ..................................................... 58
Activity: Student-and-teacher
collaboration .......................................... 48 Duration ......................................................... 58

Try-it: Edit simple macros ................. 48 Instructions ................................................... 58

Try-it 1 ....................................................... 48 Tasks ................................................................ 59

Try-it 2 ....................................................... 49 File 1:


Cornerstone_macros_starter.xlsm . 59
Wrap-up ........................................................ 50
File 2:
Cornerstone_report_starter.xlsm ... 59

3
Using simple macros

Module overview
Description
As you use Microsoft Excel 2019, you’re likely to find that you do certain tasks over and
over again. Perhaps your boss asks you to create a report on a regular basis, and you
always set up the same row and column headings. Maybe your company wants you to
include the same information at the beginning of every spreadsheet, such as the logo
and contact information. Macros help automate these repetitive tasks so that you can
create and manage your workbooks more efficiently.
In this module, you’ll learn how macros work and how you can record your own macros.
You’ll run, copy, and edit macros so that you can use them wherever and whenever you
need them.
The following table outlines the lessons in this module and their corresponding learning
and exam objectives.

Lesson Learning objective Exam objective(s)

Understanding Display the Developer tab in the ribbon and • 1.1.3


macros enable macros in a workbook.

Recording and Record and run a simple macro. • 3.6.1


running
macros

Using the View Visual Basic for Applications (VBA) • 1.1.1


Visual Basic macros and reuse existing macros by
Editor copying VBA code from one workbook to
another.

Editing macros Name and edit an existing macro. • 3.6.2


• 3.6.3

Understanding Create and run a relative macro. None


relative
macros

4
Using simple macros

Lesson Learning objective Exam objective(s)

Cornerstone: Automate report formatting by copying, • 1.1.1


Using macros editing, and recording macros. • 1.1.3
• 3.6.1
• 3.6.2
• 3.6.3

Table 1: Objectives by lesson

Scenario
Based on your experience at Munson's Pickles and Preserves Farm, you think that
creating some simple macros will help team members with less confidence in Excel
easily carry out repetitive tasks—like applying Munson’s branding to cells and setting
workbooks for printing. For example, one repetitive task at the farm is importing reports
to Excel and setting up those reports for printing. You’ll use your knowledge of macros
to help with this task.

Cornerstone
This module concludes with a Cornerstone in which you’ll use macros to automate some
repetitive reporting tasks at Munson's. In the Cornerstone, you'll:
• Enable and run a macro.
• Copy and edit a macro with Visual Basic Editor.
• Record a macro.

5
Using simple macros

Lesson 1: Understanding macros


Overview
In this lesson, you’ll learn how to add the Developer tab to the ribbon and use it to
access tools for creating, editing, and running macros. You'll learn the role that macros
play in Microsoft Excel, and how you can enable them when you open a workbook. The
activities, discussions, and demonstrations in this lesson will enhance your learning and
give you the opportunity to practice and articulate what you learn about using macros.

Warm-up
Use these questions to find out what you already know about this lesson’s topics:
1. Which tab includes commands related to macros?
Select the correct option.
a. Home
b. Insert
c. Formulas
d. Data
e. Developer
2. Which of the following do you use to automate repetitive tasks?
Select the correct option.
a. Filters
b. Formulas
c. Macros
d. PivotTables
3. To save a workbook with macros, you must select Select here to enter text. as the
file type.
Fill in the blank space.

6
Using simple macros

Topic 1: Display the Developer tab


As you explore macros in this module, you’ll need the commands on the Developer tab.
The Developer tab includes features that are often unnecessary for day-to-day Excel
use, so it’s hidden by default. You must enable it if you want to create, edit, and run
macros.
Select the File tab, and then, in the Backstage view, select Options to open Excel
Options. In Excel Options, select Customize Ribbon. The following screenshot depicts
Excel Options with Customize Ribbon highlighted:

Figure 1: Excel Options with Customize Ribbon highlighted

7
Using simple macros

In the Customize the Ribbon pane, in the Main Tabs section, select the Developer
check box, and then select OK to close Excel Options. The following screenshot depicts
the Customize the Ribbon pane in Excel Options with the Developer check box
highlighted.

Figure 2: Excel Options with Customize Ribbon selected and the Developer check box
highlighted

8
Using simple macros

The Developer tab will remain available until you clear the Developer check box in
Excel Options to disable it. Note that you might have to enable it again if you open
Excel on a different device. The following screenshot depicts the Developer tab.

Figure 3: The Developer tab

Additional information
For more information on the Developer tab, go to: Show the
Developer tab

Activity: Setting the scene


In this activity, your teacher will lead a discussion about macros. Then, the teacher will
demonstrate how to add the Developer tab to the ribbon.

Resources required
You’ll need the following resources for this activity:
• None

Activity instructions
Participate in the class discussion and then follow along as the teacher demonstrates
adding the Developer tab to the ribbon. Ask any clarifying questions that you might
have.

Try-it: Display the Developer tab


In this leveled try-it activity, you’ll practice customizing the ribbon to display the
Developer tab.

Try-it 1
In this try-it activity, you’ll enable the Developer tab on the ribbon.

9
Using simple macros

Resources
You’ll need the following resources for this try-it:
• None

Instructions
The following are the general tasks that you need to perform during this try-it:
1. Open Excel with a blank workbook.
2. Select the File tab to open the Backstage view and access Excel Options.
3. Select Customize Ribbon, select the check box for the Developer tab, and then
close Excel Options.
4. Select the Developer tab to confirm that it’s enabled.
5. If time allows, practice repeating the steps to remove the Developer tab and then
add it again.

Try-it 2
In this try-it activity, you’ll enable the Developer tab on the ribbon and learn about
some of the commands that it includes.

Resources
You’ll need the following resources for this try-it:
• None

Instructions
The following are the general tasks that you need to perform during this try-it:
1. Open a blank workbook in Excel.
2. Select the File tab to open the Backstage view and access Excel Options.
3. Select Customize Ribbon, select the check box for the Developer tab, and then
close Excel Options.
4. Select the Developer tab to confirm that it’s enabled.
5. Explore the Developer tab, hovering over different commands to reveal the
ScreenTip. Focus on commands in the Code group.
6. As time allows, visit Office Help and Training and research commands that you'd like
to know more about.

10
Using simple macros

Topic 2: Get an introduction to macros


When you use Excel regularly, you might find that you do some tasks . For example, for
school assignments, you might have to enter your name, the teacher's name, and the
date at the beginning of every new worksheet. Or your boss at work might want you to
add the same header and footer on each worksheet, so that they’re easy to identify
when you print them.
You can use macros to automate these kinds of repetitive tasks by grouping a series of
actions together into a single command. You can create a macro with all the necessary
steps to complete the repetitive task, and then replay the macro any time that you need
it.
A macro consists of Visual Basic for Applications (VBA) code, which is a programming
language available in most Microsoft Office applications. The code conducts the steps
that you wish to repeat, and you can review or modify it by using the Visual Basic Editor.

Did you know?


You don't need to know VBA to create macros, thanks to the macro
recorder. This tool records the steps that you take to perform an
action and creates the VBA code to repeat your steps.

The following is an example of VBA code for a macro that puts basic information at the
beginning of a worksheet:
Range("A1").Select
ActiveCell.FormulaR1C1 = "Munson's Pickles and Preserves Farm"
Range("A2").Select
ActiveCell.FormulaR1C1 =
"http://www.munsonspicklesandpreservesfarm.com"
Range("A3").Select
ActiveCell.FormulaR1C1 = "=TODAY()"

11
Using simple macros

In this example, the user selected A1 and entered the text Munson's Pickles and
Preserves Farm, and then selected A2 and entered
http://www.munsonspicklesandpreservesfarm.com. Finally, they selected A3 and
entered a formula to display the current date. The macro recorder created the VBA code
based on those actions.

Did you know?


When it's recording, the macro recorder captures almost everything
that you do. It even captures your mistakes! If you add a misspelled
word to a cell and then delete it and add it back correctly, those
incorrect steps and corrections become part of the macro. If you want
to remove them, you'll have to record the sequence again or change
the VBA code.

A macro is part of the workbook, so Excel uses a different file type to save workbooks
that have macros. An Excel Macro-Enabled Workbook has the .xlsm file extension. If
you attempt to save a workbook containing macros as an Excel Workbook with the .xlsx
file extension, you’ll receive a warning that the macro will be lost.

Additional information
For more information on macros, go to: Automate tasks with the
Macro Recorder

Activity: Discuss and learn


In this activity, the teacher will lead a class discussion about basic macro concepts.

Resources required
You’ll need the following resources for this activity:
• None

Activity instructions
Participate in the group discussion and ask clarifying questions about any topics that
you don’t fully understand.

Try-it: Get an introduction to macros


In this leveled try-it, you’ll summarize what you know about macros.

12
Using simple macros

Try-it 1
In this try-it activity, you’ll summarize basic macro concepts.

Resources
You’ll need the following resources for this try-it:
• None

Instructions
The following are the general tasks that you need to perform during this try-it:
1. Create a paragraph responding to the following prompts:
o What is the purpose of a macro?
o What are some tasks that using macros can help you with?
o What programming language and tools do you use to create and edit macros?
2. If time allows, do some research by going to Office Help and Training to review
concepts that you aren't sure about. Add what you learn to your response.

Try-it 2
In this try-it activity, you’ll summarize basic macro concepts, including key terminology.

Resources
You’ll need the following resources for this try-it:
• None

Instructions
The following are the general tasks that you need to perform during this try-it:
1. Create a paragraph responding to the following prompts:
o What is the purpose of a macro?
o What are some tasks that using macros can help you with?
o Explain what macro recorder does.
o Explain the role of the Visual Basic Editor in creating and editing macros.
2. If time allows, do some research by going to Office Help and Training to review
concepts you aren't sure about. Add what you learn to your response.

13
Using simple macros

Topic 3: Enable macros in a workbook


Some macros can pose significant security risks, such as making your computer or
network vulnerable to a virus or other malicious attacks. Therefore, Excel takes
precautions when you open a file that includes a macro. By default, macros will be
disabled when you open a macro-enabled workbook. To run a macro, you'll need to
enable it.
Always think carefully before enabling macros in a file from a source you don’t know or
can't verify—malicious hackers can distribute workbooks with macros in hope that
someone will enable their commands. If you don't enable macros, it’s safe to open a
workbook and examine—or even edit—its contents.
If you trust the source of the macro-enabled workbook, or if you created the workbook,
you can enable macros and take advantage of the automation that they provide.
When you open a workbook with one or more macros, the SECURITY WARNING
message bar displays with the Enable Content option. Select Enable Content to enable
macros. The following screenshot depicts the SECURITY WARNING message bar with
Enable Content highlighted.

Figure 4: The SECURITY WARNING message bar with Enable Content highlighted

14
Using simple macros

You can also enable macros from the Backstage view. Select the File tab, and then, in
the Backstage view, select Info. Select the Enable Content drop-down box, and then
select Enable All Content. The following screenshot depicts the Backstage view with
the Enable Content drop-down box open and with Enable All Content highlighted.

Figure 5: The Backstage view with the Enable Content drop-down box open and Enable
All Content highlighted

Additional information
For more information on enabling macros, go to: Edit a macro

Activity: Pose a question


In this activity, the teacher will ask questions about macro security to engage the class in
a discussion. Then the teacher will demonstrate how to enable macros when opening a
macro-enabled workbook.

Resources required
You’ll need the following resources for this activity:
• None

15
Using simple macros

Activity instructions
Participate in the group discussion and ask clarifying questions about any topics that
you don’t fully understand.

Try-it: Enable macros in a workbook


In this standalone try-it activity, you’ll open a workbook and enable macros.

Resources
You’ll need the following resources for this try-it:
• Open L1_T3_try_munsons_starter.xlsm in this lesson’s Learning Activity Resources.

Instructions
The following are the general tasks that you need to perform during this try-it:
• Open L1_T3_try_munsons_starter.xlsm and enable macros.

Wrap up
Use these questions to check what you learned in this lesson:
1. Which of the following converts actions that you perform into VBA code?
Select the correct option.
a. Developer tab
b. Macro recorder
c. Visual Basic Editor
d. Visual Basic for Applications
2. Where can you enable macros when you open a macro-enabled workbook?
Select all that apply.
a. A command in the Backstage view
b. On the Home tab
c. On the message bar
d. On the Review tab
3. To work with macros, you should first enable the Select here to enter text. tab.
Fill in the blank space.

16
Using simple macros

Lesson 2: Recording and


running macros
Overview
In this lesson, you’ll learn how to record and run a simple macro. The activities,
discussions, and demonstrations in this lesson will give you the opportunity to practice
creating and running simple macros.

Warm-up
Use these questions to find out what you already know about this lesson’s topics:
1. Where can you find the Record Macro command?
Select the correct option.
a. On the Data tab
b. On the Developer tab
c. On the Formulas tab
d. On the Home tab
2. By default, where are macros saved?
Select the correct option.
a. In the current workbook
b. In a Personal Macro Workbook
c. In a separate text file
3. On the Developer tab, select Select here to enter text. to begin recording a macro.
Fill in the blank space.

17
Using simple macros

Topic 1: Record simple macros


You can create macros by writing VBA code, but the macro recorder makes it
quicker and easier to create a simple macro, even if you don’t know VBA.
Before recording a macro, you should carefully plan each step that you’ll take to
perform the tasks that you wish to automate. The macro recorder will capture almost
everything you do, including mistakes, and create corresponding VBA code. It’s a good
idea to practice the steps prior to recording the macro.
To record a macro, select the Developer tab in the ribbon, and then, in the Code group,
select Record Macro. The following screenshot depicts the Code group with Record
Macro highlighted:

Figure 6: The Code group with Record Macro highlighted

This activates the Record Macro dialog box. In the Macro name box, enter a name for
your macro. You might have more than one macro in a workbook; so, make sure that
your macro names are descriptive. Macro names are often verbs that describe what the
macro does, such as SetupPage or FormatReport. The first character of the name must
be a letter, but subsequent characters can be numbers or underscore characters.
If you want to assign a keyboard shortcut to your new macro, select the Shortcut key
box and enter any letter. It's a good idea to use Ctrl+Shift key combinations so that you
don't overwrite any default Excel keyboard shortcuts. For example, if you use Ctrl+Z you
won't be able to use that combination to Undo in Excel while that workbook is active,
but Ctrl+Shift+Z does not overwrite Undo. Therefore, it's usually a good idea to
include Shift in your shortcut combination to avoid conflicts.
In the Store macro in drop-down box, you can select a location for the macro that
you’re recording. Usually, you'll select This Workbook and keep the macro in the
workbook that you're currently editing. However, you can also choose to store it in your
Personal Macro Workbook, which will be created if it doesn’t already exist. Finally, you
can select New Workbook to create a new blank workbook for the new macro.
In the Description box, you can describe the macro’s function, which is especially
helpful in a workbook with more than one macro. It's a good idea to write a brief
sentence explaining what the macro does.

18
Using simple macros

The following screenshot depicts the Record Macro dialog box.

Figure 7: The Record Macro dialog box

Did you know?


The keyboard shortcut to access the Record Macro dialog box is
Alt+T+M+R.

After you've entered the information that you want in the Record Macro dialog box,
select OK to begin recording. Perform the actions that you want to record. When you’re
ready to stop, select the Developer tab, and then, in the Code group, select Stop
Recording. The following screenshot depicts the Code group with Stop recording
highlighted.

Figure 8: The Code group with Stop Recording highlighted

19
Using simple macros

Remember that each action that you perform before selecting Stop Recording will be
included in the macro, so it’s a good idea to plan your steps carefully and rehearse them
prior to recording.

Did you know?


If you’re planning to record a long process as a macro, consider
breaking it up and recording smaller macros. Then, record one macro
that runs each of the other macros in the correct sequence.

Additional information
For more information on recording a macro, go to: Quick start: Create
a macro

Video
To review the video on recording a macro, go to: Work with macros

Activity: Student-and-teacher collaboration


In this activity, the class will collaborate with the teacher to create a simple macro that
formats a cell.

Resources required
You’ll need the following resources for this activity:
• None

Activity instructions
Participate in the activity by collaborating with the teacher and other students to create
a macro.

Try-it: Record simple macros


In this leveled try-it activity, you’ll record a macro to apply formatting to a cell.

20
Using simple macros

Try-it 1
In this try-it activity, you’ll record a simple macro to format numbers.

Resources
You’ll need the following resources for this try-it:
• None

Instructions
The following are the general tasks that you need to perform during this try-it:
1. Create and save a new macro-enabled workbook named
L2_T1_try1_number_style.xlsm.
2. Add random numbers with at least three decimal digits to cells A1:A3.
3. Select one of the numbers that you entered and record a macro named
ApplyFancyFormat. In the macro, apply the following formatting to the cell:
o Number displayed to one decimal place
o Italics
o Font color Blue
4. Stop recording and save your workbook.

Try-it 2
In this try-it activity, you’ll record a simple macro to format dates, including a keyboard
shortcut and description.

Resources
You’ll need the following resources for this try-it:
• None

Instructions
The following are the general tasks that you need to perform during this try-it:
1. Create and save a new macro-enabled workbook named
L2_T1_try2_date_style.xlsm.
2. Add random date to cells A1:A3. You might wish to use your birthday or other
significant dates.

21
Using simple macros

3. Select one of the dates that you entered and record a macro named
ApplyFancyFormat by using the following steps:
a. Configure the macro to use the keyboard shortcut combination Ctrl+Shift+D.
b. Enter the following text for the description: Applies a fancy date format to the
active cell.
c. In the macro, apply the following formatting to the cell:
Date is displayed in the format 2012-03-14.
Italics
Font color is Blue
4. Stop recording and save your workbook.

Topic 2: Run simple macros


After you’ve recorded a macro, you can run it any time that you want to repeat the
recorded steps. It's like recording a song and playing it whenever you want.
If you set up a combination shortcut key for the macro, simply select that keyboard
combination and the macro will run. Regardless of whether the macro has a
combination keyboard shortcut, you can run it from the ribbon. Select the Developer
tab, and then, in the Code group, select Macros to open the Macro dialog box. The
following screenshot depicts the Code group with Macros highlighted.

Figure 9: The Code group with Macros highlighted

22
Using simple macros

In the Macro dialog box, select the macro that you want to run in Macro name, and
then select Run. The following screenshot depicts the Macro dialog box with the
AddReportHeading macro selected and Run highlighted.

Figure 10: The Macro dialog box with the AddReportHeading macro selected and Run
highlighted

Add a macro to the Quick Access Toolbar


To make it even easier to run a macro, you can add it to the Quick Access Toolbar.
Select File, and then, in the Backstage view, select Options to open Excel Options.
Select Quick Access Toolbar, and then use the Choose commands from drop-down
list to select Macros. The following screenshot depicts Excel Options with Quick Access
Toolbar and the Macros option highlighted:

23
Using simple macros

Figure 11: Excel Options with Quick Access Toolbar and the Macros option highlighted

Then, select the macro you want to add to the Quick Access Toolbar and select Add.
Select OK to close Excel Options. The following screenshot depicts Excel Options with
the AddWorksheetInfo macro added to the Quick Access Toolbar and the Add
command highlighted.

24
Using simple macros

Figure 12: Excel Options with the AddWorksheetInfo macro added to the Quick Access
Toolbar and the Add command highlighted

To run the macro, select the macro in the Quick Access Toolbar. The following
screenshot depicts the Quick Access Toolbar with the macro icon highlighted:

Figure 13: The Quick Access Toolbar with the macro icon highlighted

Additional information
For more information on running macros, go to: Run a macro

25
Using simple macros

Additional information
For more information on adding macro to the Quick Access Toolbar,
go to: Assign a button to a macro

Video
To review the video on adding a macro to the Quick Access Toolbar,
go to: Assign a button to a macro

Activity: Guess and learn


In this activity, the teacher will lead a group discussion about different ways to run
macros. Then, the teacher will demonstrate several different options for running a
macro.

Resources required
You’ll need the following resources for this activity:
• None

Activity instructions
Participate in the activity by following these instructions:
1. Participate in the teacher-led group discussion.
2. Follow along as the teacher demonstrates how to run macros.
3. Ask any clarifying questions that you might have.

Try-it: Run simple macros


In this leveled try-it, you’ll run macros by using the ribbon or a keyboard shortcut.

Try-it 1
In this try-it activity, you’ll use the ribbon to run the macro that you created in the
previous try-it.

26
Using simple macros

Resources
You’ll need the following resources for this try-it:
• Open L2_T1_try1_number_style.xlsm from the previous activity and save it as
L2_T2_try1_number_style_starter.xlsm. Alternatively, open
L2_T2_try1_number_style_starter.xlsm in this lesson’s Learning Activity Resources.

Instructions
The following are the general tasks that you need to perform during this try-it:
1. Select one of the cells with a random number.
2. Use the ribbon to run the ApplyFancyFormat macro.
3. As time allows, repeat the process on other cells with random numbers.

Try-it 2
In this try-it activity, you’ll use a keyboard shortcut to run the macro that you created in
the previous try-it.

Resources
You’ll need the following resources for this try-it:
• Open L2_T1_try2_date_style.xlsm from the previous activity and save it as
L2_T2_try2_date_style_starter.xlsm. Alternatively, open
L2_T2_try2_date_style_starter.xlsm in this lesson’s Learning Activity Resources.

Instructions
The following are the general tasks that you need to perform during this try-it:
1. Select one of the cells with a date.
2. Run the ApplyFancyFormat macro by selecting Ctrl+Shift+D.
3. As time allows, repeat the process on other cells with dates.

27
Using simple macros

Wrap-up
Use these questions to check what you learned in this lesson:
1. Which of the following best describes what the macro recorder captures?
Select the correct option.
a. Almost all the actions that you perform
b. Text input and cell formatting
c. Actions you perform by using the ribbon
2. Which of the following can you configure from the Record Macro dialog box?
Select all that apply.
a. Description
b. Macro name
c. Security settings
d. Shortcut key
3. Add a frequently used macro to the Select here to enter text. to provide a
convenient way to run it.
Fill in the blank space.
4. To make a macro available to any worksheet that you open from the same computer,
save it to a Select here to enter text..
Fill in the blank space.

28
Using simple macros

Lesson 3: Using the Visual Basic


Editor
Overview
In this lesson, you’ll learn to review VBA code and reuse existing macros by copying
macros from one workbook to another. The discussions, activities, and demonstrations
will reinforce your learning and provide the opportunity to practice these skills.

Warm-up
Use these questions to find out what you already know about this lesson’s topics:
1. Which command opens the code for a macro?
Select the correct option.
a. Macros
b. Record Macro
c. Visual Basic
2. Macros consist of code in which programming language?
Select the correct option.
a. Basic
b. C#
c. Machine language
d. Visual Basic for Applications

Topic 1: Access the Visual Basic Editor


Macros are saved as VBA code. This code is just text, and Office includes the Visual
Basic Editor for viewing and editing VBA code.
To open macros in the Visual Basic Editor, select the Developer tab on the ribbon.
Then, in the Code group, select Visual Basic. The following screenshot depicts the Code
group with Visual Basic highlighted.

29
Using simple macros

Figure 14: The Code group with Visual Basic highlighted

Did you know?


The keyboard shortcut to open the Visual Basic Editor is Alt+F11.

The Visual Basic Editor will open as a separate window displaying a text editor with the
VBA code for all the macros in your workbook. The following screenshot depicts the
Visual Basic Editor with the VBA code for a macro highlighted.

Figure 15: The Visual Basic Editor with the VBA code for a macro highlighted

30
Using simple macros

The line Sub AddReportHeading() indicates the start of the AddReportHeading


macro, which is called a subroutine in coding. End Sub indicates the end of the macro.
These two lines establish the borders for all the information about the macro. Additional
macros in the workbook would be listed following End Sub.
The first set of lines all start with an apostrophe and the editor shades them green. This
indicates that those lines are informational and aren’t part of the steps that the macro
will take. In fact, Excel just skips those lines when running the macro. You can think of
them as a heading that displays the name of the macro and the text that you entered in
the Description box before you started recording. You can add a similar note, called a
comment by programmers, by entering an apostrophe and entering any text that you
want. Comments are often useful in providing additional information to people who
read the code later.
The code itself displays in black text. Some of the lines might be easy to figure out, but
others are not as obvious. For example, Range("A1").Select is the code that the macro
recorder generated when the user selected cell A1. The line ActiveCell.FormulaR1C1 =
"Munson's Pickles and Preserves Farm" is what the macro recorder generated when
the user entered the name of the farm. Note that the creator then selected A2 and
entered the farm's web address, and then selected A3 and entered a formula to display
the current date.
If you make any changes to VBA code in the Visual Basic Editor, you'll need to use
Save to save the changes to the macro-enabled workbook. You can close the editor at
any time and reopen it from Excel.

Activity: Show and tell


In this activity, the teacher will demonstrate how to open and use the Visual Basic
Editor. You’ll examine the code for simple macros created with the macro recorder.

Resources required
You’ll need the following resources for this activity:
• Open L3_T1_act_days_starter.xlsm in this lesson’s Learning Activity Resources.

Activity instructions
Participate in the activity by following along with the teacher’s demonstration. During
the demonstration, ask questions about topics or tasks that are unclear to you.

31
Using simple macros

Try-it: Access the Visual Basic Editor


In this leveled try-it, you’ll open the Visual Basic Editor and examine the VBA code
for two macros.

Try-it 1
In this try-it, you’ll open the Visual Basic Editor from the ribbon.

Resources
You’ll need the following resources for this try-it:
• Open L3_T1_try1_months_starter.xlsm in this lesson’s Learning Activity Resources.

Instructions
The following are the general tasks that you need to perform during this try-it:
1. Use the Visual Basic command on the ribbon to open the Visual Basic Editor.
2. If necessary, use the View menu to access Project Explorer.
3. If necessary, expand Modules and double-click Module1, or select it, and then
select Enter.
4. After the line that reads ' MonthsV Macro, add the following comment: ' Lists the
months vertically starting in the active cell.
5. Close the Visual Basic Editor.

Try-it 2
In this try-it, you’ll open the Visual Basic Editor with the keyboard shortcut and elements
of the Project Explorer and the VBA code for two macros.

Resources
You’ll need the following resources for this try-it:
• Open L3_T1_try2_months_starter.xlsm in this lesson’s Learning Activity Resources.

Instructions
The following are the general tasks that you must perform during this try-it:
1. Use the Visual Basic command on the ribbon to open the Visual Basic Editor.
2. If necessary, use the View menu to access Project Explorer.

32
Using simple macros

3. If necessary, expand Modules and double-click Module1, or select it, and then
select Enter.
4. After the line that reads ' MonthsV Macro, add the following comment: ' Lists the
months vertically starting in the active cell.
5. Close the Visual Basic Editor.

Topic 2: Copy macros between


workbooks
Unless you save a macro to a Personal Macro Workbook, it will only be attached to
the workbook you were using when you recorded it. If you want the same automation in
a different workbook, you can copy the macro, instead of recording it again.
When macros are attached to a workbook, they are organized into modules. By default,
Excel creates a module named Module1 when you record a macro, and each
subsequent macro that you record is added to Module1.
To find Module1 for your file, you'll need to open the Project Explorer in Visual Basic
Editor. To do so:
1. Select the Developer tab, and then, in the Code group, select Visual Basic.
2. If the Project Explorer does not display, select the View tab, and then select Project
Explorer.

Did you know?


The Project Explorer displays a variety of VBA elements for each
workbook that you have open at the time, not just the workbook that
you’re using.

33
Using simple macros

The following screenshot depicts the Visual Basic Editor with Project Explorer
highlighted.

Figure 16: The Visual Basic Editor with Project Explorer highlighted

The screenshot depicts Project Explorer with two workbooks open: Drone_data.xlsm
and Report.xlsm. Drone_data.xlsm doesn’t have any modules because it doesn’t
contain any macros yet.

Copy and paste the VBA code for a macro


To copy an individual macro from one workbook to another, you can Copy and Paste
the VBA code in the Visual Basic Editor. Begin by opening the workbook that contains
the macro module that you want to copy and the workbook that you want to copy it to.

34
Using simple macros

Select the Developer tab, and then, in the Code group, select Visual Basic to open the
Visual Basic Editor. If Project Explorer is not already available, select the View tab, and
then select Project Explorer.
In Project Explorer, select the module that you wish to copy, which is often Module1.
This displays the VBA code for all macros in that module. Both the source and
destination workbooks might have modules named Module1; so, you must make sure
that you select the module within the source workbook. The following screenshot
depicts the Visual Basic Editor with Module1 in Project Explorer highlighted:

Figure 17: The Visual Basic Editor with Module1 in Project Explorer highlighted

35
Using simple macros

Next, select the text for the entire macro that you wish to copy from the line that begins
Sub to the line End Sub. Select the Edit menu, and then select Copy. The following
screenshot depicts the Visual Basic Editor with a single macro selected in preparation for
being copied.

Figure 18: The Visual Basic Editor with a single macro selected in preparation for being
copied

36
Using simple macros

If the destination workbook doesn’t have a module, or if you wish to copy the macro to
a new module, right-click or activate the context menu for the destination workbook.
Select Insert, and then select Module. A new module will be added to the workbook.
The following screenshot depicts the context menu for a workbook in Project Explorer,
with Insert selected and Module highlighted.

Figure 19: The context menu for a workbook in Project Explorer, with Insert selected and
Module highlighted

To open the code, double-click the module name or select the module name, and then
select Enter. Position the cursor after the last line of code. If there are no existing
macros, position the cursor on the first line. Select the Edit menu, and then select Paste.

Copy a macro module


You can copy a macro module, including all the macros in that module, to another
workbook. To do so, begin by opening the workbook that contains the macro module
and the workbook that you want to copy it to.
Select the Developer tab, and then, in the Code group, select Visual Basic to open the
Visual Basic Editor. If Project Explorer isn’t already available, select the View menu
and then select Project Explorer.

37
Using simple macros

In Project Explorer, select the module that you wish to copy, which is often Module1.
Drag the module to the destination workbook. The following screenshot depicts Project
Explorer with the module to copy highlighted and the destination workbook
highlighted.

Figure 20: Project Explorer with the module to copy highlighted and the destination
workbook highlighted

The macro module will be added to the destination workbook, and any macros that it
contains will be usable in the destination workbook.

Additional information
For more information on copying macros, go to: Copy a macro
module to another workbook

Activity: Show and learn


In this activity, the teacher will demonstrate how copy a macro module to a different
workbook and how to copy a single macro by copying the VBA code.

Resources required
You’ll need the following resources for this activity:
• Open L3_T2_act_module_starter.xlsm in this lesson’s Learning Activity Resources.

38
Using simple macros

Activity instructions
Participate in the activity following along with the teacher’s demonstration. During the
demonstration, ask questions about topics or tasks that you’re unclear about.

Try-it: Copy macros between workbooks


In this leveled try-it activity, you’ll copy one or more macros to a new workbook.

Try-it 1
In this try-it, you’ll copy an entire macro module to a new workbook.

Resources
You’ll need the following resources for this try-it:
• Open L3_T2_try1_module_starter.xlsm in this lesson’s Learning Activity Resources.

Instructions
The following are the general tasks that you need to perform during this try-it:
1. Ensure that the starter file is open.
2. Create a new macro-enabled workbook named L3_T2_try1_module.xlsm.
3. Copy Module1 from L3_T2_try1_module_starter.xlsm to L3_T2_try1_module.xlsm.
4. Close L3_T2_try1_module_starter.xlsm.
5. Run the two macros in L3_T2_try1_module.xlsm.

Try-it 2
In this try-it, you’ll copy the VBA code for one macro to a new workbook.

Resources
You’ll need the following resources for this try-it:
• Open L3_T2_try2_copy_starter.xlsm in this lesson’s Learning Activity Resources.

Instructions
The following are the general tasks that you need to perform during this try-it:
1. Ensure that the starter file is open.
2. Create a new macro-enabled workbook named L3_T2_try2_copy.xlsm.

39
Using simple macros

3. Copy the VBA code for the MonthsH macro from L3_T2_try1_copy_starter.xlsm to
L3_T2_try2_copy.xlsm.
4. Close L3_T2_try2_copy_starter.xlsm.
5. Run the MonthsH macro in L3_T2_try2_copy.xlsm.

Wrap-up
Use these questions to check what you learned in this lesson:
1. Which of the following is a container that organizes macros in a workbook?
Select the correct option.
a. Blocks
b. Modules
c. Subs
d. Workbooks
2. Which character indicates a comment in a macro?
Select the correct option.
a. Ampersand
b. Apostrophe
c. Comma
d. Slash
3. How can you copy a macro from one workbook to another?
Select all that apply.
a. Open the Macro dialog box.
b. Copy and paste the VBA code.
c. Drag and drop in the Visual Basic Editor.
d. Use a command on the Developer tab.
4. The last line of VBA code for a macro is Select here to enter text..
Fill in the blank space.

40
Using simple macros

Lesson 4: Editing macros


Overview
In this lesson, you’ll learn to change the name of a macro and edit macro code. The
activities, discussions, and demonstrations will give you the opportunity to apply and
practice the concepts that you learn.

Warm-up
Use these questions to find out what you already know about this lesson’s topics:
1. Where can you change the name of a macro?
Select the correct option.
a. From the context menu
b. From the Macro dialog box
c. In the Visual Studio Editor
d. In the Backstage view
2. How can you change or correct a macro after you’ve recorded it?
Select all that apply.
a. By recording it again
b. By editing the VBA code for the macro
c. By adding it to the Quick Access Toolbar
d. You cannot change a macro after it's been recorded.
3. A macro name must start with a Select here to enter text..
Fill in the blank space.

41
Using simple macros

Topic 1: Name simple macros


After you’ve recorded a macro, you cannot change its name in Excel. However, you
can change a macro name by editing the macro in the Visual Basic Editor.
Select the Developer tab on the ribbon, and then, in the Code group, select Visual
Basic to open the Visual Basic Editor. To open the VBA code in the editor, double-click
the module containing the macro, which is usually Module1, or select it, and then select
Enter.
The name of the macro is part of the first line of that macro's VBA code, which begins
with Sub and ends with parentheses. The following screenshot depicts the VBA code
for a macro, with the name highlighted:

Figure 21: The VBA code for a macro, with the name highlighted

To change the name of the macro, select the name in the editor, being careful not to
select the space before the name or the parentheses following the name. Then, enter
the new name for the macro. The first character of the name must be a letter, but
subsequent characters can be letters, numbers, or underscore characters. You should
give the macro a name that describes the task or tasks that it will record. Avoid using
the same name as another macro in the workbook.

42
Using simple macros

After entering the new name, select Save. The following screenshot depicts the Visual
Basic Editor, with Save highlighted.

Figure 22: The Visual Basic Editor, with Save highlighted

Activity: Show and tell


In this activity, your teacher will discuss the importance of macro names and will
demonstrate how to change the name of an existing macro.

Resources required
You’ll need the following resources for this activity:
• Open L4_T1_act_days_starter.xlsm in this lesson’s Learning Activity Resources.

Activity instructions
Participate in the activity by following along with the teacher’s demonstration. During
the demonstration, ask questions about topics or tasks that you’re not clear about.

Try-it: Name simple macros


In this leveled try-it, you’ll access the Visual Basic Editor, which you’ll then use to
change the names of macros.

Try-it 1
In this try-it, you’ll open the Visual Basic Editor from the ribbon or the Macro dialog
box, and then you’ll change the name of a macro.

43
Using simple macros

Resources
You’ll need the following resources for this try-it:
• Open L4_T1_try1_months_starter.xlsm in this lesson’s Learning Activity Resources.

Instructions
The following are the general tasks that you need to perform during this try-it:
1. Use the ribbon to open the Visual Basic Editor. You can use the Visual Basic
command or select Macros and, in the Macro dialog box, select Edit.
2. Rename the MonthsH macro to FillMonthsHortizontal.
3. Return to Excel and open the Macro dialog box to confirm your changes.

Try-it 2
In this try-it, you’ll open the Visual Basic Editor with the keyboard shortcut, and then
you’ll change the name of a macro.

Resources
You’ll need the following resources for this try-it:
• Open L4_T1_try2_months_starter.xlsm in this lesson’s Learning Activity Resources.

Instructions
The following are the general tasks that you need to perform during this try-it:
1. Select Alt+F11 to open the Visual Basic Editor.
2. Rename the MonthsH macro to FillMonthsHortizontal.
3. Return to Excel and open the Macro dialog box to confirm your changes.

44
Using simple macros

Topic 2: Edit simple macros


You can use the Visual Basic Editor to change any part of a macro's VBA code.
Although making extensive changes or additions requires knowledge of the VBA
programming language, you can add comments and make simple substitutions to the
parts of the code that are easy to understand.
Consider the following VBA code:
Range("A1").Select
ActiveCell.FomulaR1C1 = "The Farm"
Range("A2").Select
ActiveCell.FormulaR1C1 = "Weekly Report"
Range("A2").Select
With Selection.Font
.Name = "Calibri"
.Size = 14
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
This macro selects A1 and enters the text The Farm, and then selects A2 and enters
Weekly Report. After that, it selects A2 and sets the font to Calibri with the font size
14.

45
Using simple macros

However, the user made a couple of mistakes. Instead of The Farm, they intended to
input Munson's Pickles and Preserves Farm. To fix this mistake, select The Farm in the
second line code, being careful not to select the quotation marks themselves, and then
enter the correct text.
The user also intended to select A1 instead of A2 before setting the font properties. You
can fix this by finding the second instance of the line Range("A2").Select, which is on
the fifth line of code. Select A2, and then enter A1.
The VBA code for the macro is now as follows:
Range("A1").Select
ActiveCell.FomulaR1C1 = "Munson's Pickles and Preserves Farm"
Range("A2").Select
ActiveCell.FormulaR1C1 = "Weekly Report"
Range("A1").Select
With Selection.Font
.Name = "Calibri"
.Size = 14
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
You can also use an apostrophe to add a comment to the code explaining the most
complicated part of the macro. To do so, position the cursor at the start of the line and
select Enter to insert a new line. Position the cursor on that new line and enter the
following text:
' Change the font to Calibri size 14.

46
Using simple macros

The final version of this macro is as follows:


Range("A1").Select
ActiveCell.FomulaR1C1 = "Munson's Pickles and Preserves Farm"
Range("A2").Select
ActiveCell.FormulaR1C1 = "Weekly Report"
Range("A1").Select
' Change the font to Calibri size 14.
With Selection.Font
.Name = "Calibri"
.Size = 14
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
You can also change colors by editing the macro. The following code sets the color of
the text in A1 to red:
Range("A1").Font.Color = -16776961
The macro recorder uses an unusual value to represent red: -16776961. However, VBA
has a system for using name colors. For example, vbRed represents red and vbYellow
represents yellow. To set the color to green, use the following line of VBA code:
Range("A1").Font.Color = vbGreen

47
Using simple macros

Video
To review the video on editing a macro, go to: Edit a macro

Activity: Student-and-teacher collaboration


In this activity, the class will collaborate with the teacher to edit a macro by using the
Visual Basic Editor.

Resources required
You’ll need the following resources for this activity:
• None

Activity instructions
Participate in the activity by collaborating with the teacher and other students to create
a macro.

Try-it: Edit simple macros


In this leveled try-it, you’ll use the Visual Basic Editor to make simple changes to an
existing macro.

Try-it 1
In this try-it, you’ll change two formatting options in an existing macro.

Resources
You’ll need the following resources for this try-it:
• Open L4_T2_try1_months_starter.xlsm in this lesson’s Learning Activity Resources.

Instructions
The following are the general tasks that you need to perform during this try-it:
1. Open the Visual Basic Editor to examine the macro in the starter file.
2. Find the line of VBA code that sets the font size to 16. Change the font size to 14.
3. Save and close the editor, and then run the macro to verify that the changed code
works as expected.

48
Using simple macros

4. Find the line of VBA code that sets the font style to Bold Italic. Change the font style
to Italic.
5. Save and close the editor, and then run the macro to verify that the changed code
works as expected.

Try-it 2
In this try-it, you’ll change three formatting options in an existing macro.

Resources
You’ll need the following resources for this try-it:
• Open L4_T2_try2_months_starter.xlsm in this lesson’s Learning Activity Resources.

Instructions
The following are the general tasks that you need to perform during this try-it:
1. Open the Visual Basic Editor to examine the macro in the starter file.
2. Find the line of VBA code that sets the font size to 16. Change the font size to 14.
3. Save and close the editor, and then run the macro to verify that the changed code
works as expected.
4. Find the line of VBA code that sets the font style to Bold Italic. Change the font style
to Italic.
5. Save and close the editor, and then run the macro to verify that the changed code
works as expected.
6. Find the line of VBA code that sets the color to the numeric value 192. Change the
color to blue.
7. Save and close the editor, and then run the macro to verify that the changed code
works as expected.

49
Using simple macros

Wrap-up
Use these questions to check what you learned in this lesson:
1. Which of the following is the correct name for the color blue in VBA?
Select the correct option.
a. blue
b. vbBlue
c. vbaBlue
d. rgbBlue
2. Which of the following are valid macro names?
Select all that apply.
a. ApplySettings
b. Apply_Settings
c. Apply-Settings
d. 2Settings
e. Apply2Settings
3. You can find the macro name on a line that starts with Select here to enter text..
Fill in the blank space.

50
Using simple macros

Lesson 5: Understanding
relative macros
Overview
In this lesson, you’ll learn the difference between absolute and relative references in
macros. You’ll also record a relative macro. The activities, discussions, and
demonstrations will give you the opportunity to practice and apply what you learn.

Warm-up
Use these questions to find out what you already know about this lesson’s topics:
1. Which of the following options refers to macro references that change depending on
the active cell when you run a macro?
Select the correct option.
a. Absolute cell references
b. Macro cell references
c. Relative cell references
d. VBA cell references
2. Which type of cell references should you use if you always want a macro to run on
the same exact cells?
Select the correct option.
a. Absolute cell references
b. Macro cell references
c. Relative cell references
d. VBA cell references
3. To record a relative macro, select the Select here to enter text. command.
Fill in the blank space.

51
Using simple macros

Topic 1: Understand relative vs.


absolute macros
The macro recorder has two different ways to manage the cells that you select while
recording actions. By default, the VBA code for the macro will record the exact cells that
you select during the recording. If you start the recording in C4, select A1 and enter
Munson's, and then move to A2 and enter Weekly Report, the resulting VBA code
would be as follows:
Range("A1").Select
ActiveCell.FormulaR1C1 = "Munson's"
Range("A2").Select
ActiveCell.FormulaR1C1 = "Weekly Report"
The macro will always put the text in cells A1 and A2, no matter which cell you’ve
selected when you start the macro. This is called absolute cell references.
If you record the same macro with relative cell references, the location of the text will be
relative to the cell that you have selected when you begin recording. The same steps,
recorded by using relative cell references beginning from C4, generate the following
VBA code:
ActiveCell.Offset(-3, -2).Range("A1").Select
ActiveCell.FormulaR1C1 = "Munson's"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "Weekly Report"
When you select cell A1 by using relative references, the macro recorder doesn't create
code to select A1. Instead, it creates code to select a cell based on an offset: it moves to
a cell that is three rows and two columns before the starting point and enters
Munson's. It then moves to the next row from there and adds Weekly Report.
If you select cell F13 and run the previous macro with absolute cell references,
Munson's is added to A1. However, if you run the code with relative references from
that location, the text will be added to D10, because that cell is three rows and two
columns before F13.
Use absolute cell references when you want the steps performed in the exact same cells
on the worksheet every time, regardless of which cell is active when the macro starts.
For example, a macro to place heading information at the beginning of a worksheet
would use absolute cell references.

52
Using simple macros

Use relative cell references when you want the macro to perform steps on any cell,
based on the selected location when it begins. An example of this would be a macro
that adds the days of the week beginning in the selected cell.

Did you know?


Relative cell references can cause problems in a macro. For example,
if the macro includes instructions to move a few columns or rows
before a cell, and you run it from cell A1, it will stop responding,
because A1 is the first cell in the worksheet.

Activity: Show and tell


In this activity, your teacher will discuss and demonstrate the difference between
absolute cell references and relative cell references when recording a macro.

Resources required
You’ll need the following resources for this activity:
• None

Activity instructions
Participate in the activity by following along with the teacher’s demonstration. During
the demonstration, ask questions about topics or tasks that you’re unclear about.

Try-it: Understand relative vs. absolute macros


In this standalone try-it activity, you’ll create a macro by using absolute cell
references.

Resources
You’ll need the following resources for this try-it:
• None

53
Using simple macros

Instructions
The following are the general tasks that you need to perform during this try-it:
1. Create a new macro-enabled workbook named L5_T1_try_my_info.xlsm.
2. Select any cell other than A1 before you begin recording.
3. Use the macro recorder to begin recording a macro, and:
a. Name the macro InsertMyInfo.
b. Assign it the shortcut keyboard combination Ctrl+Shift+M.
c. For the Description, enter the text Inserts a few lines of information about
me, starting in cell A1.
4. After you begin recording, enter the following information:
a. Enter your name in cell A1.
b. Enter your location (city, state, province, or similar) in cell A2.
c. Enter your date of birth in cell A3.
5. Apply the formatting of your choice to the information that you’ve entered.
6. Stop recording. Clear the contents of cells A1:A3 and run your macro to verify that it
works as expected.

Topic 2: Record a relative macro


The macros that you've recorded to this point have used absolute cell references. The
Developer tab on the ribbon includes a command to switch to relative references and
back to absolute references. To record steps by using relative references, select the
Developer tab, and then, in the Code group, select Use Relative References. To switch
back to absolute references, select Use Relative References again. The following
screenshot depicts the Code group with Use Relative References enabled and
highlighted.

Figure 23: The Code group with Use Relative References enabled and highlighted.

Generally, you'll turn Use Relative References on or off before you select Record
Macro, but you can use the command while you’re recording.

54
Using simple macros

Activity: Discuss and learn


In this activity, the teacher will lead a class discussion about the Use Relative
References feature and will then demonstrate recording a macro with relative
references.

Resources required
You’ll need the following resources for this activity:
• None

Activity instructions
Participate in the group discussion and ask clarifying questions about any topics that
you don’t fully understand.

Try-it: Record a relative macro


In this standalone try-it activity, you’ll record a macro with relative references.

Resources
You’ll need the following resources for this try-it:
• Open your workbook from the previous try-it activity. Alternatively, open
L5_T2_try_my_info_starter.xlsm in this lesson’s Learning Activity Resources.

Instructions
The following are the general tasks that you need to perform during this try-it:
1. Turn on the Use Relative References feature and record a macro named
InsertMyInfoRelative. Assign the keyboard shortcut Ctrl+Shift+R and add an
appropriate description.
2. After you begin recording, enter a few lines of information, as you did in the
previous try-it:
a. Enter your name in the currently selected cell.
b. Enter your location (city, state, province, or similar) on the next row, in the same
column.
c. Enter your date of birth on the next row, in the same column.
3. Apply the formatting of your choice to the information that you’ve entered.

55
Using simple macros

4. Stop recording. Select a cell in an unused area of the worksheet and run your macro
to verify that it works as expected. It should add your name to the current cell and
then add the additional information on the next rows.

Wrap-up
Use these questions to check what you learned in this lesson:
1. On which tab can you enable relative references?
Select the correct option.
a. Home
b. Insert
c. Formulas
d. Data
e. Review
f. Developer
2. When can you enable or disable relative references?
Select the correct option.
a. Only before you record
b. Only after you record
c. Only while you record
d. At any time
3. Use the Select here to enter text. command to disable relative references.
Fill in the blank space.

56
Using simple macros

Glossary
Absolute cell Macro locations that do not change regardless of the active cell
references when you start the macro.

Developer tab A tab on the ribbon with commands for working with macros, which
is hidden by default.

Macro A series of actions grouped together into a single command.

Macro-enabled A workbook type that can save macros for use when you open the
Workbook workbook in the future.

Macro recorder A tool that creates a macro by capturing actions and recording them
in VBA code.

Module A container for one or more macros.

Personal Macro A special workbook that stores macros so they are available any time
Workbook you use Excel on the same computer.

Relative cell Macro locations that are based on the active cell when the macro is
references started.

VBA Visual Basic for Applications, a programming language available in


most Microsoft Office apps.

Visual Basic Editor An environment for creating and editing VBA code and procedures.

Table 2: Glossary terms and definitions

57
Using simple macros

Cornerstone
Overview
In this Cornerstone, you’ll help automate a report for Munson’s Pickles and Preserves
Farm by managing, editing, and creating macros to format imported data. You’ll copy
macros between workbooks, edit macros in Visual Basic Editor, and record a macro to
add information to the beginning of a workbook.

Objectives
The following table outlines the Cornerstone objectives and their corresponding
Microsoft Office Specialist (MOS) exam objectives.

Enable and run a • 1.1.3: Enable macros in a workbook


macro

Copy and edit a • 1.1.1: Copy macros between workbooks


macro with Visual • 3.6.2: Name simple macros
Basic Editor • 3.6.3: Edit simple names

Record a macro • 3.6.1: Record simple macros

Table 3: Cornerstone objectives

Duration
50 minutes

Instructions
1. Complete the following tasks for each file.
2. When saving your file, add your name to the end of the filename, for example:
Cornerstone_macros_Dwayne_Espino. Follow your teacher’s directions for where to
save your files.
3. When you’re done with the Cornerstone, assess your completion and enter the
points that you think you earned within the following task lists. You can ask your
teacher for help if required.

58
Using simple macros

Tasks
You’ll work with two files in this Cornerstone. The following are the tasks you must do
within each file.

File 1: Cornerstone_macros_starter.xlsm
Task: Enable and run a macro (2 points)
1. Open Cornerstone_macros_starter.xlsm and enable macros. This worksheet is an
example of the raw data that Munson’s employees import when generating a report.
(1 point) (Exam objective 1.1.3)
2. Run the macro named AddHeadings to insert column and row headings. You can
use the ribbon or the keyboard shortcut combination Ctrl+Shift+H. (1 point)
Points scored: Select here to enter text. / 2
FILE 1 TOTAL POINTS: Select here to enter text./ 2

File 2: Cornerstone_report_starter.xlsm
Task: Copy and edit a macro with the Visual Basic Editor (9
points)
1. Open Cornerstone_report_starter.xlsm and enable macros. This worksheet is the
report that you’ll prepare by using macros.
2. Access the Visual Basic Editor and copy Module1 from
Cornerstone_report_starter.xlsm to Cornerstone_macros_starter.xlsm. This will
create a new module named Module11. (1 point) (Exam objective 1.1.1)
3. Copy only the AddHeadings VBA code from Cornerstone_macros_starter.xlsm to
Cornerstone_report_starter.xlsm. Place it after the DeleteHeadings macro already
in the workbook. (2 points) (Exam objective 1.1.1)
4. Close Cornerstone_macros_starter.xlsm.
5. In the VBA code for Cornerstone_report_starter.xlsm, make the following edits to
the AddHeadings macro:
a. Change the font color from vbRed to vbBlack.
b. Change the bold setting from False to True. (4 points) (Exam objective 3.6.3)
6. Change the name of the DeleteHeadings macro to RemoveHeadings. (1 point)
(Exam objective 3.6.2)

59
Using simple macros

7. Close the Visual Basic Editor and switch back to Cornerstone_report_starter.xlsm


in Excel.
8. Run the AddHeadings macro. (1 point)
Points scored: Select here to enter text. / 9

Task: Record a macro (3 points)


1. Create a new macro with the following settings:
a. Name: AddInfo
b. Shortcut key: Ctrl+Shift+I
c. Description: Adds company information at the top of the current worksheet
(1 point)
2. The macro should perform the following actions:
a. Add three blank rows before the existing row 1.
b. Place the text Munson’s Pickles and Preserves Farm in A1. Apply the Title style
to this text.
c. Place the text http://www.munsonspicklesandpreservesfarm.com in A2.
d. Place the text Summary Report in A3. (2 points) (Exam objective 3.6.1)
Points scored: Select here to enter text. / 3
FILE 2 TOTAL POINTS: Select here to enter text./12

60

You might also like