Data Type - Variant

The Variant data type can hold any type of data except fixed-length strings and user defined types.
The Variant data type can hold any of the value or reference data types.
This data type automatically works out which data type is the most appropriate based on the value that is assigned.
This means the data type of the variable can change at runtime depending on what is assigned to it.
Using this data type might be tempting but you compromise both speed and memory by using it.

Dim vMyVariable As Variant 

Using the Variant data type is a feature of a dynamically typed programming language and is the default.


Option Explicit

Whether a language is dynamically typed has nothing to do with Option Explicit
The following two lines are equivalent in VBA, the only difference is that using Option Explicit forces you to have the first one.

Dim vMyVariable As Variant 
Dim vMyVariable

Using the Variant data type (or not declaring a data type at all) is very bad practice and leads to code that is slow and difficult to read.


Statically Typed

The opposite of a dynamically typed programming language is a one that is statically typed.
In a statically typed programming language the data type of a variable cannot change at runtime.
VBA is not a Statically Typed programming language by default.
By giving all your variables specific data types and not using Variant, allows you to write code that is statically typed.


Decimal, Double, Currency

The variant has a special sub type called Decimal that can be used to hold very large numbers.
An error occurs when the Variant variables containing Decimal, Double and Currency values exceed their respective ranges.


Empty, Null, Error

This data type can also contain the special values "Empty", "Null" and "Error".
This keyword/value can only be used in conjunction with the Variant data type.
The Empty data type is the default value when you declare a variable as a Variant.

Dim vMyVariant As Variant 
If (VBA.IsEmpty(vMyVariant) True) Then
If (vMyVariant = Empty) Then

The Null keyword/value can only be used in conjunction with the Variant data type.
This keyword/value is used when a variable declared as variant does not contain any valid data.

Dim vMyVariant As Variant 
If (VBA.IsNull(vMyVariant) True) Then
If (vMyVariant = Null) Then

The Error keyword/value can be used to store an error number.


Calculations

You can perform operations on Variant variables but you need to be careful.
Arithmetic operations or functions can only be performed on numerical values.
If you are performing string concatenation make sure you use the ampersand (&) and not the plus (+).
Variant variables with numbers require 16 bytes of memory.
Variables of the Variant data type with characters usually require 22 bytes of memory plus the memory required by the string.
For negative numbers: -1.797693134862315E308 to -4.94066E-324.
For positive numbers: 4.94066E-324 to 1.797693134862315E308.

Dim vMyVariable As Variant 
myVariable = 200
Call MsgBox(TypeName(myVariable))

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext