Redigera

Dela via


Understanding Variants

The Variant data type is automatically specified if you don't specify a data type when you declare a constant, variable, or argument.

Variables declared as the Variant data type can contain string, date, time, Boolean, or numeric values, and can convert the values that they contain automatically. Numeric Variant values require 16 bytes of memory (which is significant only in large procedures or complex modules), and they are slower to access than explicitly typed variables of any other type. You rarely use the Variant data type for a constant. String Variant values require 22 bytes of memory.

The following statements create Variant variables:

Dim myVar 
Dim yourVar As Variant 
theVar = "This is some text." 

The last statement does not explicitly declare the variable, but rather declares the variable implicitly, or automatically. Variables that are declared implicitly are specified as the Variant data type.

Tip

If you specify a data type for a variable or argument, and then use the wrong data type, a data type error will occur. To avoid data type errors, either use only implicit variables (the Variant data type) or explicitly declare all your variables and specify a data type. The latter method is preferred.

See also

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.