Name

VarType Function

Syntax

VarType(varname)
varname

Use: Required

The name of a variable.

Return Value

A Variant of subtype Integer representing the data subtype of varname.

Description

Determines the data subtype of a specified variable.

Rules at a Glance

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

Constant

Value

Datatype

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

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—returned only with vbArray (8194)

  • If varname is an array, the VarType function returns 8200 (vbArray) and vbVariant.

  • 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

    Alternatively, you can also use the IsArray function.

Programming Tips & 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, VarType returns the datatype of the default property.

  • The VarType ...

Get VBScript in a Nutshell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.