Const statement
Declares constants for use in place of literal values.
Syntax
[ Public | Private ] Const constname [ As type ] = expression
The Const statement syntax has these parts:
Part | Description |
---|---|
Public | Optional. Keyword used at the module level to declare constants that are available to all procedures in all modules. Not allowed in procedures. |
Private | Optional. Keyword used at the module level to declare constants that are available only within the module where the declaration is made. Not allowed in procedures. |
constname | Required. Name of the constant; follows standard variable naming conventions. |
type | Optional. Data type of the constant; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Decimal (not currently supported), Date, String, or Variant. Use a separate As type clause for each constant being declared. |
expression | Required. Literal, other constant, or any combination that includes all arithmetic or logical operators except Is. |
Remarks
Constants are private by default. Within procedures, constants are always private; their visibility can't be changed. In standard modules, the default visibility of module-level constants can be changed by using the Public keyword. In class modules, however, constants can only be private and their visibility can't be changed by using the Public keyword.
To combine several constant declarations on the same line, separate each constant assignment with a comma. When constant declarations are combined in this way, the Public or Private keyword, if used, applies to all of them.
You can't use variables, user-defined functions, or intrinsic Visual Basic functions (such as Chr) in expressions assigned to constants.
Note
Constants can make your programs self-documenting and easy to modify. Unlike variables, constants can't be inadvertently changed while your program is running.
If you don't explicitly declare the constant type by using As type, the constant has the data type that is most appropriate for expression.
Constants declared in a Sub, Function, or Property procedure are local to that procedure. A constant declared outside a procedure is defined throughout the module in which it is declared. Use constants anywhere you can use an expression.
Example
This example uses the Const statement to declare constants for use in place of literal values. Public constants are declared in the General section of a standard module, rather than a class module. Private constants are declared in the General section of any type of module.
' Constants are Private by default.
Const MyVar = 459
' Declare Public constant.
Public Const MyString = "HELP"
' Declare Private Integer constant.
Private Const MyInt As Integer = 5
' Declare multiple constants on same line.
Const MyStr = "Hello", MyDouble As Double = 3.4567
See also
Support and feedback
Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기