How to create a macro in Microsoft Word and Excel

Updated: 05/01/2023 by Computer Hope
Microsoft Word

A macro helps improve efficiency when creating documents and spreadsheets, adding or maintaining information, or creating forms for other people to fill out. Microsoft Word and Excel are examples of software that use macros to enhance documents and automate repetitive tasks. Macros in Word and Excel are created using the Visual Basic programming language or by recording performed actions.

To create a macro in Microsoft Word or Microsoft Excel, select from the list below and follow the instructions.

Create a macro in Microsoft Word

To create a macro in Microsoft Word, choose how you want to create it, either by writing Visual Basic code or by recording actions, and follow the steps provided.

Note

If you are using Microsoft Word Online (the web version), you must open a document in the Microsoft Word desktop application to create a macro. Word Online does not include the ability to create macros.

Create macro by coding it with Visual Basic

  1. In Microsoft Word, click the View tab in the menu bar.
  2. Click the Macros option.

Create macro in Microsoft Word

  1. In the Macros window, type a name for the new macro in the Macro name text field.
  2. Click the Create button.
  3. The Microsoft Visual Basic for Applications program opens, where you can create the macro by writing Visual Basic code.
  4. When you have completed the creation of the macro, click the Save icon in the menu bar and close the Visual Basic program.

Create macro by recording actions

  1. In Microsoft Word, click the View tab in the menu bar.
  2. Click the arrow below the Macros option and select the Record Macro option.

Record macro in Microsoft Word

  1. In the Record Macro window, type a name for the new macro in the Macro name text field.
  2. Click the keyboard icon to assign a key combination to execute the macro.
  3. In the Customize Keyboard window, click in the Press new keyboard shortcut key field, then press the combination of keys you want to use for executing the macro in Microsoft Word. For example, you could press the keys Ctrl+Shift+M, and that key combination will be entered into the field automatically.

Assign key combination to execute recorded macro in Word

  1. Click the Assign button in the lower-left corner, then click the Close button.
  2. Recording of the macro begins immediately after clicking the Close button. Perform the actions you want to record and have the macro execute. You can type text, format the text, create a table, add shapes or pictures, and other actions.
  3. When you completed all the actions you want to include in the macro, on the View tab, click the arrow below the Macros option and select Stop Recording.

Create a macro in Microsoft Excel

To create a macro in Microsoft Excel, choose how you want to create it, either manually or by recording actions, and follow the steps provided.

Note

If you are using Microsoft Excel Online (the web version), you must open a spreadsheet in the Microsoft Excel desktop application to create a macro. Excel Online does not include the ability to create macros.

Create macro manually with Visual Basic

  1. In Microsoft Excel, click the View tab in the menu bar.
  2. Click the Macros option.

Create macro in Microsoft Excel

  1. In the Macros window, type a name for the new macro in the Macro name text field.
  2. Click the Create button.
  3. The Microsoft Visual Basic for Applications program opens, where you can create the macro by typing the Visual Basic code manually.
  4. When you have completed the creation of the macro, click the Save icon in the menu bar and close the Visual Basic program.

Create macro by recording actions

  1. In Microsoft Excel, click the View tab in the menu bar.
  2. Click the arrow below the Macros option and select the Record Macro option.

Record macro in Microsoft Excel

  1. In the Record Macro window, type a name for the new macro in the Macro name text field.
  2. Click the keyboard icon to assign a key combination to execute the macro.
  3. In the Customize Keyboard window, click in the Press new keyboard shortcut key field, then press the combination of keys you want to use for executing the macro in Microsoft Excel. For example, you could press the keys Ctrl+Shift+M, and that key combination will be entered into the field automatically.

Assign key combination to execute recorded macro in Excel

  1. Click the Assign button in the lower-left corner, then click the Close button.
  2. Recording of the macro begins immediately after clicking the Close button. Perform the actions you want to record and have the macro execute. You can enter data, format the data, create a chart or graph from the data, add formulas, and other actions.
  3. When you completed all the actions you want to include in the macro, on the View tab, click the arrow below the Macros option and select Stop Recording.