In this topic, I’ll take you on a quick tour of all the IDE components and some of the features they offer—that way you’ll get an idea about how everything fits together.
The VBA IDE contains a collection of powerful commands and tools that you’ll find extremely convenient and helpful for creating VBA macros and applications. In this topic I’ve stuck to the components you’ll find useful for creating your code. Following are the components from the IDE covered in this topic:
• Menu bar
• Toolbars
• Toolbox
• Project Explorer window
• Object Browser window
• Code window
• Properties window
Figure 3.1 illustrates a typical layout of these components in the IDE window. By default, the menu bar is positioned horizontally along the top of the window, immediately below the title bar. The menu bar contains a collection of items, each with its own drop-down list of commands. The standard toolbar typically sits snugly beneath the menu bar and provides fast access to some of the most-used menu commands. When the menu bar and standard toolbar are positioned as in Figure 3.1, they are said to be in their docked positions.
Figure 3.1 VBA IDE menu bar and standard toolbar in their docked positions
The menu bar and toolbars can be positioned anywhere on the screen using a drag-and-drop action. In fact, all the IDE windows can be moved by dragging and dropping their title bars, and they can be resized by adjusting their borders. However, when the menu bar and toolbar windows are docked, their title bars are sometimes hidden, so if you want them repositioned you’ll need to drag them by the two parallel vertical bars on their left-hand sides.
Exploring the Menu Bar
The menu bar has 11 drop-down lists of menu commands. These provide access to all the commands available in the IDE. These Windows-style drop-down menus are what make VBA an easy-to-learn and easy-to-use powerful, visual system that saves you from having to memorize textual commands. Among other things, the commands available from the menu bar allow you to
• Save and print files
• Use the editing features
• Open and close windows in the IDE
• Insert forms and modules into your project
• Line up controls in forms
• Run and debug code
• Change settings that alter the appearance and behavior of the IDE
• Access the online Help facility
If you take a quick look at all the drop-down lists of menu commands, it won’t take long to discover that quite a few of the commands are dimmed (“grayed”) indicating that they are unavailable for selection. This is because they are considered inappropriate for selection in the current context. For example, most of the Edit commands will be dimmed unless there is something to edit, so you’ll need to open a Code window to make them available.
The examples that follow use a few of the most common commands from each list to give you an idea of the power you have at your fingertips while working in the IDE.
Cascading Menus
Menu commands that sport an arrow at the far right are called cascading menus. Pausing the mouse cursor over these commands displays another drop-down list of related submenus.As shown here, several menu commands in the Format group have submenus:
Setting Your IDE Options
The Tools menu commands allow you to access the Visual Basic Editor to create and run macros, set options to customize the features that are available, and set up your project’s properties.
When you select Tools ^ Options, the Options dialog box opens as shown in Figure 3.2. The options you can set from the Editor and General tabs of the Options dialog box are described in the next two sections. You have already seen how to set the color of the Keyword Text in the Editor Format tab.
Figure 3.2 Editor settings in the Options dialog box
Editor Options
The Editor tab (Figure 3.2) contains the following settings that will help you as you enter your code:
Auto Syntax Check When this option is enabled (the check box is checked), the Visual Basic interpreter will examine the code for syntax errors line by line, as it is entered. If an error is found, the interpreter will change the color of the whole line to the color setting for the Syntax Error Text that has been set in the Editor Format tab. In the following example, you can see that the second line is different in appearance:
If any particular part of the line can be identified as causing the error, it is highlighted as shown just above, and a message box is displayed with a brief error message:
Clicking the Help button in this message box calls the VBA online Help facility to give a detailed explanation of the cause of the error:
Require Variable Declaration This option allows you to control whether or not variables must be declared before you can use them in code without getting an error message.
Auto List Members Enabling this option provides a drop-down list of valid items that can be used to complete the code you have entered so far. For example, Figure 3.3 shows the drop-down list that appears after you enter ThisDrawing followed by a full stop character (the period).
Figure 3.3 List of valid code elements that can follow ThisDrawing
Auto Quick Info When this option is turned on, a drop-down list will be displayed if you enter a function name followed by a parenthesis character, as shown in Figure 3.4. The list contains information about the parameters for which you are expected to provide values. The current parameter is always displayed in bold format to keep you informed of how you are progressing through the list.
Figure 3.4 List of parameters required by the AddCircle function
Auto Data Tips Enabling this option tells the IDE to display the value of the variable currently under the mouse cursor when your application has stopped at a breakpoint or because of an error in the code:
A breakpoint is a statement that you have chosen for the interpreter program to stop at, so that (for example) you can find the current values of variables.
Auto Indent When this option is turned on and you indent a line of code using the Tab key, the subsequent lines of code will be lined up with the same indent, like this:
You can also control the number of spaces for the tab, by changing the number in the Editor tab’s Tab Width text box (situated just below the Auto Indent check box).