Variant Data Types and Coercion
The variant data type is capable of storing numbers, strings, dates, and times. When using a variant, you do not have to explicitly convert a variable from one data type to another. This data type conversion is handled automatically, and is termed data type coercion.
' declares variant variable
Dim vntVar
' assign numeric 10 to variant
vntVar = 10
' add numeric 8 to numeric variant value
vntVar = vntVar + 8
' convert variant to string value and concatenates strings
vntVar = "F" & vntVar
' print string "F18"
print vntVar
Numeric characters inside quotes ("567") will be stored and treated as a string in a variant data type variable. If this string (containing numeric characters) is subsequently used in a numeric operation, it will be coerced into a numeric data type and treated as a number in the operation. Conversely, numeric characters stored as a number data type in a variant variable, and subsequently used in an operation with a string, will be coerced into a string data type, and treated as a string value in the operation.
Note: To determine the type
of a variant variable, use the function VarType( )
, which returns a value that
corresponds to the explicit data types. See VarType for return values.