Important This feature isn’t available in Office on a Windows RT PC. Want to see what version of Office you're using?
To automate a repetitive task so that you can do the task again with a single click, you can record a macro (macro: An action or a set of actions that you can use to automate tasks. Macros are recorded in the Visual Basic for Applications programming language.) in Excel. You can also create a macro by using the Visual Basic Editor (Visual Basic Editor: An environment in which you write new and edit existing Visual Basic for Applications code and procedures. The Visual Basic Editor contains a complete debugging toolset for finding syntax, run-time, and logic problems in your code.) in Microsoft Visual Basic for Applications (VBA) to write your own macro programming code, or you can copy all or part of a macro to a new macro. After you create a macro, you can assign it to an object on your spreadsheet (such as a toolbar button, graphic, or control) so that you can run the macro by clicking the object. If you no longer use a macro, you can delete it.
What do you want to do?
Record a macro
When you record a macro, the macro recorder records all the steps required to complete the actions that you want your macro to perform. These steps can include typing text or numbers, clicking cells or commands on the ribbon or on menus, formatting, selecting cells, rows, or columns, and dragging your mouse to select cells on your spreadsheet. The commands for recording, creating, and deleting macros are available on the Developer tab on the Ribbon.
- If the Developer tab is not available, do the following to display it:
- Click the File tab.
- Click Options, and then click Customize Ribbon.
- In the Customize Ribbon category, in the Main Tabs list, select the Developer check box, and then click OK.
- You may first need to set the security level to enable all macros. To do this:
- On the Developer tab, in the Code group, click Macro Security.
- Under Macro Settings, click Enable all macros (not recommended, potentially dangerous code can run), and then click OK.
Caution To help prevent potentially dangerous code from running, we recommend that you return to any one of the settings that disable all macros after you finish working with macros.
- On the Developer tab, in the Code group, click Record Macro.
- In the Macro name box, enter a name for the macro.
Note The first character of the macro name must be a letter. Subsequent characters can be letters, numbers, or underscore characters. Spaces cannot be used 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 Control (Ctrl) combination shortcut key (shortcut key: A function key or key combination, such as F5 or CTRL+A, that you use to carry out a menu command. In contrast, an access key is a key combination, such as ALT+F, that moves the focus to a menu, command, or control.) to run the macro, in the Shortcut key box, type any lowercase letter or uppercase letter that you want to use.
The shortcut key will override any equivalent default Excel shortcut key while the workbook that contains the macro is open.
- In the Store macro in list, select the workbook where you want to store the macro.
If you want a macro to be available whenever you use Excel, select Personal Macro Workbook. When you select Personal Macro Workbook, Excel creates a hidden personal macro workbook (Personal.xlsb) if it does not already exist, and saves the macro in this workbook. In Windows 7 and Windows Vista, this workbook is saved in the C:\Users\user name\AppData\Local\Microsoft\Excel\XLStart folder. In Microsoft Windows XP, this workbook is saved in the C:\Documents and Settings\user name\Application Data\Microsoft\Excel\XLStart folder. Workbooks in the XLStart folder are opened automatically whenever Excel starts. If you want a macro in the personal macro workbook to be run automatically in another workbook, you need to also save that workbook in the XLStart folder so that both workbooks are opened when Excel starts.
- In the Description box, type a description of the macro.
- Click OK to start recording.
- Perform the actions that you want to record.
- On the Developer tab, in the Code group, click Stop Recording .
Top of Page
Create a macro by using VBA
- On the Developer tab, in the Code group, click Visual Basic.
- If needed, in the Visual Basic Editor, on the Insert menu, click Module.
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.
- In the Visual Basic Editor, on the File menu, click Close and Return to Microsoft Excel when you finish writing the macro.
Top of Page
Copy part of a macro to create another macro
- Open the workbook that contains the macro that you want to copy.
- On the Developer tab, in the Code group, click Macros.
- In the Macro name box, 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 whole macro, include the Sub and End Sub lines in the selection.
- On the Edit menu, click Copy.
- In the Procedure box of the code window, click the module where you want to place the code.
- On the Edit menu, click Paste.
You may not be able to edit your Personal Macro Workbook file (Personal.xlsb) because it is a hidden workbook that is always open. You need to first unhide it by using the Unhide command. You can also open Personal.xlsb in the Visual Basic Editor by pressing Alt+F11.
Top of Page
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 Macro.
- In the Macro name box, click the macro that you want to assign.
Top of Page
Delete a macro
- Do one of the following:
- Open the workbook that contains the macro that you want to delete.
- If the macro that you want to delete is stored in the personal macro workbook (Personal.xlsb), and this workbook is hidden, do the following to unhide the workbook:
- On the View tab, in the Window group, click Unhide.
- Under Unhide workbooks, click PERSONAL, and then click OK.
- In the Macros in list, select the workbook that contains the macro that you want to delete. For example, click This Workbook.
- In the Macro name box, click the name of the macro that you want to delete.
- Click Delete.
Top of Page