声明常量

By declaring a constant, you can assign a meaningful name to a value. 使用 Const 语句声明常量并设置其值。 After a constant is declared, it cannot be modified or assigned a new value.

在声明部分中,可在过程内或模块的顶部声明常量。 Module-level constants are private by default. To declare a public module-level constant, precede the Const statement with the Publickeyword. 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. 有关详细信息,请参阅 了解范围和可见性

以下示例将 公共 常量 conAge 声明为 Integer ,并为其赋值 34

Public Const conAge As Integer = 34

常量可以声明为以下 数据类型之一: 布尔字节整数货币日期字符串Variant。 Because you already know the value of a constant, you can specify the data type in a Const statement.

可在一个语句中声明多个常量。 若要指定数据类型,必须为每个常量包括数据类型。

在以下语句中,常量 conAgeconWage 声明为 Integer

Const conAge As Integer = 34, conWage As Currency = 35000

另请参阅

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。