Skip to the stories
Excel & Power BI

Excel VBA Editor Guide: Windows, Modules, Macros and .xlsm Files

Find your way around the VBA Editor, organize code in modules, run a sample macro and save it in a format that retains VBA.

Excel VBA Editor Guide: Windows, Modules, Macros and .xlsm Files

Before an Excel macro can automate a task, someone has to put the code in the right place, run it and save the workbook in a format that keeps it. The Visual Basic Editor (VBE) is where those steps happen. A quick tour of its windows and project structure makes it easier to work with VBA code, whether you write it yourself or use code drafted with an AI assistant.

Open the Visual Basic Editor

Press Alt + F11 to switch from Excel to the VBE. Press the shortcut again to return to the workbook. You can also open the editor from Excel’s Developer tab by selecting Visual Basic. If the tab isn’t shown, follow these steps to enable the Developer tab.

The Visual Basic button in the Developer tab of Excel

The VBE opens in a separate window. Its layout can include several panes, and four are especially useful when starting out:

  • Project Explorer lists open workbook projects, their worksheets and modules. Open it with Ctrl + R.
  • Code window is where you view and edit code. Press F7 to show it.
  • Properties window displays settings for the selected item, such as a module’s name. Press F4.
  • Immediate window lets you enter a line of code for a quick check or view a result. Press Ctrl + G.
The VBA editor and its four windows: Project Explorer, Code, Properties and Immediate

If a pane is closed, restore it from the VBE’s View menu or use its keyboard shortcut. Knowing these names helps when you need to locate a workbook, edit a procedure or inspect a selected module.

image excel exercises

Put general macros in a module

In the Project Explorer, each open workbook appears as a VBA project. A standard module is the usual home for a macro. To create one, select Insert and then Module. The project gains a new item, usually named Module1, and its code window opens ready for editing.

Workbooks also contain code locations for individual sheets, such as Sheet1, and for the workbook itself, ThisWorkbook. Those locations are commonly used for event code that runs in response to an action, such as opening the workbook or changing a cell. For ordinary macros, a standard module is the sensible place to begin; event code can wait until you need that behavior.

Give modules descriptive names

Default names such as Module1 and Module2 don’t explain what the code does. Names like mdlImport, mdlCopy or mdlSendMail make a project easier to navigate as it grows. The mdl prefix is a common convention that signals the item is a module.

image excel quizzes
  1. Select the module in Project Explorer, such as Module1.
  2. Show the Properties window with F4 if it isn’t already visible.
  3. In the (Name) field, enter a descriptive name such as mdlImport, then press Enter.

Module names can’t contain spaces or begin with a number. Keep names concise and tied to the code’s purpose so they remain useful when you revisit the project.

Rename a VBA module in the Properties window: Module1 becomes mdlImport

A short project may need only one module. As you add more procedures, splitting code by subject can make the workbook easier to maintain. For example, separate modules named mdlImport, mdlReport and mdlTools show where to look for related tasks. Open a module to see its procedures; the procedure list at the top right of the code window lets you move between them.

Project Explorer with three renamed modules: mdlImport, mdlReport and mdlTools

Run a simple macro

This example writes My first macro into cell A1 of the active worksheet and formats the cell’s contents in bold. Add it to a standard module:

Sub FirstMacro()
 Range("A1").Value = "My first macro"
 Range("A1").Font.Bold = True
End Sub

To run the procedure from the VBE, place the cursor anywhere between Sub and End Sub, then press F5. From Excel, press Alt + F8, select FirstMacro from the list and click Run. When you return to the worksheet, A1 should display the text in bold.

This small example also shows the basic cycle: put a procedure in a module, run it, then check the result in Excel. Macros can change workbook contents, so make sure you understand what a procedure does before running it on important data.

Save VBA code in a macro-enabled workbook

A standard .xlsx workbook doesn’t retain VBA macros. When saving a workbook that contains code, choose Excel Macro-Enabled Workbook (*.xlsm). Saving it as .xlsx removes the VBA code, so check the selected file type before you close the workbook.

Save a workbook in the .xlsm format

When you reopen the file, Excel may show an Enable Content prompt before allowing the macros to run. Follow your organization’s guidance when deciding whether to enable macros in a workbook. For background on blocked macros, see Microsoft’s macro blocking guidance.

What to learn next

With the editor’s main panes, module locations, naming and run options in hand, the next concepts to learn are VBA procedures and functions. They are the building blocks used to organize code and return results. See the guides to a VBA procedure and a VBA function, or continue with What Is a Program in VBA?. Regular practice with Excel exercises can help reinforce the steps.

Source: Frédéric LE GUEN · excel-tutorial.com

Same flag