Variant Data Type

See Also7DDMJS

The Variant data type3GYXY7 is the default for Visual Basicthe data type that all variables become if they are not explicitly declared as some other type (using statements such as DimLANDIM, GlobalTDN897, or Deftype2OR30RP).  The Variant data type has no type-declaration character4TVC9NP.

The Variant is a special data type that can contain numeric, string, or date data as well as the special values Empty1L2JEZ4 and Null1DDW7C0.  You can determine how the data in a Variant is treated by using the VarTypeXRZH1Y function.  The following table illustrates the return value of the VarType function and the corresponding interpretation of the contents of the Variant:

Return value

Interpretation of Variant

 

0

Empty (uninitialized)

1

Null (no valid data)

2

Integer

3

Long (long integer)

4

Single (single-precision floating-point)

5

Double (double-precision floating-point)

6

Currency (scaled integer)

7

Date

8

String

 

Numeric data can be any integer or real number value ranging from -1.797693134862315D308 to -4.94066D-324 for negative values and from 4.94066D-324 to 1.797693134862315D308 for positive values.  Generally, numeric Variant data is maintained in its original fundamental data type within the Variant.  For example, if you assign an Integer to a Variant, subsequent operations treat the Variant as if it were an Integer.  However, if an arithmetic operation is performed on a Variant containing an Integer, a Long, or a Single, and the result exceeds the normal range for the original data type, the result is promoted within the Variant to the next larger data type.  An Integer is promoted to a Long, and a Long and a Single are promoted to a Double.  An Overflow error occurs when Variant variables containing Currency and Double values exceed their respective ranges.

Always use the Variant data type when the data could contain date information, Empty, or a Null.  You can also use the Variant data type in place of any fundamental data type to work with data in a more flexible way.  If the contents of a Variant variable are digits, they may be either the string representation of the digits or their actual value, depending on the context.  For example:

   Dim MyVar As Variant

   MyVar = 98052

 

In the example shown above, MyVar contains a numeric representationthe actual value 98052.  Arithmetic operators work as expected on Variant variables that contain numeric values or string data that can be interpreted as numbers.  If you use the + operator to add MyVar to another Variant containing a number or to a variable of a numeric data type, the result is an arithmetic sum.  See the information about addition and concatenation operators for complete information on how to use them with Variant data.

The value Empty denotes a Variant variable that hasn't been initialized.  A Variant containing Empty is 0 if it is used in a numeric context and a zero-length string if it is used in a string context.

Don't confuse Empty with Null.  Null indicates that the Variant variable intentionally contains no valid data.


See Also

+ OperatorLANADD

& Operator40C5HGY

VarType FunctionXRZH1Y

Visual Basic Data Types2M0APUD