Run a macro
There are several ways to run a macro . You can always run a macro by using the menu command. Depending on how a macro is assigned to run, you may also be able to run it by pressing a CTRL combination shortcut key or clicking a toolbar button or an area on an object, graphic, or control. Also, you can run a macro automatically when the workbook is opened.
Note When the macro security level in Microsoft Office Excel is set to Disable all macros without notification, Excel will run only those macros that are digitally signed or stored in a trusted location, such as the Excel startup folder. If the macro that you want to run is not digitally signed or located in a trusted location, you can temporarily change the security level that enables all macros.What do you want to do?
Run a macro by pressing a CTRL combination shortcut key
Run a macro by clicking a button on the Quick Access Toolbar
Run a macro by clicking an area on a graphic object
Run a macro automatically upon opening a workbook
Run a macro
- If the
Developertab is not available, do the following to display it:- Click the
Microsoft Office Button
, and then click Excel Options. - In the
Popularcategory, underTop options for working with Excel, select theShow Developer tab in the Ribboncheck box, and then clickOK.
- Click the
- To set the security level temporarily to enable all macros, do the following:
- On the
Developertab, in theCodegroup, clickMacro Security.
- In the
Macro Settingscategory, underMacro Settings, clickEnable all macros (not recommended, potentially dangerous code can run), and then clickOK.
Note To help prevent potentially dangerous code from running, we recommend that you return to any of the settings that disable all macros after you finish working with macros.
- On the
- Open the workbook that contains the macro.
- On the
Developertab, in theCodegroup, clickMacros. - In the
Macro namebox, click the macro that you want to run. - Do one of the following:
- To run a macro in an Excel workbook, click
Run.Tip You can also press CTRL+F8 to run the macro. You can interrupt the execution of the macro by pressing ESC.
- To run a macro from a Microsoft Visual Basic module, click
Edit, and then on theRunmenu, clickRun Sub/UserForm
, or press F5.
Tip You can run a different macro while you are working in the Visual Basic Editor. On the
Developertab, in theCodegroup, clickMacros. In theMacro namebox, click the macro that you want to run, and then clickRun. - To run a macro in an Excel workbook, click
Run a macro by pressing a CTRL combination shortcut key
- On the
Developertab, in theCodegroup, clickMacros.
- In the
Macro namebox, click the macro that you want to assign to a CTRL combination shortcut key. - Click
Options. - In the
Shortcut keybox, type any lowercase letter or uppercase letter that you want to use.Note The shortcut key will override any equivalent default Excel shortcut key while the workbook that contains the macro is open. For a list of CTRL combination shortcut keys that are already assigned in Excel, see Excel shortcut and function keys.
- To include a description of the macro, in the
Descriptionbox, type the text that you want. - Click
OK, and then clickCancel.
Run a macro by clicking a button on the Quick Access Toolbar
- Click the
Microsoft Office Button
, and then click Excel Options. - Click
Customize, and then in theChoose commands fromlist, selectPopular Commands. - In the list box, click
View Macros, and then clickAdd. - Click
OK.Tip The
View Macrosbutton will be added to theQuick Access Toolbar.
- On the
Quick Access Toolbar, click theView Macrosbutton. - In the
Macro namebox, click the macro that you want to run, and then clickRun.
Run a macro by clicking an area on a graphic object
- In the worksheet, select an existing graphic object, such as a picture, clip art, shape, or SmartArt.
- To create a hot spot on the existing object, on the
Inserttab, in theIllustrationsgroup, clickShapes, select the shape that you want to use, and then draw that shape on the existing object.
- Right-click the hot spot that you created, and then click
Assign Macroon the shortcut menu . - Do one of the following:
- To assign an existing macro to the graphic object, double-click the macro or enter its name in the
Macro namebox. - To record a new macro to assign to the selected graphic object, click
Record. When you finish recording the macro, clickStop Recording
on the Developertab in theCodegroup.Tip You can also click
Stop Recording
on the left side of the status bar.
- To edit an existing macro, click the name of the macro in the
Macro namebox, and then clickEdit.
- To assign an existing macro to the graphic object, double-click the macro or enter its name in the
- Click
OK. - In the worksheet, select the hot spot.
Tip This displays the
DrawingTools, adding aFormattab.
- On the
Formattab, in theShape Stylesgroup, do the following:- Click the arrow next to
Shape Fill, and then clickNo Fill. - Click the arrow next to
Shape Outline, and then clickNo Outline.
- Click the arrow next to
Run a macro automatically upon opening a workbook
If you record a macro and save it by using the name "Auto_Open", the macro will run whenever you open the workbook that contains the macro. Another way to automatically run a macro when you open a workbook is to write a Visual Basic for Applications (VBA) procedure in the Open event of the workbook by using the Visual Basic Editor . The Open event is a built-in workbook event that runs its macro code each time that you open the workbook.
Create an Auto_Open macro
- If the
Developertab is not available, do the following to display it:- Click the
Microsoft Office Button
, and then click Excel Options. - In the
Popularcategory, underWorking with Excel and other Office applications, select theDeveloper toolscheck box, and then clickOK.
- Click the
- To set the security level temporarily to enable all macros, do the following:
- On the
Developertab, in theCodegroup, clickMacro Security.
- In the
Macro Settingscategory, underMacro Settings, clickEnable all macros (not recommended, potentially dangerous code can run), and then clickOK.
Note To help prevent potentially dangerous code from running, we recommend that you return to any of the settings that disable all macros after you finish working with macros.
- On the
- If you want to save the macro with a particular workbook, open that workbook first.
- On the
Developertab, in theCodegroup, clickRecord Macro. - In the
Macro namebox, type Auto_Open. - In the
Store macro inlist, select the workbook in which you want to store the macro.Tip If you want a macro to be available whenever you use Excel, select
Personal Macro Workbook. When you choosePersonal Macro Workbook, Excel creates a hidden personal macro workbook (Personal.xlsb), if it doesn't already exist, and saves the macro in this workbook. In Microsoft Windows XP, this workbook is saved in the C:\Documents and Settings\user name\Application Data\Microsoft\Excel\XLStart folder so that it can be loaded automatically whenever Excel starts. In Microsoft Windows Vista, this workbook is saved in the C:\Users\user name\Application Data\Microsoft\Excel\XLStart folder. If you want a macro in the personal macro workbook to be run automatically in another workbook, you must also save that workbook in the XLStart folder so that both workbooks are opened when Excel starts. - Click
OK, and then perform the actions that you want to record. - On the
Developertab, in theCodegroup, clickStop Recording
.
Tip You can also click
Stop Recording
on the left side of the status bar.
Notes:
- If you chose to save the macro in
This WorkbookorNew Workbookin step 6, save or move the workbook into one of the XLStart folders. - Recording an Auto_Open macro has the following limitations:
-
- Many actions that you may want to perform cannot be recorded.
- If the workbook in which you save the Auto_Open macro already contains a VBA procedure in its Open event, the VBA procedure for the Open event will override all actions in the Auto_Open macro.
- An Auto_Open macro is ignored when a workbook is opened programmatically by using the Open method.
- An Auto_Open macro runs before any other workbooks are opened. Therefore, if you record actions that you want Excel to perform on the default Book1 workbook or on a workbook that is loaded from the XLStart folder, the Auto_Open macro will fail when you restart Excel, because the macro runs before the default and startup workbooks are opened.
If you encounter these limitations, instead of recording an Auto_Open macro, you must create a VBA procedure for the Open event as described in the next section of this article.
- If you chose to save the macro in
Create a VBA procedure for the Open event of a workbook
The following example uses the Open event to run a macro when you open the workbook.
- If the
Developertab is not available, do the following to display it:- Click the
Microsoft Office Button
, and then click Excel Options. - In the
Popularcategory, underWorking with Excel and other Office applications, select theDeveloper toolscheck box, and then clickOK.
- Click the
- To set the security level temporarily to enable all macros, do the following:
- On the
Developertab, in theCodegroup, clickMacro Security.
- In the
Macro Settingscategory, underMacro Settings, clickEnable all macros (not recommended, potentially dangerous code can run), and then clickOK.
Note To help prevent potentially dangerous code from running, we recommend that you return to any of the settings that disable all macros after you finish working with macros.
- On the
- Save and close all open workbooks.
- Open the workbook that you want to add the macro to, or create a new workbook.
- On the
Developertab, in theCodegroup, clickVisual Basic. - In the Project Explorer window, right-click the
ThisWorkbookobject, and then clickView Codeon the shortcut menu.Tip In the Project Explorer window is not visible, on the
Viewmenu, clickProject Explorer. - In the
Objectlist above the Code window, selectWorkbook.This automatically creates an empty procedure for the Open event like this:
Private Sub Workbook_Open() End Sub
- Add the following lines of code to the procedure:
Private Sub Workbook_Open() MsgBox Date Worksheets("Sheet1").Range("A1").Value = Date End Sub - Switch to Excel and save the workbook as a macro-enabled workbook (.xlsm).
- Close and reopen the workbook. When you open the file again, Excel runs the Workbook_Open procedure, which displays today's date in a message box.
- Click
OKin the message box. - Note that cell A1 on Sheet1 also contains the date as a result of running the Workbook_Open procedure.