Module 5
Module 5
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
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
2
Using simple macros
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.
4
Using simple macros
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
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
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.
Additional information
For more information on the Developer tab, go to: Show the
Developer tab
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 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
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.
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
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.
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
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
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.
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
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
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
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.
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.
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
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.
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.
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
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
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 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
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
29
Using simple macros
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
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 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.
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.
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.
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
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 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
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
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.
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 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
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
47
Using simple macros
Video
To review the video on editing a macro, go to: Edit a macro
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 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
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.
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.
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.
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
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.
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-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.
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.
Visual Basic Editor An environment for creating and editing VBA code and procedures.
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.
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
60