Creating Macros in Microsoft Excel
Creating Macros in Microsoft Excel
IN MICROSOFT EXCEL
WHAT, WHY & WHEN . . .
WHAT IS A MACRO?
• A macro is an action or a set of actions that you can run as many times as you want.
WHY WOULD YOU USE A MACRO?
• If you have tasks in Microsoft Excel that you do repeatedly, you can record a macro to automate
those tasks, saving you time. When you create a macro, you are recording your mouse clicks and
keystrokes. After you create a macro, you can edit it to make minor changes to the way it works.
WHEN WOULD I USE IT?
• Any time you want data to stand out, to automatically create charts, identify changes to
information etc. You want to format the names of the customers with overdue accounts in red,
and also apply bold formatting. You can create and then run a macro that quickly applies these
formatting changes to the cells you select.
2
Insert Image
RECORDING A MACRO
When you record a macro, the macro recorder records ALL the steps in Visual Basic for
Applications (VBA) code.
These steps can include typing text or numbers, clicking cells or commands on the ribbon
or on menus, formatting cells, rows, or columns, or even importing data from an external
source.
Visual Basic Application (VBA) is a subset of the powerful Visual Basic programming
language and is included with most Office applications. Although VBA gives you the
ability to automate processes within and between Office applications, it is not necessary
to know VBA code or computer programming if the Macro Recorder does what you want.
3
Insert Image
TIPS ON RECORDING
• It is important to know that when you record a macro, the Macro Recorder captures
almost every move you make.
• If you make a mistake in your sequence, for example, clicking a button that you did not
intend to click, the Macro Recorder will record it.
• The resolution is to re-record the entire sequence or modify the VBA code itself.
• This is why whenever you record something, it's best to record a process you are highly
familiar with. The more smoothly you record a sequence, the more efficiently the macro
will run when you play it back.
4
Insert Image
Although Macros automate frequently used tasks those that are created with VBA can be made by
malicious people to cause destruction and even spread a virus on your computer.
This complicated issue offers several possible solutions for you to choose from:
• Disable all macros without notification.
Disable all macros with notification (default).
• Disable all macros except digitally signed macros.
• Enable all macros (not recommended).
• Trust access to the VBA project object model.
• You may also use a trusted location on your computer drive or network share so that the macros
can run without being checked by the Trust Center security system.
5
Insert Image
LESSON OBJECTIVES
In this lesson we will use Visual Basic for Applications (VBA) to:
1. Create a row Macro in an absolute form
2. Create a row Macro in a relative reference form
3. Create a column Macro in a relative reference form
4. Delete a Macro
MICROSOFT EXCEL
After you show the tab, the Developer tab stays visible, unless you clear the check box or have to reinstall a
Microsoft Office program.
8
Insert Image
11
Insert Image
12
Insert Image
MODIFY AN EXISTING MACRO AND SAVE IT AS ANOTHER MACRO
13
Insert Image
DELETING A MACRO
14
Insert Image
1. Click on File->Save
2. A message will appear stating that you need to save it as a macro-enabled
document
3. Click on No, which will open a Save-As window
4. Open the list and select Excel Macro-Enabled Workbook
5. Click OK
15
Insert Image
ASSIGNMENT:
Upload your MacroExample Excel Macro-Enabled Workbook to Canvas. You
should see a file extension of .xlsm
See the picture on the following slide of what your document should look like.
NOTE: Assignments submitted without the Macros will result in a zero
16
Insert Image
17