Appendix A. VBA Statements
Throughout this book, I've introduced you to various VBA statements. (It's worth mentioning here that a statement is any VBA keyword or construct that isn't a function, object, property, or method.) These statements appeared on an "as-needed" basis whenever I wanted to explain a particular VBA topic (such as If...Then...Else and the other control structures you saw in , "Controlling Your VBA Code"). Although I covered many VBA statements in this book, I bypassed quite a few in the interests of brevity and simplicity.
In an effort to put some finishing touches on our VBA coverage, this appendix presents a brief, but complete, look at every VBA statement. I give you the name of the statement, the arguments it uses (if any; note, too, that required arguments are shown in bold type ), and a short description. For those statements that I didn't cover in this book, you can get full explanations and examples from the Statements section of the VBA Help file.
Table A.1. VBA Statements
Statement | Description |
---|
AppActivate title , wait | Activates the running application with the title or task ID given by title . |
Beep | Beeps the speaker. |
Call name, argumentlist | Calls the name procedure. (Because you can call a procedure just by using its name, the Call statement is rarely used in VBA programming.) |
ChDir path | Changes the current directory (folder) to path . |
ChDrive drive | Changes the current drive to drive . |
Close filenumberlist | Closes one or more I/O files opened with the Open statement. |
Const CONSTNAME | Declares a constant variable named CONSTNAME . |
Date = date | Changes the system date to date . |
Declare name | Declares a procedure from a dynamic link library (DLL). |
DefBool letterrange | A module-level statement that sets the default data type to Boolean for all variables that begin with the letters in letterrange (for example, DefBool A-F). |
DefByte letterrange | Sets the default data type to Byte for all variables that begin with the letters in letterrange . |
DefCur letterrange | Sets the default data type to Currency for all variables that begin with the letters in letterrange . |
DefDate letterrange | Sets the default data type to Date for all variables that begin with the letters in letterrange . |
DefDbl letterrange | Sets the default data type to Double for all variables that begin with the letters in letterrange . |
DefInt letterrange | Sets the default data type to Integer for all variables that begin with the letters in letterrange . |
DefLng letterrange | Sets the default data type to Long for all variables that begin with the letters in letterrange . |
DefObj letterrange | Sets the default data type to Object for all variables that begin with the letters in letterrange . |
DefSng letterrange | Sets the default data type to Single for all variables that begin with the letters in letterrange . |
DefStr letterrange | Sets the default data type to String for all variables that begin with the letters in letterrange . |
DefVar letterrange | Sets the default data type to Variant for all variables that begin with the letters in letterrange . |
DeleteSetting appname,section , key | Deletes a section or key from the Registry. |
Dim varname | Declares a variable named varname . |
Do...Loop | Loops through one or more statements while a logical condition is True. |
End keyword | Ends a procedure, function, or control structure. |
Enum name | Module-level statement that declares an enumeration variable. |
Erase arraylist | Frees the memory allocated to a dynamic array or reinitializes a fixed-size array. |
Error errornumber | Simulates an error by setting Err to errornumber . |
Event procedurename ( arglist ) | Class module-level statement that declares a user-defined event. |
Exit keyword | Exits a procedure, function, or control structure. |
FileCopy source, destination | Copies the source file to destination . |
For Each...Next | Loops through each member of a collection. |
For...Next | Loops through one or more statements until a counter hits a specified value. |
Function | Declares a user-defined function procedure. |
Get # filenumber, varname | Reads an I/O file opened by the Open statement into a variable. |
GoSub...Return | Branches to and returns from a subroutine within a procedure. (However, creating separate procedures makes your code more readable.) |
GoTo line | Sends the code to the line label given by line . |
If...Then...Else | Runs one of two sections of code based on the result of a logical test. |
Implements InterfaceName, Class | Specifies the name of an interface or a class to be implemented in a class module. |
Input # filenumber, varlist | Reads data from an I/O file into variables. |
Kill pathname | Deletes the file pathname from a disk. |
Let varname = expression | Sets the variable varname equal to expression . Let is optional and is almost never used. |
Line Input # filenumber, var | Reads a line from an I/O file and stores it in var . |
Load | Loads a user form into memory without displaying it. |
Lock # filenumber , recordrange | Controls access to an I/O file. |
LSet stringvar = string | Left-aligns a string within a String variable. |
LSet var1 = var2 | Copies a variable of one user-defined type into another variable of a different user-defined type. |
Mid | Replaces characters in a String variable with characters from a different string. |
MidB | Replaces byte data in a String variable with characters from a different string. |
MkDir path | Creates the directory (folder) named path . |
Name oldpathname As newpathname | Renames a file or directory (folder). |
On Error | Sets up an error-handling routine. |
On...GoSub, On...GoTo | Branches to a line based on the result of an expression. |
Open pathname , etc. | Opens an input/output (I/O) file. |
Option Base 0|1 | Determines (at the module level) the default lower bound for arrays. |
Option Compare Text|Binary |