The Visual Basic Editor in Excel

Download the Sample Macro Workbook

Get the macro-enabled workbook from the video in a zip file. Open it, press Alt + F11 and explore the recorded macros in the Visual Basic Editor.

How to Open the Visual Basic Editor in Excel

  • Shortcut key (ALT+F11)
  • View tab
    • Under the View tab, select Macros > View Macros
    • Select a macro name and click Edit
Macro dialog box with the Edit button highlighted
  • Developer tab
    • If not yet shown, right click on the Ribbon and Customize the Ribbon. Put a check mark for Developer
    • Under the Developer tab > Macros > Edit
    • Or click Visual Basic on the Developer tab
Visual Basic button on the Developer tab
  • Right click on the spreadsheet > View Code

New to macros? Start with how to record your first Excel macro.

The Parts of the Visual Basic Editor

Visual Basic Editor with its main parts labeled

1 – Switch to Excel (Alternative: ALT + F11)

2 – Project explorer: outline of all open projects, workbooks and worksheets and the objects they contain.

  • PERSONAL.XLSB: Personal Macro workbook. Any macros saved in this VBAProject will be available to all workbooks

3 – Insert menu bar: insert procedures, modules, and userforms (Alternative: right click > Insert module)

4 – Code editor window: Shows the code of the selected module. Code is written in Sub procedures

5 – Switch views

  • Full module view: include all codes in a particular module
  • Procedure view: view code of a selected Sub and exclude any codes nested below

6 – Edit Toolbar: editing options. Activated from View > Toolbars > Edit.

7 – Properties Window: Properties of the items found in the Project Explorer (modules, sheets, etc). Activated from View > Properties Window

8 – Immediate Window: Used for code testing. Activated from View > Immediate Window.

9 – Run / Stop code during testing: Reduces the need of going back to Excel to run the code.

Modules vs Sheet and Workbook Objects

In the VBE, macros that apply to the entire workbook are written and saved in modules.

Code that is specific to a sheet is written and saved in its own sheet.

Generally, the latter is used for special worksheet events which trigger certain actions (e.g. selection change, table update, etc).

To edit a macro, select the module in the Project Explorer.

Double clicking on another object or module on the Project Explorer allows you to shift views.

The active object is highlighted in gray.

Project Explorer with the ThisWorkbook object selected

When opening a workbook object, select Workbook under the first dropdown on the Code Window.

On the right, you will see a list of events that apply.

For example, if you want a certain macro to run when the workbook opens, select the Open event and write code under that Sub.

Workbook Open event selected in the code window dropdown

While additional macros and code can be written on the “ThisWorkbook” object, it is recommended to restrict it to the events that are available in the object, and write all general procedures inside a separate module.

How to Insert a Module

While the macro recorder automatically inserts a new module, there are two ways to do this manually:

  1. Click on Insert > Module.
Insert menu in the VBE with Module selected

2. Right-click inside the Project Explorer and choose Insert > Module

Right-click menu in the Project Explorer with Insert Module

Switch Between Full Module and Procedure View

Views can be changed by clicking on either the Full module view or Procedure view at the bottom left corner of the Code Window.

The latter will show you the Sub procedure you are currently working on.

This can also be done by selecting from the event dropdown on the upper right corner of the Code Window.

Rename Modules in the Properties Window

Select the object or module in the Project Explorer Window.

The Properties window shows the properties of the selected item.

To change the name of a module, click on the text field beside the (Name) and specify the name.

Properties window with the module name field highlighted

Changing the name of a sheet on Excel will automatically update the name of the sheet on the VBE side.

Turn On the Edit Toolbar

To activate this, go to View > Toolbars > Edit.

View menu with Toolbars and Edit selected

Sub Procedures vs Function Procedures

  1. Sub procedure

A set of commands the code should execute and is enclosed by the Sub and End Sub statements. Writing a custom VBA code includes creating your own modules. Each module can be composed of multiple codes and macros.

TIP: Work with fewer modules by putting multiple Subs in each module.

  1. Function Procedure

Code that creates your own formulas. A function returns one value or an array of values. This can be used as “normal” formulas in Excel or inside other procedures.

VBA Color Codes and Formatting

  • Blue: Code keywords (capitalized)
  • Red: Wrong use of code
  • Green: Comments

Add Comments with an Apostrophe

Comments can be added anywhere in the code space for ease of navigation as long as it starts with an apostrophe (‘).

