Microsoft Office Tutorials and References
In Depth Information
Chapter 2: Writing Code
Chapter 2: Writing Code
In This Chapter
Understanding VBA and its syntax
Writing custom VBA procedures
Entering and editing code in the Code window
Putting your custom procedures to the test
Writing Visual Basic for Applications (VBA) code is different from writing
in English or any other human language. When you write in English,
presumably you’re writing for other human beings who speak the same
language. Even if your English isn’t so great (bad spelling, poor grammar), your
recipients probably can still figure out what your message means. Humans
have flexible brains that can figure things out based on context.
That’s not so for computers. Computers don’t have brains and can’t figure
out anything based on context. When you write code, a computer does
exactly what the code tells it to do. If the computer can’t read and process
a statement, the procedure stops running, and an error message appears
onscreen.
Before you start writing custom code, you need to know about syntax. You
need to know what resources are available for finding the syntax for the
tasks you want to program. Finally, you need to know at least some basic
techniques for testing your code to see whether it’s going to work — before
you try putting it to use.
Seeing How VBA Works
VBA code is organized in procedures. Each procedure contains any number
of lines of code called statements. Each statement instructs VBA to perform
some action. The procedure sits in its module, doing nothing, until some
event calls the procedure.
When a procedure is called, statements execute (run) one at a time. VBA fully
executes the first statement and then fully executes the second statement,
and so on, until it reaches the End Sub or End Function statement, which
marks the end of the procedure. At that point, the code stops executing.
Figure 2-1 summarizes how procedures work.
Search JabSto ::




Custom Search