Microsoft Office Tutorials and References
In Depth Information
Chapter 8: VBA Programming Fundamentals
Total = Total + i
Next i
MsgBox Total
End Sub
This procedure uses some common VBA language elements, including:
h A comment (the line that begins with an apostrophe)
h A variable declaration statement (the line that begins with Dim )
h Two variables ( Total and i )
h Two assignment statements ( Total = 0 and Total = Total + i )
h A looping structure ( For-Next )
h A VBA function ( MsgBox )
All these language elements are discussed in subsequent sections of this chapter.
VBA procedures need not manipulate any objects. The preceding procedure, for
example, doesn’t do anything with objects. It simply works with numbers.
Entering VBA code
VBA code, which resides in a VBA module, consists of instructions. The accepted practice is to
use one instruction per line. This standard isn’t a requirement, however; you can use a colon to
separate multiple instructions on a single line. The following example combines four instructions
on one line:
Sub OneLine()
x= 1: y= 2: z= 3: MsgBox x + y + z
End Sub
Most programmers agree that code is easier to read if you use one instruction per line:
Sub MultipleLines()
x = 1
y = 2
z = 3
MsgBox x + y + z
End Sub
Search JabSto ::

Custom Search