These are commonly used to insert brief descriptions of the code for reference.

Split a Long Line with Space and Underscore

For readability, a single long line of code can be split into two lines by adding a space and underscore (_) in the middle of a line of code and hitting Enter.

Recorded macro with a long line split using space and underscore

Indent Your Code

VBE automatically adds an indent for every level to keep things organized.

This can be done manually by pressing Tab, and can be undone by pressing SHIFT + Tab.

This can be applied to multiple lines of code by highlighting them and pressing Tab.

The Indent and the Outdent icons on the Edit Option Toolbar can also be used.

Useful VBE Options

Turn Auto Syntax Check On or Off

This is useful in alerting you with errors in your code through a popup.

To enable this, go to Tools > Options. Tick the box for Auto Syntax Check.

Tools menu in the VBE with Options selected
VBE Options dialog with Auto Syntax Check highlighted

Require Variable Declaration (Option Explicit)

Helpful when creating your own procedures or modifying codes.

This automatically puts Option Explicit on top of all the modules created.

To activate this, go to Tools > Options.

Tick the box for Require Variable Declaration.

Auto List Members

Aids in having the correct name references in the code by providing a list of members found in the module.

There are three ways to activate this:

  1. Go to Tools > Options. Tick the box for Auto List Members.
  2. Under the Edit options toolbar, select the first icon
  3. CTRL+J

Complete a Word with Ctrl + Space

It helps you complete a word in the code by giving you a list of options.

This is enabled through the Edit Options toolbar or by pressing CTRL + Space.

How to Read a Recorded Macro

Comment Out Code to Disable It

When testing the code, there might be some lines that need to be disabled.

To do this in a quick manner, highlight the lines and click on Comment Block in the Edit Options Toolbar.

This converts those lines into a block of comments, indicated by the single quotation mark at the start and green color.

Selected lines of code ready to comment out with Comment Block

This can be switched back by clicking on the icon for Uncomment Block on the Edit Options Toolbar.

Remove Select Statements

This is automatically added to the lines of code when the macro recorder is used.

The Select statement is usually not necessary and can be removed to improve readability.

This is because with VBA, objects don’t need to be selected to manipulate them as long as they are referred to.

The Equal Sign Assigns Values

This is usually used as a sign of equality between sides, but for VBA, its main job is to assign the value on the right to whatever is on the left.

How the Macro Recorder Refers to Cells

VBA refers to cells in different ways.

ActiveCell refers to the cell that is currently active or is currently clicked on.

When referencing to a range, it uses syntax that is similar to Excel formulas (e.g. Range("A1:E1")).

To see a full recorded macro in action, try this macro to copy data from one sheet to another.

Download the Sample Workbook

Excel blocks macros in files downloaded from the internet. If you see a red security bar, close the file, right-click it in File Explorer, choose Properties, check Unblock and click OK. Then open it again.

Get the Sample Workbook

The macro workbook from the video, ready to open in the VBE.

Frequently Asked Questions

How do I open the Visual Basic Editor in Excel?

Press Alt + F11. You can also go to the Developer tab and click Visual Basic, or go to View, Macros, View Macros, select a macro and click Edit. Right-clicking a sheet tab and choosing View Code opens the code for that sheet.

How do I add the Developer tab in Excel?

Right-click anywhere on the ribbon and choose Customize the Ribbon. Check the Developer box on the right and click OK. The tab then shows the Visual Basic, Macros and Record Macro buttons.

Where are recorded macros stored in Excel?

Macros saved in This Workbook are stored in modules such as Module1 inside that workbook. Macros saved in the Personal Macro Workbook go into PERSONAL.XLSB, which Excel opens in the background so the macros work in every workbook.

What is the difference between a module and a sheet object in VBA?

A module holds general code that you can run from anywhere in the workbook. Sheet objects and ThisWorkbook hold event code, for example a macro that runs when a cell changes or when the workbook opens.

What does Option Explicit do in VBA?

Option Explicit forces you to declare every variable before you use it, which catches typos in variable names. Turn on Require Variable Declaration under Tools, Options to add it to every new module.

Why are the macros blocked in a downloaded workbook?

Excel blocks macros in files from the internet. Close the file, right-click it in File Explorer, choose Properties, check Unblock and click OK. Then open the workbook again and enable the content.

Leila Gharani

Founder of XelPlus and ten-time Microsoft MVP. Leila helps over 500,000 professionals master Excel, Power BI, and data automation through practical, real-world training.