VBA Variant Data Type in Excel

VBA Variant Data Type in Excel

VBA Variant Data Type in Excel 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 length 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

Instructions to Run VBA Macro Code or Procedure:

You can refer the following link for the step by step instructions.

Instructions to run VBA Macro Code

Other Useful Resources:

Click on the following links of the useful resources. These helps to learn and gain more knowledge.

VBA Tutorial VBA Functions List VBA Arrays VBA Text Files VBA Tables

VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers Blog

1 thought on “VBA Variant Data Type in Excel”

Leave a Comment