Variant Data Type in Excel VBA explained with syntax and examples.Variant Data Type in Excel VBA explained with syntax and examples. It is a special and universal data type to use for any kind of data except fixed lenth string. It is more flexible, but occupies more space in memory. Type declaration character is not available for variant data type.
There are two types of variant data types..
1. Variant (numbers) and
2. Variant (characters)
The Variant (numbers) can contain any numeric value as large as Double.The Variant numbers occupies 16 bytes (128 bits).
The Variant (characters) can contain Same as variant variable-length String. The Variant characters occupies 22 bytes (176 bits) and the memory that is required for the Variant itself.
Let us see Data Type Variant Syntax in Excel VBA.
Dim VariableName as Variant 'or Dim VariableName
Where VariableName represents the name of the variable.
Variant represents type of data.
Here is an example on the Variant numbers and characters in Excel VBA.
Sub VBAF1_Variant_DataType() 'Variable declaration Dim Name As Variant Dim DateofJoin As Variant Dim Salary As Variant Name = "Patricia Katts" DateofJoin = #1/1/2019# Salary = 75000 End Sub
The above example accepted string, date and integer data types.
You can convert an expression to Variant VBA data type using VBA CVar function. Click on the following link to learn about CVar function and complete tutorial and examples.
Also read about all other data types.