Excel Macros - Running a Macro

There are several ways of executing a macro in your workbook. The macro would have been saved in your macro enabled workbook or in your Personal macro workbook that you can access from any workbook as you had learnt earlier.

You can run a macro in the following ways −

Running a Macro from View Tab

You have already learnt running a macro from the View tab on the Ribbon. A quick recap −

Active Cell

The Macro dialog box appears.

Dialog Box

Running a Macro with Shortcut Key

You can assign a shortcut key (Ctrl + key) for a macro. You can do this while recording the macro in the Create Macro dialog box. Otherwise, you can add this later in the Macro Options dialog box.

Adding a Shortcut Key While Recording a Macro

The Create Macro dialog box appears.

Adding

Adding a Shortcut Key in Macro Options

The Macro dialog box appears.

Shortcut Key

The Macro Options dialog box appears. Type a letter, say q, in the box next to Ctrl + under Shortcut key. Click OK.

Type a Letter

To run the macro with the shortcut key, press the Ctrl key and the key q together. The macro will run.

Note − You can use any lowercase or uppercase letters for the shortcut key of a macro. If you use any Ctrl + letter combination that is an Excel shortcut key, you will override it. Examples include Ctrl+C, Ctrl+V, Ctrl+X, etc. Hence, use your jurisdiction while choosing the letters.

Running a Macro through Quick Access Toolbar

You can add a macro button to the Quick Access Toolbar and run the macro by clicking it. This option would be useful when you store your macros in personal macro workbook. The added button will appear on the Quick Access Toolbar in whatever workbook you open, thus making it easy for you to run the macro.

Suppose you have a macro with the name MyMacro in your personal macro workbook.

To add the macro button to the Quick Access Toolbar do the following −

Quick Access

The Excel Options dialog box appears. Select Macros from the dropdown list under the category- Choose commands from.

Commands

A list of macros appears under Macros.

List

The macro name appears on the right side, with a macro button image.

To change the macro button image, proceed as follows −

Modify

The Modify Button dialog box appears. Select one symbol to set it as the icon of the button.

Icon

Modify the Display name that appears when you place the pointer on the Button image on the Quick Access Toolbar to a meaningful name, say, Run MyMacro for this example. Click OK.

MyMacro

The Macro name and the icon symbol change in the right pane. Click OK.

Symbol

The macro button appears on the Quick Access Toolbar and the macro display name appears when you place the pointer on the button.

Pointer

To run the macro, just click the macro button on the Quick Access Toolbar.

Running a Macro in Custom Group

You can add a custom group and a custom button on the Ribbon and assign your macro to the button.

Custom Group

The Excel Options dialog box appears.

Excel Option

The New Tab (Custom) appears in Main Tabs list.

The New Group (Custom) appears under New Tab (Custom).

Custom

The Rename dialog box appears. Type the name for your custom tab that appears in Main tabs on the Ribbon, say - My Macros and click OK.

Rename

Note − All the Main tabs on the Ribbon are in uppercase letters. You can use your discretion to use uppercase or lowercase letters. I have chosen lowercase with capitalization of words so that it stands out in the standard tabs.

The new tab name changes to My Macros (Custom).

New Group

The Rename dialog box appears. Type the group name in the Display name dialog box and click OK.

Display Name

The new group name changes to Personal Macros (custom).

Click Macros in the left pane under Choose commands from.

Commands from Macro List

The macro will be added under the Personal Macros (Custom) group.

Personal Macros Arrows

The position of the tab in the main tabs list determines where it will be placed on the Ribbon. Click OK.

Position

Your custom tab – My Macros appears on the Ribbon.

Click the tab - My Macros. Personal Macros group appears on the Ribbon. MyFirstMacro appears in the Personal Macros group. To run the macro, just click on MyFirstMacro in the Personal Macros group.

Click Tab

Running a Macro by Clicking an Object

You can insert an object such as a shape, a graphic or a VBA control in your worksheet and assign a macro to it. To run the macro, just click the object.

For details on running a macro using objects, refer to chapter – Assigning Macros to Objects.

Running a Macro from the Developer Tab

You can run a macro from the Developer tab.

Developer Tab

The Macro dialog box appears. Click the macro name and then click Run.

Click Run

Running a Macro from VBA Editor

You can run a macro from the VBA editor as follows −

UserForm