The Excel Visual Basic Editor

The easiest way to access the Visual Basic Editor in Excel is to press the key combination ALT-F11 (ie. press the ALT key, and while this is pressed down, press F11). You will be presented with the Visual Basic Editor, similar to the image below (note that your normal Excel window will remain open behind this window).

Excel Visual Basic Editor

There are a number of windows that can be viewed in the Excel VBA Editor. These are controlled via the View menu at the top of the VBA Editor window. The individual windows are described below.


The Project Window

Empty VBA Editor Project Window

The Project Window opens up on the left of the VBA Editor (as shown in the above image). This window provides you with an index of all Visual Basic files, linked to all open Excel workbooks. These consist of :

A file (or a worksheet object), linked to each worksheet of your Excel workbook
    and

Additional files (called Modules), which contain user-defined code and are not linked to any specific worksheet

If you want to write a macro that is to be linked to a specific worksheet, then this code should be entered into the relevant worksheet object. If your code is more general, you may want to create a new module to hold this.


The Code Windows

In the Visual Basic Editor image above, the code window for the Worksheet, Sheet1, is displayed (although it contains no VBA code at present).

The Code Window contains the VBA code for each individual worksheet object or module. Once a worksheet object or module is opened up, you can type your VBA code directly into the code window and the VBA editor assists you with typing in valid VBA code by highlighting lines that do not adhere to the VBA code rules, as you type.

The code window for an existing worksheet object or module is opened up by double-clicking on the worksheet or module name in the Project Window. Alternatively, you can create a new module as follows:

  1. If you have more than one Excel workbook open, in the 'Project Window', select the workbook you want to add a module to
  2. Right click in the 'Project Window' and select Insert->Module
Empty VBA Editor Properties Window

The Properties Window

The Properties Window lists the properties of the object that is selected in the Project Window at design time (ie. not during run time). These properties vary, depending on the type of object that is selected (a worksheet, workbook, module, etc).


The Immediate Window

View the Immediate Window by selecting View→Immediate Window from the Visual Basic Editor, or by pressing CTRL-G. This window assists with the debugging of code by allowing you to execute individual lines of code. This is done by typing in an individual line code and then pressing enter to execute it.


The Locals Window

View the Locals Window by selecting View→Locals Window from the Visual Basic Editor. This window displays all local variables that are declared in the current procedure. It is split into columns which show the name, value and type of each variable and updates these values automatically, as the programme is executed. The Locals window is therefore useful for debugging VBA code.


The Watch Window

The Watch Window is also useful when debugging VBA code, as it shows the value, type and context of any watch expressions that have been defined by the user.

The Watch Window can opened by selecting View→Watch Window from the Visual Basic Editor, or the window appears automatically when a 'watch' expression is defined. to define a 'watch' expression:

  1. Highlight an expression within the VBA code
  2. From the Debug menu at the top of the VBA editor, select the option Quick Watch...
  3. Click on Add

The menus at the top of the Excel Visual Basic Editor contain numerous options and commands for designing, executing and debugging VBA code. Further information on the VBA editor can be found at excel-vba.com (opens in a new window).

Return to the Writing Excel Macros page

Return to the ExcelFunctions.net Home Page

Valid XHTML 1.0 Transitional Valid CSS!
Disclaimer   Privacy Policy
Copyright © 2008-2013 ExcelFunctions.net