By declaring a constant, you can assign a meaningful name to a value. You use the Const statement to declare a constant and set its value. After a constant is declared, it cannot be modified or assigned a new value.
You can declare a constant within a procedure or at the top of a module, in the Declarations section. Module-level constants are private by default. To declare a public module-level constant, precede the Const statement with the Public keyword. You can explicitly declare a private constant by preceding the Const statement with the Private keyword to make it easier to read and interpret your code. For more information, see Understanding scope and visibility.
The following example declares the Public constant conAge
as an Integer and assigns it the value 34
.
Public Const conAge As Integer = 34
Constants can be declared as one of the following data types: Boolean, Byte, Integer, Long, Currency, Single, Double, Date, String, or Variant. Because you already know the value of a constant, you can specify the data type in a Const statement.
You can declare several constants in one statement. To specify a data type, you must include the data type for each constant.
In the following statement, the constants conAge
and conWage
are declared as Integer.
Const conAge As Integer = 34, conWage As Currency = 35000