Skip to main content

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.

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>