Skip to main content

VarType function

Returns an Integer indicating the subtype of a variable, or the type of an object's default property.

Syntax

VarType(varname)

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
vbEmpty 0 Empty (uninitialized)
vbNull 1 Null (no valid data)
vbInteger 2 Integer
vbLong 3 Long integer
vbSingle 4 Single-precision floating-point number
vbDouble 5 Double-precision floating-point number
vbCurrency 6 Currency value
vbDate 7 Date value
vbString 8 String
vbObject 9 Object
vbError 10 Error value
vbBoolean 11 Boolean value
vbVariant 12 Variant (used only with arrays of variants)
vbDataObject 13 A data access object
vbDecimal 14 Decimal value
vbByte 17 Byte value
vbLongLong 20 LongLong integer (valid on 64-bit platforms only)
vbUserDefinedType 36 Variants that contain user-defined types
vbArray 8192 Array (always added to another constant when returned by this function)

Remarks

If an object is passed and has a default property, VarType(object) returns the type of the object's default property.

The VarType function never returns the value for vbArray by itself. It's always added to some other value to indicate an array of a particular type. For example, the value returned for an array of integers is calculated as vbInteger + vbArray, or 8194.

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.

Example

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`.

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>