ExcelFunctions.net Logo

The Excel Visual Basic Editor

Home » Writing-Excel-Macros » Visual-Basic-Editor


Search this site:
Custom Search


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
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
The Code Window show the VBA code for each individual worksheet object or module. You can open the code window for an existing worksheet object or module by double-clicking on the name in the Project Window. (In the image above, the worksheet object for Sheet1 of the workbook is displayed, although it contains no VBA code at present).

Alternatively, to create a new module:
  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
Once a worksheet object or module is opened up, you can type your VBA code directly into the code window. This window assists you with typing in valid VBA code by highlighting lines that do not adhere to the VBA code rules, as you type. Whenever you save any changes in the Visual Basic Editor, this saves your whole Excel workbook.


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
The Immediate 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
The Locals Window displays all local variables that are declared in the current procedure. The window is split into columns which show the name, value and type of each variable. These are updated automatically as the programme is executed. This window is therefore useful to assist with debugging.


The Watch Window
The Watch Window is also useful to assist with debugging procedures. This window shows the value, type and context of any watch expressions that have been defined by the user. It will appear automatically when a watch expression is defined, or it can selected from the View window.

A 'watch' expression can be defined by the following steps :
  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 on this site (opens in a new window).





Disclaimer Privacy Policy

Copyright © 2008-2010 ExcelFunctions.net