To add another macro in Excel, open the workbook where you want it, press Alt+F11 (Option+F11 on a Mac) to open the Visual Basic Editor, insert a new module with Insert > Module, and write or paste your Sub procedure there. Then assign it to a button, shortcut, or the Quick Access Toolbar so you can run it. The key idea is that each macro is just a separate Sub block, and multiple macros can live side by side in the same module or in different modules inside the same workbook.

Labels in the ribbon, the Visual Basic Editor, and the Trust Center can differ slightly by Excel version, platform, and region, so if a name looks different on your screen, look for the closest equivalent.

What You'll Need

  • Microsoft Excel for Windows or Mac with macros enabled. Excel for the web cannot create or edit VBA macros; it can only open workbooks that already contain them, and even then macro execution may be restricted.
  • A workbook saved as .xlsm (Excel Macro-Enabled Workbook) so your macros are kept. Saving as .xlsx strips out VBA code.
  • Access to the Developer tab, or permission to turn it on via File > Options > Customize Ribbon (Excel > Preferences on Mac).
  • Macro security settings that allow macros in trusted locations. If your organization manages Excel through Group Policy, you may not be able to change these yourself.
  • If the macro touches files or folders outside the workbook, confirm you have permission to those paths before running it.

Step-by-Step

  1. Save the workbook as macro-enabled first. Go to File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm). If you build macros in a .xlsx file and save normally, Excel warns you that the code will be lost. Doing this first avoids that mistake entirely.
  2. Make the Developer tab visible. Right-click anywhere on the ribbon and choose Customize the Ribbon, then tick Developer in the right-hand list. On Mac, go to Excel > Preferences > Ribbon & Toolbar and enable Developer. If the tab is already there, skip this.
  3. Open the macro editor. Click Developer > Visual Basic, or press Alt+F11 (Option+F11 on Mac). This opens the Visual Basic Editor in a separate window. On newer Macs you may need to allow it through the macOS security prompt the first time.
  4. Insert a fresh module. In the editor, make sure your workbook is selected in the Project Explorer on the left, then choose Insert > Module. A blank code pane appears. A module is the container for your macro; use a new one for each group of related macros so things stay organized.
  5. Write the new macro. Type a Sub block, for example Sub HighlightRow() followed by your code and ending with End Sub. Give every macro a unique name that starts with a letter and contains no spaces. If you already have other macros, you can add your new Sub in the same module underneath the existing ones, or in a separate module. The editor's dropdown at the top left of the code pane lists every procedure so you can jump between them.
  6. Check for name clashes. Two macros with the same name in the same workbook cause errors or run the wrong one. If you copied a macro from elsewhere, rename it in both the Sub line and any RunMacro references that point to it.
  7. Test it on a copy of your data. Press F5 with the cursor inside your Sub, or return to the sheet and run it from Developer > Macros. Confirm it does what you expect before wiring it to anything. Use Ctrl+Z cautiously: many macros cannot be undone.
  8. Attach it to a way of running it. For a one-click button, use Developer > Insert > Button (Form Control), draw it on the sheet, and pick your macro from the Assign Macro dialog. For a keyboard shortcut, go to Developer > Macros, select the macro, click Options, and assign a letter. To put it on the Quick Access Toolbar, right-click the ribbon, choose Customize Quick Access Toolbar, and set the dropdown to Macros.
  9. Save and keep a backup copy. Save the .xlsm file and keep a dated copy elsewhere, especially if the macro edits data. If the workbook lives on a shared drive, check with anyone else who uses it before adding a macro that changes sheets or files.

Common Mistakes

  • Saving as .xlsx. This is the most frequent cause of a disappearing macro. Excel warns you, but people click through the prompt. Always confirm the file type in the title bar.
  • Putting code in the wrong place. Code pasted into a sheet object (Sheet1) fires on events and does not appear in the macro list. It needs to go in a standard Module, not the ThisWorkbook object or a user form.
  • Duplicate names. Copying a macro without renaming it leaves two Subs with the same name and causes a compile error.
  • Assuming Undo works. VBA does not add to the undo stack, so an accidental deletion or overwrite may be permanent. Work on a copy.
  • Hard-coding paths or sheet names. A macro that references a specific sheet name breaks the moment someone renames it, which is a common problem in a shared workbook.
  • Leaving the file in a macro-hostile folder. If the workbook sits in an untrusted location, macros may be blocked each time it opens.

Tips

  • Record a macro first with Developer > Record Macro, then open the editor to see the generated code. It is the fastest way to learn the syntax for a new Sub.
  • Start each macro with a comment line such as 'Purpose: cleans data so you remember later what it does.
  • Use Option Explicit at the top of each module to force variable declarations and catch typos early.
  • Keep small, single-purpose macros rather than one giant Sub; they are easier to test, assign to buttons, and reuse.
  • Give each button or toolbar entry a clear caption so nobody has to guess what clicking it does.
  • If the workbook will be shared, put a short note in a cell explaining what the macros do and which ones are safe to run.

Troubleshooting

  • The macro is missing from the list. Check you are looking at the correct workbook and that the code is in a standard Module. Private Subs also do not appear in the macro dialog.
  • Macros are disabled and a security warning appears. Use Enable Content if you trust the file. If you do not, do not enable it. Persistent blocking usually means the file is in a blocked location or the Trust Center settings are low. A business-managed computer may need an administrator.
  • A compile error appears. The editor highlights the offending line. Check for typos, missing End Sub, or mismatched quotes. Click Debug when prompted to jump to the line.
  • It runs but does nothing. Step through with F8 to watch each line execute, and check that your macro points at the right sheet and range.
  • The button runs the wrong macro. Right-click the button, choose Assign Macro, and reselect the correct Sub.
  • The file opens read-only or the macro refuses to save. Close other copies, check the file is not locked by a sync service or another user, and make sure you have write permission.
  • The editor itself will not open. Check for another instance of Excel running, close and reopen Excel, and confirm the VBA component is installed, which some IT setups restrict.

Frequently Asked Questions

Can I have multiple macros in one module?

Yes. A module can hold any number of Sub procedures, each starting with Sub and ending with End Sub. Separate modules are optional but make organizing and troubleshooting easier.

What is the keyboard shortcut to open the macro editor?

Alt+F11 on Windows and Option+F11 on Mac. This opens the Visual Basic Editor, where you add your new macro.

Can I create a macro in Excel for the web?

No. Excel for the web does not include the Visual Basic Editor, so you need the desktop version of Excel on Windows or Mac to add and edit VBA macros.

Where does a new macro get saved?

Inside the workbook file, so the file must be saved as .xlsm. To make a macro available in every workbook, save it in the Personal Macro Workbook, which is the workbook named Personal.xlsb that Excel keeps in your startup folder.

Can I run a macro without a button?

Yes. You can run it from Developer > Macros, assign a keyboard shortcut through that dialog, or add it to the Quick Access Toolbar.

Why does Excel keep warning me about macros?

It is a security feature. Only enable macros in files from people you trust. Recording and automation laws do not apply here, but macros can change or delete your data, so keep regular backups.