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 −
You have already learnt running a macro from the View tab on the Ribbon. A quick recap −
The Macro dialog box appears.
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.
The Create Macro dialog box appears.
The Macro dialog box appears.
The Macro Options dialog box appears. Type a letter, say q, in the box next to Ctrl + under Shortcut key. Click OK.
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.
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.
The Excel Options dialog box appears. Select Macros from the dropdown list under the category- Choose commands from.
A list of macros appears under Macros.
The macro name appears on the right side, with a macro button image.
To change the macro button image, proceed as follows −
The Modify Button dialog box appears. Select one symbol to set it as the icon of the button.
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.
The Macro name and the icon symbol change in the right pane. Click OK.
The macro button appears on the Quick Access Toolbar and the macro display name appears when you place the pointer on the button.
To run the macro, just click the macro button on the Quick Access Toolbar.
You can add a custom group and a custom button on the Ribbon and assign your macro to the button.
The Excel Options dialog box appears.
The New Tab (Custom) appears in Main Tabs list.
The New Group (Custom) appears under New Tab (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.
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).
The Rename dialog box appears. Type the group name in the Display name dialog box and click OK.
The new group name changes to Personal Macros (custom).
Click Macros in the left pane under Choose commands from.
The macro will be added under the Personal Macros (Custom) group.
The position of the tab in the main tabs list determines where it will be placed on the Ribbon. Click OK.
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.
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.
You can run a macro from the Developer tab.
The Macro dialog box appears. Click the macro name and then click Run.
You can run a macro from the VBA editor as follows −