VBA Function: VarType
The VBA VarType function returns an integer value corresponding to the type of the variable passed as an argument.
Usage:
VarType(variable)
Return Values
The list of values returned by the VarType function:
Constant | Value |
---|---|
vbEmpty | 0 |
vbNull | 1 |
vbInteger | 2 |
vbLong | 3 |
vbSingle | 4 |
vbDouble | 5 |
vbCurrency | 6 |
vbDate | 7 |
vbString | 8 |
vbObject | 9 |
vbError | 10 |
vbBoolean | 11 |
vbVariant | 12 |
vbDataObject | 13 |
vbDecimal | 14 |
vbByte | 17 |
vbLongLong | 20 |
vbUserDefinedType | 36 |
vbArray | 8192 |
If the variable is an array, the number returned by the function will be the sum of two values. For example, a value of 8194 corresponds to 8192 (vbArray) + 2 (vbInteger).
Example of Usage
Using the VarType function to display the type of the variable:
Sub example()
myVariable = "Excel"
MsgBox VarType(myVariable) 'Returns: 8
End Sub
Using the VarType function to display the type of the array:
Sub example()
myArray = Split("x l p", " ")
MsgBox VarType(myArray) 'Returns: 8200
End Sub
The value 8200 here corresponds to 8192 (vbArray) + 8 (vbString).