VBA Compiler Syntax Reference
This document provides a reference for the VBA syntax supported by the XLS Padlock VBA compiler.
Script Structure
Section titled “Script Structure”Script structure is made of function and sub declarations.
SUB DoSomething CallSomethingEND SUB
FUNCTION MyFunction MyFunction = "Ok!"END FUNCTIONStatements in a single line can be separated by the : character.
Comments
Section titled “Comments”Comments can be inserted inside a script. You can use the ' character or REM. The comment will extend to the end of the line.
' This is a comment before ShowMessageShowMessage("Ok")
REM This is another commentShowMessage("More ok!")Identifiers
Section titled “Identifiers”Identifier names (variables, functions, procedures, etc.) must begin with a character (a-z, A-Z) or an underscore _. They can be followed by alphanumeric characters or underscores. Names cannot contain any other character or spaces.
Variables
Section titled “Variables”There is no need to declare variables in a script, but if you want to, you can declare a variable using the DIM directive.
SUB Msg(Param1) DIM S S = "Hello world!" ShowMessage(S)END SUBYou can also declare global variables as PRIVATE or PUBLIC.
PRIVATE APUBLIC BB = 0A = B + 1ShowMessage(A)Variables declared with DIM are public by default. Private variables are not accessible from other scripts. Variables can also be initialized upon declaration:
DIM A = "Hello world"DIM B As Integer = 5Assign Statements
Section titled “Assign Statements”Assign statements use the = operator to assign a value or expression result to a variable or object property.
MyVar = 2Application.Range("C4").Value = "This " + "is ok."Arrays
Section titled “Arrays”The compiler offers basic support for array constructors and variant arrays. To construct an array, use [ and ] characters. You can construct a multi-index array by nesting array constructors.
Arrays in the compiler are 0-based index.
NewArray = [ 2,4,6,8 ]Num = NewArray[1] 'Num receives 4
MultiArray = [ ["green","red","blue"] , ["apple","orange","lemon"] ]Str = MultiArray[0,2] 'Str receives "blue"MultiArray[1,1] = "new orange"Dynamic arrays:
' Create a dynamic arrayDIM PTIM = VarArrayCreate([0,3000,0,5], 12)' Assign a value:PTIM[1,2] = 1530Indexes
Section titled “Indexes”Strings, arrays, and array properties can be indexed using [ and ] characters. For example, if Str is a string variable, the expression Str[3] returns the third character in the string.
MyChar = MyStr[2]MyStr[1] = "A"MyArray[1,2] = 1530Keywords and Operators
Section titled “Keywords and Operators”The Basic syntax supports:
- Declarations:
SUB...END SUB,FUNCTION...END FUNCTION - Directives:
BYREF,DIM - Conditionals:
IF...THEN...ELSE...ELSEIF...END IF,SELECT CASE...END SELECT - Loops:
FOR...TO...STEP...NEXT,DO...WHILE...LOOP,DO...LOOP...WHILE,DO...UNTIL...LOOP,DO...LOOP...UNTIL - Operators:
^,*,/,AND,+,-,OR,<>,>=,<=,=,>,<,DIV,MOD,XOR,SHL,SHR - Error Handling:
TRY...EXCEPT,TRY...FINALLY - Other:
EXIT, array constructors[1, 2, 3], object accessObjectName.Property
If Statements
Section titled “If Statements”There are two forms: IF...THEN...END IF and IF...THEN...ELSE...END IF. If the expression is true, the THEN statements are executed. If it is false, the ELSE statements are executed (if present).
IF J <> 0 THEN Result = I/JEND IF
IF J = 0 THEN ExitELSE Result = I/JEND IFIf the statement is on a single line, you don’t need END IF:
IF J <> 0 THEN Result = I/JSelect Case Statements
Section titled “Select Case Statements”If the selectorExpression matches one of the caseexpr expressions, the respective statements will be executed. Otherwise, the CASE ELSE statement will be executed.
SELECT CASE uppercase(Fruit) CASE "lime" ShowMessage("green") CASE "orange" ShowMessage("orange") CASE ELSE ShowMessage("black")END SELECTFor Statements
Section titled “For Statements”The FOR statement repeats execution of statements until a counter reaches a final value.
FOR counter = initialValue TO finalValue STEP stepValue StatementsNEXTThe STEP part is optional; if omitted, the step value is 1.
FOR c = 1 TO 10 STEP 2 a = a + cNEXTWhile Statements
Section titled “While Statements”A WHILE statement repeats statements as long as a control condition is true. The condition is evaluated before the statements are executed.
WHILE (Data[I] <> X) I = I + 1END WHILEDo...Loop Statements
Section titled “Do...Loop Statements”Statements are executed WHILE an expression is true, or UNTIL an expression is true. The condition can be tested before or after the iteration.
' Condition tested afterDO K = I mod J I = J J = KLOOP UNTIL J = 0
' Condition tested beforeDO WHILE I < 0 ...LOOPFunction and Sub Declarations
Section titled “Function and Sub Declarations”Declarations are similar to standard Basic. To return a value from a function, assign it to the implicit variable with the same name as the function, or use the Return statement. Parameters can be passed by reference using the BYREF directive.
SUB HelloWord ShowMessage("Hello world!")END SUB
FUNCTION Max(A,B) IF A > B THEN MAX = A ELSE MAX = B END IFEND FUNCTION
SUB SwapValues(BYREF A, B) DIM TEMP TEMP = A A = B B = TEMPEND SUBSubs and functions are public by default but can be declared as PRIVATE.