The VBA CVar Function

Description

The VBA CVar function converts an expression into a Variant data type.

The syntax of the function is:

CVar( Expression )

Where the Expression argument is the expression that that you want to convert to a Variant data type.

Note that the Variant data type can hold numeric, string, or date values.


VBA CVar Function Examples

The following VBA code shows how the CVar function can be used to convert numeric values, strings and dates into Variant data types.

' Convert an Integer, a String and a Date into Variants
Dim var1 As Variant
Dim var2 As Variant
Dim var3 As Variant
var1 = CVar( 100 )
' var1 is now equal to 100
var2 = CVar( "Test String" )
' var3 is now equal to "Test String"
var3 = CVar( #1/1/2016# )
' var3 is now equal to 1/1/2016

After running the above VBA code, the variables var1, var2 and var3 are equal to 100, "Test String" and 1/1/2016, respectively. However, these values are now stored as Variant data types.