| Start |
|---|
| Type Hints Type Hints are heavily discouraged. They exist and are documented here for historical and backward-compatibility reasons. You should use the Type hints significantly decrease code readability and encourage a legacy Hungarian Notation (opens new window) which also hinders readability: Instead, declare variables closer to their usage and name things for what they're used, not after their type: Type hints can also be used on literals, to enforce a specific type. By default, a numeric literal smaller than 32,768 will be interpreted as an Type hints are usually not needed on literals, because they would be assigned to a variable declared with an explicit type, or implicitly converted to the appropriate type when passed as parameters. Implicit conversions can be avoided using one of the explicit type conversion functions: String-returning built-in functionsThe majority of the built-in functions that handle strings come in two versions: A loosely typed version that returns a These functions are:
Note that these are function aliases, not quite type hints. The In very early versions of VBA the |
| Declaring Variables |
| Constants (Const) 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 cannot be a source file, namespace, or interface. Whilst it can be considered good practice to specify Constant types, it isn't strictly required. Not specifying the type will still result in the correct type: Note that this is specific to Constants and in contrast to variables where not specifying the type results in a Variant type. While it is possible to explicitly declare a constant as a String, it is not possible to declare a constant as a string using fixed-width string syntax |
| Declaring Fixed-Length Strings In VBA, Strings can be declared with a specific length; they are automatically padded or truncated to maintain that length as declared. |
| Implicit And Explicit Declaration If a code module does not contain In the above code, if It is considered best practice to use Option Explicit in code modules, to ensure that you declare all variables. See VBA Best Practices (opens new window) how to set this option by default. |
| Access Modifiers The
Access modifiers are applicable to variables and procedures alike. Option Private ModulePublic parameterless Conversely, public Specifying |
| When to use a Static variable 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 binding an object in a 'helper' sub that is called repeatedly. Snippet 1: Reuse a Scripting.Dictionary object (opens new window) across many worksheets Snippet 2: Create a worksheet UDF that late binds the VBScript.RegExp object
scope of the operations performed. Remember that a UDF is not calculated once in the lifetime of a workbook. Even a non-volatile UDF will recalculate whenever the values within the range(s) it references are subject to change. Each subsequent recalculation event only increases the benefits of a statically declared variable.
Related reference: Static (Visual Basic) (opens new window) |
Read Article
https://develop.horje.com/learn/1434/reference