To add VBA code to Excel, open the workbook, press Alt+F11 (Windows) or Fn+Option+F11 (Mac) to open the Visual Basic Editor, insert a module, paste your code into it, then save the file as an .xlsm macro-enabled workbook. That is the short version. The steps below walk through each part carefully, because a single missed save setting is the most common reason code appears to vanish.
What You'll Need
- Excel for Windows or Excel for Mac (the desktop app, not Excel for the web, which cannot run VBA).
- A workbook you are allowed to modify, ideally a copy while you are learning.
- Your VBA code in plain text, ready to paste.
- Macro security settings that allow macros to run. You may need to enable macros for the file or adjust the Trust Center, which is covered below.
Step-by-Step
- Open the workbook in desktop Excel. VBA is not available in Excel for the web or in the mobile apps. Use the installed Windows or Mac version. If you only have the web version, you cannot add or run VBA code in that file.
- Open the Visual Basic Editor. On Windows, press Alt+F11. On Mac, press Fn+Option+F11, or use the Developer tab if it is visible. If you do not see a Developer tab, right-click the ribbon, choose Customize the Ribbon, and tick Developer in the list on the right. Menu and tab names can differ slightly by Excel version and region.
- Find your workbook in the Project Explorer. The Project Explorer is usually on the left of the editor. You will see entries like VBAProject (YourFileName.xlsx). If the panel is hidden, open it from the View menu. This confirms you are editing the right file and not a different open workbook.
- Insert a standard module. In the editor menu, choose Insert > Module. A blank code window opens. A standard module is the right place for general-purpose macros. Use a sheet module only when the code must respond to events on that specific sheet, and ThisWorkbook only for workbook-level events.
- Paste your code into the module. Click in the blank code window and paste. Each macro should begin with Sub Name() and end with End Sub. If you paste code that includes Option Explicit at the top, that is fine; it forces variables to be declared, which is good practice. Do not paste code into the Immediate window or the wrong module, or it will not appear in the macro list.
- Check for obvious errors. In the editor, choose Debug > Compile VBAProject. If Excel reports a compile error, the code has a syntax problem, a missing reference, or a name that clashes. Fix the highlighted line before continuing. Compiling does not run the code; it only checks it.
- Run the macro to test it. Press F5 while the cursor is inside the subroutine, or go back to Excel and use Developer > Macros, select the macro name, and click Run. If the macro asks for input or changes cells, test on a copy of the workbook first.
- Save as a macro-enabled workbook. This is the critical step. Use File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm). A normal .xlsx file strips out the VBA code when saved, even though the code was there while the file was open. If you see a warning that the file contains macros, choose to keep the macro-enabled format.
- Enable macros when you reopen the file. When you open an .xlsm file, Excel may show a security bar or dialog. Choose Enable Content or Enable Macros only if you trust the file and the code. If you built the file yourself, that is expected. If the file came from someone else, inspect the code before enabling anything.
Common Mistakes
- Saving as .xlsx. The code runs during the session but disappears after saving and reopening. Always use .xlsm for files that contain VBA.
- Pasting into the wrong module. Code in a sheet module or ThisWorkbook will not show up in the general macro list unless it is written as a proper macro. Use Insert > Module for ordinary macros.
- Forgetting End Sub. A missing End Sub or a stray character causes a compile error and the macro will not run.
- Blocking macros in Trust Center. If macros are disabled entirely, the code may be present but never execute. Check the Trust Center macro settings and any blocked-file warning.
- Editing the wrong workbook. With several files open, it is easy to paste code into the wrong VBAProject. Check the name in the Project Explorer before pasting.
- Assuming the web or mobile app supports VBA. It does not. If a colleague opens your .xlsm in Excel for the web, macros will not run there.
Tips
- Keep a plain-text copy of your code outside the workbook so you can restore it if a file is corrupted or saved in the wrong format.
- Use Option Explicit at the top of each module. It catches typos in variable names at compile time instead of at run time.
- Name your macros descriptively, such as FormatReport rather than Macro1, so they are easy to find in the Macros dialog.
- Add a comment line at the top of each module with the purpose and any assumptions, so future-you knows what it does.
- Test on a copy of the workbook before running code that deletes rows, overwrites cells, or sends email.
- If you plan to share the file, tell recipients it contains macros and that they will need to enable content. Do not ask them to lower security settings globally.
Troubleshooting
If the macro does not appear in the Macros list, the code is probably in the wrong place or is not written as a Sub. Check that it sits in a standard module and starts with Sub and ends with End Sub. If the list is empty, also make sure you are looking at the right workbook in the Macros dialog.
If you get a compile error, read the highlighted line and check for missing parentheses, unclosed quotes, or a misspelled object name. Use Debug > Compile VBAProject again after each fix. If the error mentions a user-defined type or an object library, the code may depend on a reference that is not set; open Tools > References and look for items marked MISSING.
If the code runs but nothing happens, the macro may be acting on a different sheet or workbook than you expect. Step through it with F8 to see which lines execute and which sheet is active. If it stops at a password prompt or a protected sheet, unprotect the sheet first, or adjust the code to unprotect it.
If macros are blocked, check the yellow security bar and the Trust Center settings. You can also right-click the file in File Explorer, open Properties, and look for an Unblock checkbox on the General tab if the file came from another computer. Only unblock files you trust.
If the code disappears after saving, you almost certainly saved as .xlsx. Reopen the last good copy, paste the code again, and save as .xlsm. If you no longer have the code, check whether a backup copy exists; Excel does not recover VBA from a standard .xlsx file.
If the file opens with a warning about disabled macros and you cannot enable them, your organization may enforce a policy. In that case, contact your IT administrator rather than changing security settings yourself.
Frequently Asked Questions
Can I add VBA code in Excel for the web or on mobile?
No. VBA requires the desktop version of Excel for Windows or Mac. The web and mobile versions can open some macro-enabled files but cannot run or edit VBA code.
Why does my code vanish after I save?
Because the file was saved as .xlsx. Save as Excel Macro-Enabled Workbook (*.xlsm) to keep the code.
Do I need the Developer tab?
No, but it makes running macros easier. You can open the editor with Alt+F11 on Windows or Fn+Option+F11 on Mac without it.
Where should I paste my code?
In a standard module: Insert > Module. Use sheet modules or ThisWorkbook only for event code tied to that sheet or workbook.
Is it safe to enable macros in a file someone sent me?
Only if you trust the sender and have reviewed the code. Macros can change files and access data, so treat unknown macro files like unknown programs.
Can I share an .xlsm file with others?
Yes, but recipients will need to enable macros, and anyone using Excel for the web will not be able to run them. Tell them in advance so the file does not appear broken.