Microsoft Office Excel 2003 Programming Inside Out (Inside Out (Microsoft))

 < Day Day Up > 


Keeping code organized and easy to find is just as important within an Excel project as keeping the files located on your hard drive organized. Fortunately, the Visual Basic Editor Project Explorer presents the elements of a project in the same tree view that is used by Windows Explorer. The Project Explorer is not as flexible as Windows Explorer, but it does keep similar elements of a project grouped together within a common folder. This familiar organization makes it easier to find a particular module, class module, user form, or other Excel object when needed.

Handling Windows

Each individual module, class module, or user form will be displayed within its own code window, which means that as you add more modules, the number of windows needed to work with them will grow. Regardless of the number of modules a project contains, you do not need to open them all to be able to work with the project. Only the module or user form you want to edit needs to be opened.

To assist you with navigating the various windows, the Visual Basic Editor provides some tools to open, switch, and close windows. Opening a module can be accomplished by double-clicking the module from within the Project Explorer window. You can also use this method to switch to a module if it has already been opened.

To switch among the various open windows, you can use the Window menu to select the desired window. You can also scroll forward through the various windows by pressing Ctrl+F6 or backward by pressing Shift+Ctrl+F6. When you’re done working with the contents of a window, you can close it by clicking the Close Window button or pressing Ctrl+F4.

Each of the windows can also be minimized or resized within the parent Visual Basic Editor window, allowing you to view more than one module window at a time. The Window menu provides commands to tile the open windows horizontally or vertically or to cascade them. If you need to view two different sections of the same module, you can split the module window by using the split window handle or Split command from the Window menu.

Adding Code

Adding code to a project can be done several ways, but the first consideration is to have a container to hold the code, such as a code module, a class module, or a user form. The container you use depends upon the tasks that will be performed by the code you intend to add. Although it is possible to keep all of your code within one module, it is best to keep your code separated into different modules grouped by tasks. To add a new module, select the type of module (module, class module, or user form) needed from the Insert menu or the Insert button on the Standard toolbar.

To add a new procedure to a module or class module, you can type directly into the code window of the module. You can also click Insert, Procedure to open the Add Procedure dialog box, shown in Figure 4-8, which will prompt you for the minimum information needed to create a procedure.

Figure 4-8: The Add Procedure dialog box lets you determine the basic outline of your procedure.

Note 

You can find more information on the options available in the Add Procedure dialog box later in this chapter.

Notes on Programming Style and Code Readability

Programmers come from many different backgrounds, and each has their own style of writing code. Programmers who work for or have worked for large software companies might follow a strict format specified by the company, while solo programmers might follow no set format. While neither the company nor the solo programmer is necessarily right or wrong, it is absolutely vital that you write code that is easy to read and understand. Even if you understand the code you write today, you or another programmer might need to modify it in the future. Unless you have a photographic memory, you might have difficulties understanding the logic you wrote earlier and find it that much harder to make your changes.

There are several steps you can take to make your code as readable as possible.


 < Day Day Up > 

Категории