Page 38 - Excel for Scientists and Engineers: Numerical Methods
P. 38
Chapter 2
Fundamentals of
Programming with VBA
This chapter provides an overview of Excel's VBA programming language.
Because of the specialized nature of the programming in this book, the material
is organized in a way that is different from other books on the subject. This
book deals almost exclusively with creating custom or user-defined functions,
and a significant fraction of VBA's keywords cannot be used in custom
functions. (For example, custom functions can't open or close workbooks, print
documents, sort lists on worksheets, etc. -these are actions that are performed
by command macros.) Therefore, that portion of the VBA language that can be
used in custom functions is introduced in the first part of this chapter, and
programming concepts that are applicable in command macros appear in the
latter part of the chapter.
If you are familiar with programming in other versions of BASIC or in
FORTRAN, many of the programming techniques described in this chapter will
be familiar.
Components of Visual Basic Statements
VBA macro code consists of statements. Statements are constructed by
using VBA commands, operators, variables, functions, objects, properties,
methods, or other VBA keywords. (VBA Help refers to keywords such as Loop
or Exit as statements, but here they'll be referred to as commands, and we'll use
"statement" in a general way to refer to a line of VBA code.)
Much of the VBA code that you will create will consist of assignment
statements. An assignment statement assigns the result of an expression to a
variable or object; the form of an assignment statement is
variable = expression
for example,
increment = 0.00000001*XValue
or
15