Type 语句Type statement

模块级别使用, 以定义包含一个或多个元素的用户定义的数据类型Used at the module level to define a user-defined data type containing one or more elements.

语法Syntax

[私有 | 公共]类型varname__elementname[([下标])]类型[ Private | Public ] Type varname elementname [ ( [ subscripts ] ) ] As type
[ elementname [([下标])]作为类型]. . .[ elementname [ ( [ subscripts ] ) ] As type ] . . .
**端面类型** **End Type**


Type 语句语法具有以下部分:The Type statement syntax has these parts:

PartPart 说明Description
PublicPublic 可选。Optional. 用于声明对所有项目中的所有模块中的所有过程可用的用户定义的类型Used to declare user-defined types that are available to all procedures in all modules in all projects.
PrivatePrivate 可选。Optional. 用于声明用户定义的类型,这些类型仅在用于生成声明的模块中可用。Used to declare user-defined types that are available only within the module where the declaration is made.
varnamevarname 必需。Required. 用户定义的类型的名称;遵循标准变量命名约定。Name of the user-defined type; follows standard variable naming conventions.
elementnameelementname 必需。Required. 用户定义的类型的元素的名称。Name of an element of the user-defined type. 元素名称也遵循标准变量命名约定(可使用的关键字除外)。Element names also follow standard variable naming conventions, except that keywords can be used.
下标subscripts 如果未明确规定_较低_的下限, 则由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.
typetype 必需。Required. 元素的数据类型;可以是ByteBooleanIntegerLongCurrencySingleDoubleDecimal (目前不支持)、 Datestring (对于可变长度字符串)、 string长度(对于固定长度的字符串)、对象变量、其他用户定义类型或对象类型Data type of the element; 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, another user-defined type, or an object type.

备注Remarks

Type语句只能在模块级别使用。The Type statement can be used only at the module level. 使用type语句声明用户定义的类型后, 可以在声明范围中的任何位置声明该类型的变量。After you have declared a user-defined type by using the Type statement, you can declare a variable of that type anywhere within the scope of the declaration. 使用 DimPrivatePublicReDimStatic 声明用户定义的类型的变量。Use Dim, Private, Public, ReDim, or Static to declare a variable of a user-defined type.

标准模块类模块中,默认情况下,用户定义的类型是公用的。In standard modules and class modules, user-defined types are public by default. 可以使用Private关键字更改此可见性。This visibility can be changed by using the Private keyword.

Type...End Type 块中不允许使用行号行标签Line numbers and line labels aren't allowed in Type...End Type blocks.

用户定义的类型通常用于数据记录,它们经常由不同的数据类型的大量相关元素组成。User-defined types are often used with data records, which frequently consist of a number of related elements of different data types.

以下示例演示了用户定义的类型中的固定大小的数组的用法:The following example shows the use of fixed-size arrays in a user-defined type:

Type StateData 
    CityCode (1 To 100) As Integer    ' Declare a static array. 
    County As String * 30 
End Type 
 
Dim Washington(1 To 100) As StateData 

在上面的示例中StateData , 包含CityCode静态数组, 且记录Washington的结构与StateData相同。In the preceding example, StateData includes the CityCode static array, and the record Washington has the same structure as StateData.

在用户定义的类型中声明固定大小的数组时,必须使用数值文本或常量而非变量来声明其维度。When you declare a fixed-size array within a user-defined type, its dimensions must be declared with numeric literals or constants rather than variables.

示例Example

此示例使用 Type 语句定义用户定义的数据类型。This example uses the Type statement to define a user-defined data type. Type 语句仅在模块级别上使用。The Type statement is used at the module level only. 如果它在类模块中出现,则 Type 语句的前面必须有关键字 PrivateIf it appears in a class module, a Type statement must be preceded by the keyword Private.

Type EmployeeRecord    ' Create user-defined type. 
    ID As Integer    ' Define elements of data type. 
    Name As String * 20 
    Address As String * 30 
    Phone As Long 
    HireDate As Date 
End Type 
Sub CreateRecord() 
    Dim MyRecord As EmployeeRecord    ' Declare variable. 
 
    ' Assignment to EmployeeRecord variable must occur in a procedure. 
    MyRecord.ID = 12003    ' Assign a value to an element. 
End Sub

另请参阅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.