VBA statements
Procedure
Sub routine |
SUB SubName (ParameterList) ... END SUB |
Function |
FUNCTION FunctionName (ParameterList) AS type … END FUNCTION |
Scope |
PUBLIC PRIVATE |
General
Line continuation character |
· _ (underscore) |
Comments |
· ‘ (single quote) or · REM … |
Variable names |
· Not a keyword · <= 255 characters · First letter must be a letter · Cannot contain a period, space, !, @, #, &, %, or $ |
Variable declaration |
·
DIM |
Variable scope |
· PRIVATE VariableName AS type ·
PUBLIC |
Static variables |
·
STATIC |
Object variables |
·
DIM e.g., DIM frmAny AS form |
Create new objects |
· SET ObjectVariable = NEW ClassName |
Frees up memory associated with objects |
· SET ObjectVariable = Nothing |
Arrays |
· DIM ArrayName(size) AS type · DIM ArrayName(1 TO #) AS type |
Constants
Symbolic constant |
· CONST constant_name = value [AS type] e.g., CONST
path = “d:\” |
Operators
Assignment operator |
· VariableName = expression · SET Obj1 = Obj2 |
Mathematical |
· + · - · * · / · \ (integer division with rounding) · ^ · mod (remainder after division) |
String |
· & (concatenation) |
Comparison |
· = · > · < · >= · <= · <> |
Logical |
· And · Or · Not |
Conditional control
If … Then … Else |
IF condition THEN statements executed under true condition [ELSE statements executed under false condition END IF] |
Select Case |
SELECT CASE test-expression CASE expression-list-1 statement-block-1 … CASE expression-list-n statement-block-n END SELECT |
For … Next |
FOR counter = start TO end [STEP increment] statements NEXT |
For Each … Next (a collection of objects) |
FOR EACH item in collection statements NEXT item |
With … End With (a single object) |
WITH object statements accessing the properties/methods of the object END WITH |
Do While … |
DO WHILE condition statements executed as long as the specified condition is true |
Do Until … |
DO UNTIL condition statements executed until the specified condition is true |
Do … Loop |
DO statements LOOP WHILE condition or DO statements LOOP UNTIL condition |
Unconditional control
GoTo |
GOTO label . . label: |
Exit a procedure |
EXIT |
Exit a For loop |
EXIT FOR |
Exit a Do loop |
EXIT DO |