Understanding Variants

Last modified: December 06, 2011

Applies to: Office 2013 | VBA

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 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.

TipTip

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.

Show:
© 2014 Microsoft