What is a Constant ?

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 are accessed as if they were static fields.
Constants that are being accessed outside the class must be prefixed with the class name.
Constants can be marked as public, private, protected, internal or protected internal
Constants are immutable values which are known at compile time and do not change for the life of the program.
Constants must be initialized when it is declared.

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.

Constant Scope

The default scope for constants is Private.
Constants have scope and this scope is identical to that defined for variables.

public const int month = 12; 

Multiple constants of the same type can be declared on the same line.

const int months = 12, weeks = 52, days = 365; 


An expression can be used that is based on other constants
Assuming there is no circular reference.

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 that is hard coded.
Symbolic constants - A literal constant that is represented by a name.
Enumerations - A grouping of symbolic constants into categories.

Literal Constants

A literal constant is a specific value such as number, date or text string that does not change.

Dim dtExpiryDate As Date 
   dtExpirydate = #01/07/2010#

Date constants are enclosed between two hash signs #01/07/2014#.

Symbolic Constants

This is also sometimes referred to as just a constant.
To define or declare a symbolic constant use the keyword Const.
Note that string constants are enclosed in double quotes "string".

Const sFOLDERPATH As String = "C:\Temp\" 

In this case every instance of the variable sFOLDERPATH is replaced with the string "C:\Temp\".
sFOLDERPATH is a constant since it never changes but it is not a literal constant since it is not used anywhere.
The advantage of using symbolic constants is that if you decide to change the value at a later date you only need to change the definition of the variable sFOLDERPATH rather than searching and replacing all occurrences of the old string.


Enumerations are value types which default to integers.
The enum type enables you to define named constants
Please refer to the Syntax > Enumerations section for more details.

© 2017 Better Solutions Limited. All Rights Reserved. © 2017 Better Solutions Limited