Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
40 lines (26 sloc) 2.2 KB
title keywords f1_keywords ms.prod ms.assetid ms.date localization_priority
Declaring constants (VBA)
vbcn6.chm1076698
vbcn6.chm1076698
office
c1b65bc4-1e94-828c-67bf-357a75261657
12/21/2018
Normal

Declaring constants

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

See also

[!includeSupport and feedback]

You can’t perform that action at this time.