Tutorial by Examples

If a code module does not contain Option Explicit at the top of the module, then the compiler will automatically (that is, "implicitly") create variables for you when you use them. They will default to variable type Variant. Public Sub ExampleDeclaration() someVariable = 10 ...
Scope A variable can be declared (in increasing visibility level): At procedure level, using the Dim keyword in any procedure; a local variable. At module level, using the Private keyword in any type of module; a private field. At instance level, using the Friend keyword in any type of class m...
If you have a value that never changes in your application, you can define a named constant and use it in place of a literal value. You can use Const only at module or procedure level. This means the declaration context for a variable must be a class, structure, module, procedure, or block, and can...
The Dim statement should be reserved for local variables. At module-level, prefer explicit access modifiers: Private for private fields, which can only be accessed within the module they're declared in. Public for public fields and global variables, which can be accessed by any calling code. Fr...
Type Hints are heavily discouraged. They exist and are documented here for historical and backward-compatibility reasons. You should use the As [DataType] syntax instead. Public Sub ExampleDeclaration() Dim someInteger% '% Equivalent to "As Integer" Dim someLong& ...
In VBA, Strings can be declared with a specific length; they are automatically padded or truncated to maintain that length as declared. Public Sub TwoTypesOfStrings() Dim FixedLengthString As String * 5 ' declares a string of 5 characters Dim NormalString As String Debug.Print Fi...
A Static variable declared locally is not destructed and does not lose its value when the Sub procedure is exited. Subsequent calls to the procedure do not require re-initialization or assignment although you may want to 'zero' any remembered value(s). These are particularly useful when late bindin...

Page 1 of 1