Public 语句Public statement

模块级别使用, 以声明公共变量和分配存储空间。Used at the module level to declare public variables and allocate storage space.

语法Syntax

公共[ WithEvents ]varname[([下标])][ As [ New ] type ]Public [ WithEvents ] varname [ ( [ subscripts ] ) ] [ As [ New ] type ]
[, [ WithEvents ] varname [([下标])] [ As [ New ] type ]]. . .[ , [ WithEvents ] varname [ ( [ subscripts ] ) ] [ As [ New ] type ]] . . .


“公共”**** 语句语法包含以下部分:The Public statement syntax has these parts:

PartPart 说明Description
WithEventsWithEvents 可选。Optional. 说明 varname 是用于响应 ActiveX 对象触发的事件的 对象变量关键字Keyword specifying that varname is an object variable used to respond to events triggered by an ActiveX object. WithEvents 仅在类模块中有效。WithEvents is valid only in class modules. 您可以使用withevents按照喜欢的任意方式声明任意多个变量, 但不能使用withevents创建数组, 也不能在withevents中使用NewYou can declare as many individual variables as you like by using WithEvents, but you can't create arrays with WithEvents, nor can you use New with WithEvents.
varnamevarname 必需。Required. 变量的名称;遵循标准命名约定。Name of the variable; follows standard naming conventions.
下标subscripts 可选。Optional. 数组变量的维度;达到 60 多维度可能会被声明。Dimensions of an array variable; up to 60 multiple dimensions may be declared. 下标参数使用以下语法:The subscripts argument uses the following syntax:

[低于**** ]上限[, [低于**** ]上部]. . .[ lowerTo ] upper [ , [ lowerTo ] upper ] . . .

如果未明确规定_较低_的下限, 则由**Option Base** 语句控制数组的下限。When not explicitly stated in lower, the lower bound of an array is controlled by the Option Base statement. 如果不存在Option Base语句, 则下限为零。The lower bound is zero if no Option Base statement is present.
NewNew 可选。Optional. 启用对象的隐式创建的关键字。Keyword that enables implicit creation of an object. 如果在声明对象变量时使用new , 则会在首次引用时创建该对象的新实例, 因此您无需使用**Set** 语句来分配对象引用。If you use New when declaring the object variable, a new instance of the object is created on first reference to it, so you don't have to use the Set statement to assign the object reference. New关键字不能用于声明任何固有数据类型的变量。The New keyword can't be used to declare variables of any intrinsic data type. 它还不能用于声明依赖对象的实例, 并且不能与WithEvents一起使用。It also can't be used to declare instances of dependent objects, and it can't be used with WithEvents.
typetype 可选。Optional. 变量的数据类型;可以是ByteBooleanIntegerLongCurrencySingleDoubleDecimal (目前不支持)、 Datestring (对于可变长度字符串)、 string长度(对于固定长度的字符串)、对象变量用户定义类型对象类型Data type of the variable; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Decimal (not currently supported), Date, String (for variable-length strings), String length (for fixed-length strings), Object, Variant, a user-defined type, or an object type. 对要定义的每个变量使用单独的As _type_子句。Use a separate As type clause for each variable being defined.

备注Remarks

使用Public语句声明的变量可用于所有应用程序中所有模块中的所有过程, 除非Option Private Module有效;在这种情况下, 变量仅在其所在的项目中是公共的。Variables declared by using the Public statement are available to all procedures in all modules in all applications unless Option Private Module is in effect; in which case, the variables are public only within the project in which they reside.

The Public statement can't be used in a class module to declare a fixed-length string variable.The Public statement can't be used in a class module to declare a fixed-length string variable.

Use the Public statement to declare the data type of a variable.Use the Public statement to declare the data type of a variable. For example, the following statement declares a variable as an Integer:For example, the following statement declares a variable as an Integer:

Public NumberOfEmployees As Integer 

Also use a Public statement to declare the object type of a variable.Also use a Public statement to declare the object type of a variable. 以下语句为新的工作表实例声明一个变量:The following statement declares a variable for a new instance of a worksheet:

Public X As New Worksheet 

如果在声明对象变量时不使用New关键字, 则必须先使用Set语句为引用该对象的变量分配一个现有对象, 然后才能使用该变量。If the New keyword is not used when declaring an object variable, the variable that refers to the object must be assigned an existing object by using the Set statement before it can be used. 在分配对象之前, 声明的对象变量具有特殊值**Nothing**, 这表示它不引用对象的任何特定实例。Until it is assigned an object, the declared object variable has the special value Nothing, which indicates that it doesn't refer to any particular instance of an object.

You can also use the Public statement with empty parentheses to declare a dynamic array.You can also use the Public statement with empty parentheses to declare a dynamic array. 声明动态数组后, 使用过程中的**ReDim** 语句定义数组中的维度和元素的数目。After declaring a dynamic array, use the ReDim statement within a procedure to define the number of dimensions and elements in the array. 如果您尝试为在**PrivatePublicDim** 语句中显式指定其大小的数组变量重新声明一个维度, 则会发生错误。If you try to redeclare a dimension for an array variable whose size was explicitly specified in a Private, Public, or Dim statement, an error occurs.

如果不指定数据类型或对象类型, 并且模块中没有**Deftype** 语句, 则默认情况下变量为VariantIf you don't specify a data type or object type, and there is no Deftype statement in the module, the variable is Variant by default.

在初始化变量时,数值变量将初始化为 0,可变长度字符串将初始化为零长度字符串 (""),固定长度字符串将使用零填充。When variables are initialized, a numeric variable is initialized to 0, a variable-length string is initialized to a zero-length string (""), and a fixed-length string is filled with zeros. 将“变量”**** 变量初始化为Variant variables are initialized to Empty. 用户定义类型的变量的每个元素将会像它们是单独的变量那样初始化。Each element of a user-defined type variable is initialized as if it were a separate variable.

示例Example

此示例使用“公共”**** 语句在标准模块的模块级(通用部分)显式声明变量为公共;即变量可用于所有应用程序中所有模块内的所有过程,除非 Option Private Module 有效。This example uses the Public statement at the module level (General section) of a standard module to explicitly declare variables as public; that is, they are available to all procedures in all modules in all applications unless Option Private Module is in effect.

Public Number As Integer ' Public Integer variable. 
Public NameArray(1 To 5) As String ' Public array variable. 
' Multiple declarations, two Variants and one Integer, all Public. 
Public MyVar, YourVar, ThisVar As Integer 

另请参阅See also

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.