A constant is a meaningful name that takes the place of a value that does not change.
You should always use constants in place of hard coded values or strings.
Constants must be declared at the very top and cannot be declared below any properties, subroutines or functions.
Unlike variables, constants cannot be inadvertently changed at run-time.
You cannot use any variables, user defined function or any built-in functions in your declarations.
You should always declare your constants with specific data types (never with a Variant data type).
All public constants and variables should be in capitals except for the data type prefix

Constant Scope

The default scope for module level constants is Private.
Constants have scope and this scope is identical to that defined for variables.
This constant can only be used in the module that it is declared.

Const m_iMODULELEVEL As Integer = 100 

This constant can be used in any of the modules in the whole project.

Public Const g_iPROJECTLEVEL As Integer = 100 

For more details on the scope of variables, please refer to the Variables > Lifetime and Scope page.


A constant can be defined using other, already declared constants.

Public Const g_sVERSION As String = "4.0.3" 
Public Const g_sDIALOG_PREFIX As String = "Better (" & g_sVERSION & ")"

Types of Constants

The VBA Language has several types of constants.
Literal constants A specific value such as a number, date or text string that cannot change.
Symbolic constants - A literal constant that is represented by a name.
Enumerations - A grouping of symbolic constants into categories.
Built-in constants - These are typically in the form of enumerations.
If you do not declare the data type of the constant then the most appropriate data type for the expression is used ??
In class modules constants can only be Private and their visibility cannot be changed to Public.


If you attempt to change the value of a constant you will receive an error.
The default scope for constants is Private.

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext