VBA Variant Data Type in Excel

Variant Data Type in Excel VBA

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)

Variant (numbers):

The Variant (numbers) can contain any numeric value as large as Double.The Variant numbers occupies 16 bytes (128 bits).

Variant (characters):

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.

Data Type Variant Syntax in Excel VBA

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.

Example on the Variant in Excel VBA

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.

Convert an Expression to Variant Data Type in Excel VBA

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.

VBA CVar Function

Also read about all other data types.

VBA Data Types

1 thought on “Variant Data Type in Excel VBA”

  1. Socopa Corretora

    I read this paragraph completely regarding the resemblance of most recent and earlier technologies, it’s awesome article.

Leave a Comment

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