VBA CVar Function

VBA CVar Function in Excel

VBA CVar Function in Excel is categorizes as a Data Type Conversion function. It is a built-in function in Excel VBA. This VBA CVar function converts an expression to a Variant data type.

This function use in either procedure or function in a VBA editor window in Excel. We can use this VBA CVar Function in any number of times in any number of procedures or functions. In the following section you learn many topics. Like what is the syntax and parameters of the CVar function, where we can use this VBA CVar Function and it’s real-time examples.

Syntax of VBA CVar Function:

The syntax of the CVar Function in VBA is

CVar(Expression)

The CVar function returns a variant data type value.

Parameters or Arguments:

The CVar function has one argument in Excel VBA.
where
Expression:It is a mandatory argument. An expression argument represents a value. It is used to convert the value to a variant value.

Where we can apply or use VBA CVar Function?

We can use this CVar Function in VBA MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.

Example 1: Convert a String(100) to Variant Data Type

Here is a simple example of the VBA CVar function. This below example specified an expression(100) converts to a variant type value.

'Convert a String(100) to Variant Data Type
Sub VBA_CVar_Function_Ex1()

    'Variable declaration
    Dim sValue As String
    Dim dResult As Variant
    
    sValue = 100
        
    dResult = CVar(sValue)
        
    MsgBox "String(100) to Variant Data Type : " & dResult, vbInformation, "VBA CVar Function"
    
End Sub

Output: Here is the screen shot of the first example output.
VBA CVar Function

Example 2: Convert a Value(12345.678) to Variant Data Type

Here is a simple example of the VBA CVar function. This below example specified an expression(12345.678) converts to a variant type value.

'Convert a Value(12345.678) to Variant Data Type
Sub VBA_CVar_Function_Ex2()

    'Variable declaration
    Dim sValue As Double
    Dim dResult As Variant
    
    sValue = 12345.678
        
    dResult = CVar(sValue)
        
    MsgBox "Value(12345.678) to Variant Data Type : " & dResult, vbInformation, "VBA CVar Function"
    
End Sub

Output: Here is the screen shot of the second example output.
VBA CVar Function

Example 3: Convert a Value(True) to Variant Data Type

Here is a simple example of the VBA CVar function. This below example specified an expression(True) converts to a variant type value.

'Convert a Value(True) to Variant Data Type
Sub VBA_CVar_Function_Ex3()

    'Variable declaration
    Dim sValue As Boolean
    Dim dResult As Variant
    
    sValue = True
        
    dResult = CVar(sValue)
        
    MsgBox "Value(True) to Variant Data Type : " & dResult, vbInformation, "VBA CVar Function"
End Sub

Output: Here is the screen shot of the third example output.
VBA CVar Function

Example 4: Convert a Value(Example_String) to Variant Data Type

Here is a simple example of the VBA CVar function. This below example specified an expression(Example_String) converts to a variant type value.

'Convert a Value(Example_String) to Variant Data Type
Sub VBA_CVar_Function_Ex4()

    'Variable declaration
    Dim sValue As String
    Dim dResult As Variant
    
    sValue = "Example_String"
        
    dResult = CVar(sValue)
        
    MsgBox "Value(Example_String) to Variant Data Type : " & dResult, vbInformation, "VBA CVar Function"
End Sub

Output: Here is the screen shot of the fourth example output.
VBA CVar Function

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 in Excel Blog

VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers

Leave a Reply