To add VBA code in Excel, open the Visual Basic Editor with Alt+F11, insert a module, paste your code, and save the workbook as .xlsm. This gives you custom macros and functions that run inside Excel. You need Excel on Windows or Mac (some features differ), and macros must be enabled in your Trust Center settings.
What You'll Need
- Excel for Windows or Mac (VBA is not available in Excel for the web).
- Macro security settings that allow macros to run—found in Trust Center (Windows) or Preferences (Mac).
- A workbook you can save as .xlsm (macro-enabled).
- Your VBA code ready to paste, or a blank module to write it in.
Step-by-Step
- Enable the Developer tab (if hidden). On Windows: File > Options > Customize Ribbon, tick Developer in the right list, click OK. On Mac: Excel > Preferences > Ribbon & Toolbar, tick Developer, save. Labels can vary slightly by version.
- Open the Visual Basic Editor. Press Alt+F11 (Windows) or Fn+Alt+F11 (Mac). Alternatively, click Developer > Visual Basic. The editor opens in a separate window.
- Insert a module. In the editor, go to Insert > Module. A blank code window appears in the Project Explorer under VBAProject > Modules.
- Paste or type your code. Copy your VBA code and paste it into the module window. If you're writing from scratch, start with
Sub MyMacro()and end withEnd Sub. - Save the workbook as macro-enabled. Close the editor, then File > Save As. Choose Excel Macro-Enabled Workbook (*.xlsm) from the file type list. Saving as .xlsx will strip the code.
- Run the macro. Press Alt+F8 (Windows) or Fn+Alt+F8 (Mac) to open the Macro dialog, select your macro, and click Run. Or assign it to a button via Developer > Insert > Button (Form Control).
- Adjust macro security if needed. If macros are blocked, go to File > Options > Trust Center > Trust Center Settings > Macro Settings (Windows) or Excel > Preferences > Security (Mac). Choose "Disable all macros with notification" to get a prompt each time, or "Enable all macros" only for trusted files—never for files from unknown sources.
Common Mistakes
- Saving as .xlsx. The code is lost when you save in the default format. Always use .xlsm.
- Pasting code into the wrong place. Code must go in a standard module (Insert > Module), not in a worksheet or ThisWorkbook module, unless it's event code tied to that sheet.
- Forgetting to enable macros. If the macro doesn't appear in the list, check Trust Center settings or the yellow security bar.
- Missing references. Some code requires specific libraries (e.g., for file system objects). In the editor, go to Tools > References and tick the required library.
- Syntax errors from copying. Smart quotes or line breaks from web pages can break code. Paste as plain text and check for red text in the editor.
Tips
- Use
Option Explicitat the top of the module to force variable declaration—it catches typos early. - Press F8 to step through code line by line for debugging; F5 runs the whole macro.
- Store personal macros in the Personal Macro Workbook (Excel will prompt when you record a macro and choose that option) so they're available in all workbooks.
- Comment your code with an apostrophe (') to explain what each part does.
- Back up your .xlsm file before making major changes—VBA has no undo.
Troubleshooting
If the Developer tab won't appear, your Excel version may not support it (e.g., Excel for the web). Use the desktop app. If macros are disabled and you see a security warning, click Enable Content only if you trust the file's source. If the macro runs but does nothing, check that it's referencing the correct sheet or range—use ThisWorkbook.Sheets("Sheet1") explicitly. If you get a "Compile error," look for red-highlighted lines and missing End statements. If the Macro dialog is empty, you may have saved as .xlsx or the code is in a worksheet module instead of a standard module. For protected workbooks, you'll need the password to edit VBA. On Mac, some Windows-specific code (e.g., API calls) won't run; look for Mac-compatible alternatives.
Frequently Asked Questions
Can I add VBA code in Excel for the web?
No, Excel for the web does not support VBA. You need the desktop version for Windows or Mac.
Why does my macro disappear after saving?
You likely saved as .xlsx. Save as .xlsm to keep the code.
How do I run a macro without the Developer tab?
Press Alt+F8 (Windows) or Fn+Alt+F8 (Mac) to open the Macro dialog and run it from there.
Is it safe to enable macros?
Only enable macros from trusted sources. Malicious macros can harm your computer. Keep macro security at "Disable with notification" and enable only when you trust the file.
Can I use VBA to automate tasks on a Mac?
Yes, but some Windows-specific features (like certain API calls) won't work. Test your code on the target platform.
What's the difference between a module and a worksheet module?
Standard modules hold general-purpose macros. Worksheet modules hold event code (e.g., Worksheet_Change) that runs automatically when something happens on that sheet.