VarType Function

This function returns a number that indicates the data type of the data stored in a variable.
If you use the Variant data type in your code you will eventually be in a situation where you need to know exactly what type of data is stored in this variable.
You can only provide the name of a single variable.
The VarType function returns an enumerated value indicating which data type has been assigned.

iValue = VarType(vMyVariant) 

where vMyVariant is the name of the variable whose sub type you want to find.


vbVarType Enumeration

There is an enumeration that you can use to indicate the type of variant object


Number Returned

There are built-in VBA constants with the same numerical values that you can use instead of the actual numbers.

NumberVBA.vbVarType
8192 (&H2000) vbArray
0vbEmpty
1vbNull
2vbInteger
3vbLong
4vbSingle
5vbDouble
6vbCurrency
7vbDate
8vbString
9vbObject
10vbError
11vbBoolean
12vbVariant
13vbDataObject
14vbDecimal
17 (&H11) vbByte
36 (&H24) vbUserDefinedType

Identifying an Array

The number equivalent for vbArray is 8192.
This actual number can never be returned from the VarType Function.
If you pass in an array variable the number that is returned will be greater than 8192.
In fact the number returned is 8192 plus the value of the arrays underlying data type.

Dim myArray() As Long 
Call MsgBox(VarType(myArray)) = 8195

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext