VarType Function

Named Arguments

No

Syntax

VarType(varname)

varname

Use: Required

The name of a variable.

Return Value

An integer representing the data type of varname.

Description

Determines the data type of a specified variable.

Rules at a Glance

  • The VarType function can't be used with user-defined types.

  • The following intrinsic constants can test the return value of the VarType function:

    Constant Value Data Type or Variant Value
    vbArray 8192 Array
    vbBoolean 11 Boolean
    vbByte 17 Byte
    vbCurrency 6 Currency
    vbDataObject 13 A data access object variable
    vbDate 7 Date
    vbDecimal 14 Decimal
    vbDouble 5 Double-precision floating-point number
    vbEmpty 0 Uninitialized variant
    vbError 10 An error
    vbInteger 2 Integer
    vbLong 3 Long integer
    vbNull 1 No valid data
    vbObject 9 Variable explicitly declared as type Object
    vbSingle 4 Single-precision floating-point number
    vbString 8 String
    cbUserDefinedType 36 User-defined type
    vbVariant 12 Variant—only returned with vbArray (8192)
  • If varname is an array, the VarType function returns 8192 (vbArray) plus the value of the data type of the array. For example, an array of strings returns 8192 + 8 = 8200. To test for an array, you can use the intrinsic constant vbArray. For example:

    If VarType(myVar) And vbArray Then
         MsgBox "This is an array"
    End If

Programming Tips and Gotchas

When you use VarType with an object variable, you may get what appears to be an incorrect return value. The reason for this is that if the object has a default property, ...

Get VB & VBA in a Nutshell: The Language now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.