You can save a macro in Excel by using the Developer tab to create a Personal Macro Workbook, which stores the code so it is available on every sheet in the same file. This requires enabling the Developer tab and saving the workbook in a macro-enabled format like .xlsm. The process involves recording the actions, assigning a shortcut key or button, and ensuring the workbook is saved correctly to retain the code for future sessions.
What You'll Need
- A computer with Microsoft Excel installed (Excel 2010 or later is recommended for the most stable macro support).
- A set of actions or formatting tasks you wish to automate (e.g., formatting a report, applying headers, or clearing specific ranges).
- Administrator privileges to enable the Developer tab if it is not already visible in the Ribbon.
- A file saved in a macro-enabled format (.xlsm) to prevent the code from being deleted when the file is closed.
- A keyboard with at least one available shortcut key (e.g., Ctrl, Shift, or Alt) if you plan to assign a shortcut to the macro.
Step-by-Step
- Enable the Developer Tab: Open Excel and navigate to the File tab to select Options. In the Excel Options window, choose Customize Ribbon. On the right side, check the box next to Developer and click OK. This adds the Developer tab to the main toolbar at the top of the window.
- Open the Macro Recorder: Go to the Developer tab on the Ribbon and click Record Macro in the Code group. This opens the Record Macro dialog box where you will configure the settings for the new macro.
- Name the Macro: In the Macro Name field, enter a unique name that follows standard naming rules: start with a letter, use no spaces, and use underscores instead of spaces. For example, "Format_Sales_Report_2024" is a clear and descriptive name that helps you identify the macro later.
- Assign a Shortcut Key (Optional): In the Shortcut key box, type a letter or combination (e.g., Ctrl+Shift+F). This allows you to run the macro by pressing the keys instead of navigating through menus. Be aware that custom shortcuts can override default Excel shortcuts, so choose a combination that does not interfere with essential Excel functions.
- Select the Storage Location: In the Store macro in dropdown, choose Personal Macro Workbook. This is the critical step for saving the macro for future use. Selecting This Workbook saves the macro only to the current file, while New Workbook saves it to a temporary file that disappears when Excel closes. Personal Macro Workbook creates a hidden file in your Excel profile that remains available every time you open Excel.
- Start Recording: Click OK in the Record Macro dialog box. Excel now begins recording your actions. Every click, keystroke, and formatting change you make will be saved as code. The Stop Recording button in the Developer tab will now be active.
- Perform the Actions: Execute the exact sequence of actions you want to automate. For example, select a range of cells, apply a bold font, change the background color to light blue, and insert a header. Ensure the actions are precise and cover the entire workflow you intend to repeat.
- Stop Recording: Once you have completed the actions, return to the Developer tab and click Stop Recording. The macro is now saved in the Personal Macro Workbook.
- Save the Workbook: Close Excel completely. When prompted to save changes to the Personal Macro Workbook, click Save. This finalizes the storage of the macro. If you do not close Excel, the macro may not be committed to the Personal Workbook.
- Verify the Macro Exists: Open Excel again and go to the Developer tab. Click Macro in the Code group. In the Macro dialog box, look for your macro name in the list of available macros. If you see it listed under Personal Macro Workbook, the save was successful. You can then click Edit to view the VBA code or Run to test the macro.
Common Mistakes
One of the most frequent errors when saving macros is selecting the wrong storage location. Choosing "This Workbook" or "New Workbook" will cause the macro to disappear when you close Excel. To ensure the macro is available in every session, you must explicitly select Personal Macro Workbook. Another common mistake is failing to save the file in a macro-enabled format. If you save the file as .xlsx, Excel will strip out the VBA code to maintain compatibility with other programs, effectively deleting your macro.
Tips
When naming macros, be consistent and descriptive. Using names like "Macro1" or "MyMacro" makes it difficult to identify which macro does what, especially if you create many of them. It is also helpful to add a description in the Macro dialog box when you create the macro. This text appears in the Macro dialog box later, providing context about what the macro does without requiring you to open the VBA editor. If you plan to share the workbook with others, remember to save it as .xlsm and warn recipients that macros are enabled, as disabling them will break the functionality.
Troubleshooting
If you cannot see the Developer tab, you likely need to enable it via Excel Options. If the macro does not appear in the Macro dialog box, double-check that you selected Personal Macro Workbook and that you have saved the Personal Macro Workbook when closing Excel. Sometimes, the Personal Macro Workbook file can become corrupted or hidden. To fix this, you can go to the Developer tab, click Visual Basic, and in the VBA editor, look in the VBAProject pane for "VBAProject (Personal.xlsb)". If it is missing or shows an error, you may need to create a new Personal Macro Workbook by recording a blank macro and saving it again.
Frequently Asked Questions
Why is my macro not available in other Excel files?
This usually happens because the macro was saved to "This Workbook" or "New Workbook" instead of the Personal Macro Workbook. The Personal Workbook is a hidden file stored in your Excel profile that is available on all sheets and files. Ensure you select that option in the Store macro in dropdown when recording.
Can I save a macro in Excel Online or the web version?
No, the web version of Excel does not support VBA macros. Macros can only be created, edited, and saved in the desktop applications for Windows or macOS. The online version allows you to open and view macro-enabled files, but it cannot save new macros.
What happens if I delete the Personal Macro Workbook?
If you delete the Personal Macro Workbook file, all macros you have saved there will be permanently lost. You will need to re-record any macros you use. To prevent accidental deletion, you can back up the file, which is typically located in your user profile folders under AppData\Roaming\Microsoft\Excel\XLSTART.
How do I run a macro from the Personal Workbook on a different computer?
Because the Personal Macro Workbook is tied to your specific Excel profile on a specific computer, you cannot transfer it to another computer. Macros saved in the Personal Workbook are not compatible across different machines. To use a macro on a different computer, you must copy the VBA code from the Personal Workbook, create a new module in the target computer's Personal Workbook, and paste the code there.
Is it safe to use macros from unknown sources?
No. Macros are executable code that can perform actions on your computer, including deleting files or spreading malware. Only run macros from trusted sources or files you have created yourself. Always enable Content when prompted by Excel if you are sure the file is safe.
Can I convert an Excel file with macros to a PDF?
Yes, you can save an .xlsm file as a PDF, but the macros will not function in the PDF. The PDF will preserve the data and formatting, but the automation code will be removed. This is often done to share the final report without exposing the underlying logic or potential security risks associated with macros.