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

Creating Macros in Microsoft Excel

This document provides an overview of macros in Microsoft Excel, including what macros are, why and when to use them, how to record macros, tips for recording macros, and security issues related to macros. Macros allow automating repetitive tasks by recording actions like mouse clicks and keystrokes. The document demonstrates how to record both absolute and relative reference macros, modify an existing macro, and delete a macro. The objectives are to create different types of macros and demonstrate deleting a macro.

Uploaded by

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

Creating Macros in Microsoft Excel

This document provides an overview of macros in Microsoft Excel, including what macros are, why and when to use them, how to record macros, tips for recording macros, and security issues related to macros. Macros allow automating repetitive tasks by recording actions like mouse clicks and keystrokes. The document demonstrates how to record both absolute and relative reference macros, modify an existing macro, and delete a macro. The objectives are to create different types of macros and demonstrate deleting a macro.

Uploaded by

oh7207887
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

CREATING MACROS

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

SECURITY ISSUES WITH MACROS

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

Let’s Get Started!


6
Insert Image

MICROSOFT EXCEL

1. Open a blank Excel workbook


2. Save the workbook with the name MacroExample
Next, we will disable all macros except digitally signed macros for the
purposes of our assignment.
3. Click on the File tab, then->Options->Trust Center->Trust Center
Settings->Macro Settings.
4. Choose Disable all macros except digitally signed macros
5. Click OK.
7
Insert Image

ACCESSING THE DEVELOPER TAB


The Developer tab isn't displayed by default, but you can add it to the ribbon.
1. Click on the File tab, go to Options > Customize Ribbon.
2. Under Customize the Ribbon and under Main Tabs, select the Developer check box.

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

LET’S RECORD A MACRO


1. Click in cell A1
2. Click on the Record Macro button (bottom left side of Excel screen)
3. Enter the Macro Name: FillRowWithMonthsByName (A brief description of what the macro
does can be entered in the description box of you prefer)
4. Click OK
5. Enter “Jan” in Cell A1
6. Drag the fill handle (green square) in bottom right corner of the cell until you are in cell L1,
release. You should now see the abbreviations for the months of Jan-Dec.
7. Click on the Stop Recording button (same location as before, now it appears as a )
8. Delete the contents of A1-L1
9
Insert Image

NOW LET’S RUN THE MACRO


1. Click on cell A1.
2. Click on the Macros button in the Developer tab (Code group)
3. Choose: FillRowWithMonthsByName (the macro you just created)
4. Click Run, The macro will automatically run and fill in the months by name
5. Note: if you try to run this macro in a cell other than A1 you will get an error
message. This is because the macro is set as an absolute macro.
6. We have now completed Lesson Objective #1: Create a row Macro in an
absolute form
7. Next, we will learn about relative reference macros
10
Insert Image

RELATIVE VS ABSOLUTE MACROS

What is the difference between relative and absolute macro?


• A macro recorded with absolute references places the recorded steps exactly in the cells where it
was recorded, irrespective of the active cell.
• On the other hand, a macro recorded with relative references can perform the recorded tasks at
different parts on the worksheet.

11
Insert Image

RUN A MACRO USING RELATIVE


REFERENCES
1. Click on cell C4.
2. Click on the Use Relative References button in the Developer tab, Code group.
3. Click on the Record Macro button in the Developer tab, Code group
4. Enter the name: FillRowWithMonthsByNameRelRef
5. Enter “Jan” in Cell C4, then drag the fill handle (green square) in bottom right corner of the
cell until you are in cell N4, release. You should now see the abbreviations for the months of
Jan-Dec.
6. Click on the Stop Recording button
7. Click in cell D5, Click Macros button, select and run the new RelRef macro
8. Note: You did not receive an error message since this is a relative reference macro that
allows you to run the macro in any cell.
We have now completed Lesson Objective #2: Create a row Macro in a relative reference form

12
Insert Image
MODIFY AN EXISTING MACRO AND SAVE IT AS ANOTHER MACRO

1. Delete contents of cells C4-N4


2. Click in Cell A1
3. Click on the Macros button, developer tab, codes group.
4. Choose: FillRowWithMonthsByNameRelRef
5. Click Edit. (This opens the VBA editor)
6. Highlight the code from Sub FillRowWithMonthsByNameRelRef down to End Sub, copy the code.
7. Press the Enter key to start a new section, Paste the information you just copied below the End Sub.
8. Edit the name of both Fill lines by changing the word Row to Column, it should now read:
FillColumnWithMonthsByNameRelRef
9. Change A1:L1 in both places to A1:A12
10. Close the VBA Editor by clicking on the Green X.
11. Click on the Macros button, click Run.
12. You will now see the Macro runs vertically (and from any starting cell).
We have now completed Lesson Objective #3: Create a column Macro in an absolute form

13
Insert Image

DELETING A MACRO

1. To delete a Macro you would:


2. Click on the Macros button
3. Highlight the name of the Macro you wish to delete
4. Click on the Delete button

We have now completed Lesson Objective #4: Deleting a Macro

14
Insert Image

SAVE THE DOCUMENT

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

You might also like