Excel: Introduction To Macros: What Is A Macro?

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

Excel: Introduction to Macros

What is a macro?
A macro is a set of instructions that allow you to automate repetitive tasks.

Creating a Macro
Excel provides two ways to create a macro. You can use the Macro Recorder or you can type your
instructions using Visual Basic for Applications (VBA).

If you use the Macro Recorder, Excel records your commands, keystrokes, and mouse clicks and Excel
stores these instructions as VBA code. If you write your own macros, you will write the instructions in
the native VBA programming language.

Recording a Macro
In this class, we will use the DEVELOPER tab to record and run a macro. To accomplish this, you may
need to customize your Ribbon to include the DEVELOPER tab.

Scottsdale Community College, CIS 105, Excel 1


To display the DEVELOPER tab on the Ribbon:
1. Click the FILE tab and then click Options to open the Excel Options dialog box.
2. In the left pane, click Customize Ribbon.
3. Click the Developer check box to select it and then click the OK button.

The process to record a macro is as follows:

1. Click the DEVELOPER tab.


2. Then click Record Macro in the Code group.

Scottsdale Community College, CIS 105, Excel 2


3. Type a name for your macro in the Macro name box. Macro names must start with a letter and
they cannot include spaces or special characters.
4. Type a name for your macro in the Macro name box. Macro names must start with a letter and
they cannot include spaces or special characters.
5. Assign a keyboard shortcut key, if you’d like.
6. Select where you would like to store the macro. The default is in the current Workbook.

7. Type a brief description of what the macro does and then click the OK button to start recording
your macro.
8. Perform the steps that you want to record.
9. Click the DEVELOPER tab and click Stop Recording in the Code group.
Note: by default, a macro records absolute cell references. In the Code group, you can change this
option by clicking on Use Relative References.

Running a Macro
After you record your macro, you should run it to test that it works correctly.

The process to run a macro is as follows:

1. Click the DEVELOPER tab.


2. Click the Macros button in the Code group.

Scottsdale Community College, CIS 105, Excel 3


3. Select your macro from the list of macros.
4. Click the Run button.

Note: Alternatively, you can run your macro by typing the keyboard shortcut, assuming you assigned
one to your macro.

Creating a Macro Button


You may assign a macro to a button on a worksheet to make it easy for a user of the worksheet to
execute the macro.

The process to add a macro button to a worksheet is as follows:

1. Click the DEVELOPER tab.


2. Click Insert in the Controls group.
3. In the Form Controls section, click Button (Form Control).

Scottsdale Community College, CIS 105, Excel 4


4. Drag the crosshair pointer to position and size your macro button in the worksheet where you
want it to be placed.
5. When you release the mouse button, the Assign Macro dialog box will open.
6. Select the macro to assign to your button and click OK.
7. Right-click the button and select Edit Text to type a descriptive label on the button.

8. Click the worksheet to complete the button.


9. Place your cursor in the desired active cell in your worksheet. Click your macro button to run
and test your macro.

Editing a Macro in the Visual Basic Editor


You can edit the VBA code created in an existing, recorded macro via the Visual Basic Editor. To access
the Visual Basic Editor, do the following:

1. Click the DEVELOPER tab.


2. In the Code group, click the Visual Basic button.

3. Use the Visual Basic Editor to edit the macro code.


4. Save and exit the Visual Basic Editor to return to Microsoft Excel.

Note: Unless you are comfortable with VBA code, you may find it easier to re-record a macro to make
enhancements or correct errors.

Scottsdale Community College, CIS 105, Excel 5


Saving a Workbook with a Macro
The default Excel Workbook file format (.xlsx) cannot store a macro. When you save a workbook
containing a macro, in the Save As dialog box, click the Save as type dropdown arrow. Then choose the
Excel Macro-Enabled Workbook (.xlsm) file type.

Scottsdale Community College, CIS 105, Excel 6

You might also like