Introducing Visual Basic For Applications
Introducing Visual Basic For Applications
Visual Basic for Applications or VBA is a development environment built into the
Microsoft Office Suite of products.
When working in VBA tell Excel exactly what to do. Don’t assume
anything.
All VBA sentences must be on a single line. When you need to write long
sentences of code and you want to force a line break to make it easier to read
you must add a space and an underscore at the end of each line and then
press Return. Here is an example of a single sentence broken into 3 lines:
Statements
A statement is a complete unit of execution that results in an action,
declaration
or definition.
Statements are entered one per line and cannot span more than one line
unless
the line continuation character ( _ ) is used.
Statements combine the language’s key words with expressions to get things
done.
Below are some examples of statements:
Naming Variables
To declare a variable you give it a name. Visual Basic associates the name
with a location in memory where the variable is stored.
Variable names have the following limitations:
Must start with a letter
Must NOT have spaces
May include letters, numbers and underscore characters
Must not exceed 255 characters in length
Must not be a reserved word like True, Range, Selection
Assigning Values To Variables
An Assignment statement is used to set the value of a variable. The variable
name is placed to the left of the equal sign, while the right side of the
statement
can be any expression that evaluates to the appropriate data type.
The syntax for a Variable declaration is as follows:
VariableName = expression
StdCounter = StdCounter + 1
SalesTotal = SalesTotal + ActiveCell.Value