Create a macro
To automate repetitive tasks, you can quickly record a macro in Microsoft Office Excel. You can also create a macro by using the Visual Basic Editor in Microsoft Visual Basic to write your own macro script, or to copy all or part of a macro to a new macro. After you create a macro, you can assign it to an object (such as a toolbar button, graphic, or control) so that you can run it by clicking the object. If you no longer need to use a macro, you can delete it.What do you want to do?
Create a macro by using Microsoft Visual Basic
Copy part of a macro to create another macro
Assign a macro to an object, graphic, or control
Record a macro
When you record a macro, all steps that are needed to complete the actions that you want to record are recorded by the macro recorder. Navigation on the Ribbon is not included in the recorded steps.
- 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.
- Under
Macro 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
- On the
Developertab, in theCodegroup, clickRecord Macro. - In the
Macro namebox, enter a name for the macro.Note The first character of the macro name must be a letter. Following characters can be letters, numbers, or underscore characters. Spaces are not allowed in a macro name; an underscore character works well as a word separator. If you use a macro name that is also a cell reference, you may get an error message that the macro name is not valid.
- To assign a CTRL combination shortcut key to run the macro, 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.
- 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 selectPersonal 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. - To include a description of the macro, in the
Descriptionbox, type the text that you want. - Click
OKto start recording. - 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.
Create a macro by using Microsoft Visual Basic
- 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.
- Under
Macro 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
- On the
Developertab, in theCodegroup, clickVisual Basic. - If needed, on the
Insertmenu, clickModule.Note Modules are automatically created for all sheets in the workbook.
- In the code window of the module, type or copy the macro code that you want to use.
- To run the macro from the module window, press F5.
- On the
Filemenu, clickClose and Return to Microsoft Excelwhen you finish writing the macro.
Copy part of a macro to create another 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.
- Under
Macro 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 that you want to copy.
- On the
Developertab, in theCodegroup, clickMacros. - In the
Macro namebox, click the name of the macro that you want to copy. - Click
Edit. - In the code window of the Visual Basic Editor, select the lines of the macro that you want to copy.
Tip To copy the entire macro, include the
SubandEnd Sublines in the selection. - On the
Editmenu, clickCopy.Tip You can also right-click and then click
Copyon the shortcut menu, or you can press CTRL+C. - In the
Procedurebox of the code window, click the module where you want to place the code. - On the
Editmenu, clickPaste.Tip You can also right-click and then click
Pasteon the shortcut menu, or you can press CTRL+V.
Tip You cannot edit your Personal Macro Workbook file (Personal.xlsb) because it is a hidden workbook that is always open. You must first unhide it by using the Unhide command. You can also open it in the Visual Basic Editor by pressing ALT+F11.
Assign a macro to an object, graphic, or control
- On a worksheet, right-click the object, graphic, or control to which you want to assign an existing macro, and then click
Assign Macroon the shortcut menu. - In the
Macro namebox, click the macro that you want to assign.
Delete a macro
- Open the workbook that contains the macro that you want to delete.
- On the
Developertab, in theCodegroup, clickMacros.
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
- In the
Macros inlist, selectThis Workbook. - In the
Macro namebox, click the name of the macro that you want to delete. - Click
Delete.