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 the View Tab
  • Running a Macro by pressing Ctrl plus a shortcut key
  • Running a Macro by clicking a button on the Quick Access Toolbar
  • Running a Macro by clicking a button in a Custom Group on the Ribbon
  • Running a Macro by clicking on a Graphic Object
  • Running a Macro from Developer Tab
  • Running a Macro from VBA Editor

Running a Macro from View Tab

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

  • Click the VIEW tab on the Ribbon.
  • Click Macros.
  • Select View Macros from the dropdown list.
Active Cell

The Macro dialog box appears.

  • Click the macro name.
  • Click the Run button.
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

  • Click the VIEW tab.
  • Click Macros.
  • Select Record Macro from the dropdown list.

The Create Macro dialog box appears.

  • Type a macro name
  • Type a letter, say q, in the box next to Ctrl + under Shortcut key.
Adding

Adding a Shortcut Key in Macro Options

  • Click the VIEW tab.
  • Click Macros.
  • Select View Macros from the dropdown list.

The Macro dialog box appears.

  • Select the macro name.
  • Click the Options button.
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 −

  • Right click on the Quick Access Toolbar.

  • Select Customize Quick Access Toolbar from the dropdown list.

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.

  • Click PERSONAL.XLSB!MyMacro.
  • Click the Add button.
List

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

To change the macro button image, proceed as follows −

  • Click the macro name in the right box.
  • Click the Modify button.
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.

  • Right click on the Ribbon.
  • Select Customize the Ribbon from the dropdown list.
Custom Group

The Excel Options dialog box appears.

  • Select Main Tabs under Customize the Ribbon.
  • Click New Tab.
Excel Option

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

  • Click New Tab (Custom).
  • Click the New Group button.

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

  • Click New Tab (Custom).
  • Click the Rename button.
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).

  • Click New Group (Custom).
  • Click the Rename button.
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
  • Select your macro name, say – MyFirstMacro from the macros list.
  • Click the Add button.
Macro List

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

Personal Macros
  • Click My Macros (Custom) in the list.
  • Click the arrows to move the tab up or down.
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.

  • Click the Developer tab on the Ribbon.
  • Click Macros.
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 −

  • Click the Run tab on the Ribbon.
  • Select Run Sub/UserForm from the dropdown list.
UserForm
Advertisements