Writing Structured VBA Programs

In This Chapter

^ Discovering the structure used by most programs ^ Using structure to your advantage ^ Creating programs by using the Macro Recorder ^ Creating a program by using a Sub ^ Creating a program by using a Function ^ Hiding some program elements by using scope ^ Adding white space to programs ^ Adding comments to your code

■ n Chapters 1 and 2, I concentrate on describing the basics of VBA programming without discussing an important element that VBA programs need — structure. Adding structure makes code easier to read and use. It's also a mandatory part of the development process.

There are a number of ways to look at structure in a program. Just as you use an outline to structure a presentation, you need to add structure to your program to make it work properly. Using structure in a program is like using a checklist with a large customer order — it helps you ensure that all the program elements are in place. The structure that you add to a program is like presenting numeric information with a graph — it makes the content of the program easier to see and understand.

In this chapter, I present various forms of structure. The obvious structuring element is physical. By using physical structure, you can divide your program into small pieces that are easy to write and understand. Many people divide programs into task-oriented pieces. It helps to use the Macro Recorder to see how you can create small tasks out of larger procedures, so this chapter discusses how to use the Macro Recorder to discover more about your specific program needs.

Another form of structure includes the concept of privacy. Consider who can see your program and how they can use it. Scope, which is the act of determining the range of program access, is important because you want to make some parts completely private and other parts completely public.

Finally, there are visual elements of structure. How you use white space when you write your program can make the difference between reading it and scratching your head. In Chapter 2, I discuss the use of comments in the form of pseudo-code, but you might find that you need additional comments to help someone truly understand your program (and to jog your memory when you need to make alterations).

0 0

Post a comment