Читать книгу Excel 2019 Power Programming with VBA - Michael Alexander, Dick Kusleika - Страница 158
Declaring constants
ОглавлениеYou declare constants with the Const
statement. Here are some examples:
Const NumQuarters as Integer = 4 Const Rate = .0725, Period = 12 Const ModName as String = "Budget Macros" Public Const AppName as String = "Budget Application"
The second example doesn't declare a data type. Consequently, VBA determines the data type from the value. The Rate
variable is a Double
, and the Period
variable is an Integer
. Because a constant never changes its value, you normally want to declare your constants as a specific data type.
Like variables, constants have a scope. If you want a constant to be available within a single procedure only, declare it after the Sub
or Function
statement to make it a local constant. To make a constant available to all procedures in a module, declare it before the first procedure in the module. To make a constant available to all modules in the workbook, use the Public
keyword and declare the constant before the first procedure in a module. Here's an example:
Public Const InterestRate As Double = 0.0725