VarType 函数VarType function

返回一个整数, 指示变量的子类型或对象的默认属性的类型。Returns an Integer indicating the subtype of a variable, or the type of an object's default property.



必需的_varname_ 参数是一个Variant , 它包含除用户定义类型的变量之外的任何变量。The required varname argument is a Variant containing any variable except a variable of a user-defined type.

返回值Return values

要么是下列常量之一, 要么返回其中一个数的总和。Either one of the following constants or the summation of a number of them is returned.

常量Constant Value 说明Description
vbEmptyvbEmpty 00 (未初始化)Empty (uninitialized)
vbNullvbNull 11 Null(不是有效数据)Null (no valid data)
vbIntegervbInteger 双面2 IntegerInteger
vbLongvbLong 第三章3 长整数Long integer
vbSinglevbSingle 44 单精度浮点数Single-precision floating-point number
vbDoublevbDouble 55 双精度浮点数Double-precision floating-point number
vbCurrencyvbCurrency 6 货币值Currency value
vbDatevbDate 7 日期值Date value
vbStringvbString utf-88 StringString
vbObjectvbObject 9 对象Object
vbErrorvbError 10 10 错误值Error value
vbBooleanvbBoolean 11x1711 布尔值Boolean value
vbVariantvbVariant 1212 Variant(仅与变量的数组一起使用)Variant (used only with arrays of variants)
vbDataObjectvbDataObject 1313 数据访问对象A data access object
vbDecimalvbDecimal 14 小数值Decimal value
vbBytevbByte ×17 字节值Byte value
vbLongLongvbLongLong 2020 LongLong整数 (仅在64位平台上有效)LongLong integer (valid on 64-bit platforms only)
vbUserDefinedTypevbUserDefinedType 3636 包含用户定义类型的变量Variants that contain user-defined types
vbArrayvbArray 81928192 数组 (此函数返回时总是添加到另一个常量)Array (always added to another constant when returned by this function)


[!注释] 这些常量由 Visual Basic for Applications 指定。These constants are specified by Visual Basic for Applications. 这些名称可在代码中的任何位置使用来代替实际值。The names can be used anywhere in your code in place of the actual values.


如果传递了一个对象并具有默认属性, 则VarType(object) 将返回该对象的默认属性的类型。If an object is passed and has a default property, VarType(object) returns the type of the object's default property.

VarType 函数本身绝不返回 vbArray 的值。The VarType function never returns the value for vbArray by itself. 总是将其添加到一些其他值以指示特定类型的数组。It is always added to some other value to indicate an array of a particular type. 例如,为整数数组返回的值的计算方式为 vbInteger + vbArray ,或 8194。For example, the value returned for an array of integers is calculated as vbInteger + vbArray, or 8194.

常量 vbVariant 仅与 vbArray 一起使用以指示 VarType 函数的参数是类型 Variant 的数组。The constant vbVariant is only returned in conjunction with vbArray to indicate that the argument to the VarType function is an array of type Variant.


此示例使用VarType函数确定不同变量的子类型, 在一种情况下, 为对象的默认属性的类型。This example uses the VarType function to determine the subtypes of different variables, and in one case, the type of an object's default property.

Dim MyCheck
Dim IntVar, StrVar, DateVar, AppVar, ArrayVar
' Initialize variables.
IntVar = 459: StrVar = "Hello World": DateVar = #2/12/1969#
Set AppVar = Excel.Application
ArrayVar = Array("1st Element", "2nd Element")
' Run VarType function on different types.
MyCheck = VarType(IntVar)   ' Returns 2.
MyCheck = VarType(DateVar)  ' Returns 7.
MyCheck = VarType(StrVar)   ' Returns 8.
MyCheck = VarType(AppVar)   ' Returns 8 (vbString)
                            ' even though AppVar is an object.
MyCheck = VarType(ArrayVar) ' Returns 8204 which is
                            ' `8192 + 12`, the computation of
                            ' `vbArray + vbVariant`.

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