This page provides a very brief overview of the Excel Visual Basic Editor. Further resources providing more detailed descriptions are highlighted throughout the page.
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).
The Project Window opens up on the left of the VBA Editor (as shown in the above image). This window provides you with a VBA Project for each currently open Excel Workbook.
A VBA Project is a collection of all the VBA objects and modules that are linked to the current workbook, and will initially consist of:
You can also add additional Userform, Module and Class Module objects into the project (the Visual Basic Editor in the above image has had a Module (named Module1) added to the VBA project for Book1.xlsm).
To create a new Userform, Module or Class Module:
Each of the objects discussed above has a code window associated with it, which should be used to store new VBA code as follows:
Double clicking on any of the objects within the Project Window will open up the associated Code Window, which is where you can type your VBA code. In the Visual Basic Editor image above, the code window for the Module 1 is displayed.
As you type your VBA code directly into the code window, the VBA editor helps you to produce valid VBA code by highlighting code that is invalid.
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).
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 acting as an output window for debug statements or by allowing you to evaluate expressions or execute individual lines of code.
For example, the expression, ?j followed by the return key will print out the current value of the variable j.
A useful article on the Immediate Window is provided at excelcampus.com
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 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:
Further information on the VBA editor can be found at excel-vba.com.