Understanding and Using the Visual Basic Editor






Understanding and Using the Visual Basic Editor

Before you start working with VBA code, take a few minutes to settle in to your surroundings. To help you work more comfortably, the subsections that follow tell you a bit about each of the components of the Visual Basic Editor that are identified in the following diagram.

Image from book

The Code Window

The Code window is where your procedures appear. This is where you type macros when writing code and where you find the macros you’ve recorded. Notice that the Procedure list is at the top right of the Code window. From this list, you can quickly move to any procedure in the active module.

Also notice the view options at the bottom of the screen. When you have several macros in a module, it can be helpful to view them one at a time. Full Module view is the default, but you can change this setting and many others through the Options dialog box, discussed in “Setting Up Your Workspace” on page 1085.

Project Explorer

Project Explorer is where you see the list of all VBA projects that are currently open or loaded. All open documents, as well as open or loaded document templates, appear here, whether or not they contain macros. You can collapse or expand a project to view the modules and objects that it contains.

Note 

Documents appear in this list whether or not they’re macro-enabled file formats. This is important to keep in mind because, if you add code to a document using an Open XML Format that ends with the letter x, you won’t be able to save the document with its code. Save the document with the equivalent file format that ends in the letter m to enable your code to be saved along with the document or template.

  • A project only has a Modules or Forms folder if it contains code modules or UserForms. However, in Word and Excel, each project contains an Objects folder, such as the Microsoft Word Objects folder you see under each of the projects visible in the preceding image.

    In Word, the Objects folder contains a document object referred to as ThisDocument. In Excel, it contains both a ThisWorkbook object and a sheet object for each existing sheet in the workbook. Some types of code (such as a type of procedure known as a document-level event) are added directly in the Code window for the document object rather than in a module. However, you will often have projects that have no code added to the document objects. Learn more about using the document objects in “Introduction to Using Events” on page 1131.

The Properties Window

The Properties window shown in the preceding image doesn’t look like much, but don’t be fooled. For modules, the Properties window is generally used only to edit the module name. However, for some object types (such as UserForms), the Properties window becomes extremely important because it’s populated with many settings that you can edit directly in that window, ranging from the height and width of a UserForm to the value to display on a form control (such as a text box or an option button).

To edit the name of a module in the Properties window, click into the name where it appears on either the Alphabetic or Categorized tabs, edit it as you would document text, and then press Enter to set it. Module naming rules are the same as macro naming rules-no spaces or special characters, and the name must begin with a letter.

Note 

All names in VBA subscribe to a similar set of rules. Names must always start with a letter, and can’t include spaces or most special characters. Most names are limited to 255 characters in length. However, module names can’t exceed 31 characters, and macro names added in the Record Macro dialog box are limited to 80 characters.

Note that, when you record macros, they’re always added to a module named NewMacros. You can rename that module if you like, but the next time you record a macro, a new module will be created with the name NewMacros.

Setting Up Your Workspace

You’ll find many settings that can be customized in the Options dialog box, available on the Tools menu in the Visual Basic Editor. I don’t recommend spending much time in this dialog box just yet, because you might not be familiar with many of the settings. But, it’s good to know that it’s there, because you will need it. I’ll point out, throughout this primer, when settings can be customized in this dialog box.

Possible settings in the Options dialog box include default behavior for a number of programming actions (such as the way you’re notified about errors in your code), the formatting for each type of text or notification you see in the Code window (such as comment text or errors), and the way the window itself is arranged.

In addition to settings in the Options dialog box, notice that you can drag to resize docked panes in the Visual Basic Editor window (such as the Project Explorer or Properties window), or close those you don’t need. Use the View menu to access any windows you’ve closed. If you’re unable to dock any window in the Visual Basic Editor, you can change the setting for that window on the Docking tab of the Options dialog box.



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows