English 中文(简体)
Excel Macros - Running a Macro
  • 时间:2024-12-22

Excel Macros - Running a Macro


Previous Page Next Page  

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 earper.

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 cpcking a button on the Quick Access Toolbar

    Running a Macro by cpcking a button in a Custom Group on the Ribbon

    Running a Macro by cpcking 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 −

    Cpck the VIEW tab on the Ribbon.

    Cpck Macros.

    Select View Macros from the dropdown pst.

Active Cell

The Macro dialog box appears.

    Cpck the macro name.

    Cpck 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

    Cpck the VIEW tab.

    Cpck Macros.

    Select Record Macro from the dropdown pst.

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

    Cpck the VIEW tab.

    Cpck Macros.

    Select View Macros from the dropdown pst.

The Macro dialog box appears.

    Select the macro name.

    Cpck 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. Cpck 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 cpcking 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 cpck on the Quick Access Toolbar.

    Select Customize Quick Access Toolbar from the dropdown pst.

Quick Access

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

Commands

A pst of macros appears under Macros.

    Cpck PERSONAL.XLSB!MyMacro.

    Cpck 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 −

    Cpck the macro name in the right box.

    Cpck 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. Cpck OK.

MyMacro

The Macro name and the icon symbol change in the right pane. Cpck 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 cpck 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 cpck on the Ribbon.

    Select Customize the Ribbon from the dropdown pst.

Custom Group

The Excel Options dialog box appears.

    Select Main Tabs under Customize the Ribbon.

    Cpck New Tab.

Excel Option

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

    Cpck New Tab (Custom).

    Cpck the New Group button.

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

    Cpck New Tab (Custom).

    Cpck 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 cpck 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 capitapzation of words so that it stands out in the standard tabs.

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

    Cpck New Group (Custom).

    Cpck the Rename button.

New Group

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

Display Name

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

Cpck Macros in the left pane under Choose commands from.

Commands from

    Select your macro name, say – MyFirstMacro from the macros pst.

    Cpck the Add button.

Macro List

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

Personal Macros

    Cpck My Macros (Custom) in the pst.

    Cpck the arrows to move the tab up or down.

Arrows

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

Position

Your custom tab – My Macros appears on the Ribbon.

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

Cpck Tab

Running a Macro by Cpcking 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 cpck 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.

    Cpck the Developer tab on the Ribbon.

    Cpck Macros.

Developer Tab

The Macro dialog box appears. Cpck the macro name and then cpck Run.

Cpck Run

Running a Macro from VBA Editor

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

    Cpck the Run tab on the Ribbon.

    Select Run Sub/UserForm from the dropdown pst.

UserForm Advertisements