In this part . . .
E
very adventure begins with a discovery phase. This
part of the book is all about discovery. You consider
all the elements that define VBA as a language. Good
structure is the basis of most projects in life. When you
build a house, you create the basic structure first and
then add on to it. Chapter 3 is all about VBA program
structure. You discover how to create a framework for the
programs that you build later.
In Chapter 4, I help you understand
how to manage data
by using VBA. Data management is the single most impor-
tant skill that a programmer can learn. This chapter also
provides useful information about questions that VBA pro-
grammers ask most often, such as when to use + or & to
concatenate strings (add them together).
In Chapter 5, I tell you how to control program flow by
using several techniques. Controlling
program flow lets
you do things like make decisions. In Chapter 6, I show
you techniques for debugging your program (removing
errors). Debugging is an essential part of creating great
applications. In Chapter 7, I help you discover the joys of
creating a user interface.
07_046500 pt02.qxp 12/5/06 5:34 PM Page 50
Chapter 3
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
I
n Chapters 1 and 2, I concentrate on describing the basics of VBA pro-
gramming 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 pro-
gram 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 pro-
gram 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 pro-
gram 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 chap-
ter 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 deter-
mining the range of program access, is important because you want to make
some parts completely private and other parts completely public.
08_046500 ch03.qxp 12/5/06 5:34 PM Page 51
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).
Do'stlaringiz bilan baham: