The Const Statement | VBA Declaration Statements

Declaring Constants - the Const statement’s syntax. Square brackets, [ ], indicate optional items while vertical bars, |, indicate mutually exclusive items.
The Const statement declares constants in VBA. Constants are prevalent in VBA programs, so it is useful to explore their declaration, as you'll do here.

In this article:

4 minutes read

Introducing the Const Statement

The Const statement declares constants. The header image above shows the Const statement’s syntax. Moreover, based on its location in code, it can confer different levels of scope on variables and objects.

The image below shows the different scope levels in VBA. Also, we’ve delved into scope levels in a separate article, make sure to check it out if you need a refresher.

VBA Program Identifiers – Scope Levels [from the narrowest (block level) to broadest (application level)]
VBA Program Identifier Scope Levels – from the narrowest (block-level) to broadest (application level)

The Const statement may be placed in blocks or procedures, or it can sit atop modules (outside all procedures). In each case, it confers block, procedure, module, or project level scope on a variable or object, respectively.

Salient Points on the Const Statement

There are several vital things to note about the Const statement:

  • A module-level variable’s scope (which is private by default) is editable. But you can’t alter a block-level or procedure-level variable’s scope. In fact, the Private and Public keywords are legal only at the module-level. However, this only applies in Standard modules, in Class modules a constant’s scope (private by default) is immutable.
  • You can include the Private or Public keyword in a module-level constant’s declaration, but not both. That’s in addition to the Const keyword which must always be present.
  • The Const and Private keywords have the same effect at the module-level. However, adding the Private keyword to the Const keyword improves code readability.
  • A single Const statement can declare several constants. To do this, insert a comma after each constant’s name (or data type, if specified). In this use-case, a Private or Public keyword applies to all the constants in the statement. Moreover, if a constant’s data type is set, then that of all other constant’s in the statement must also be set.
  • You can omit a constant’s data type in its declaration. In that case, the compiler assigns the most appropriate data type to it. However, the compiler does not assign the variant data type to such a constant, as it does for the Dim statement.
  • Lifetime does not apply to constants. So, do not replace the Const keyword with the Static keyword. That would result in a compilation error.

Sample Code (Examples)

The sample code below shows the Const statement in use. It also illustrates the salient points discussed above.

When you run the sample code above, the VarType function (lines #22-23 and #28) returns the data type of the variable passed to it as an argument. Those function return-values are then shown in the Immediate window using the Print method of the Debug object, as shown below.

Sample code illustrating the Const statement’s usage. The VarType function’s return values of 2, 8, and 11 displayed in the Immediate window denote Integer, String, and Boolean data types
Sample code illustrating the Const statement’s usage. The VarType function’s return values of 2, 8, and 11 displayed in the Immediate window denote Integer, String, and Boolean data types
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Keep Learning

Related Articles

Keep Learning

Related Articles