Skip to content
Menu
We’ll show you the difference between VAR and CVAR using some sweet graphics, and we’ll also show you how to quickly calculate both VAR and CVAR 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.
Table of Contents:
- Overview
- Syntax of VBA CVar Function
- Parameters or Arguments
- Where we can apply or use VBA CVar Function?
- Example 1: Convert a String(100) to Variant Data Type
- Example 2: Convert a Value(12345.678) to Variant Data Type
- Example 3: Convert a Value(True) to Variant Data Type
- Example 4: Convert a Value(Example_String) to Variant Data Type
- Instructions to Run VBA Macro Code
- Other Useful Resources
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.
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.
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.
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.
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
This Excel tutorial explains how to use the Excel CVAR function with syntax and examples.
Description
The Microsoft Excel CVAR function converts a value to a variant.
The CVAR function is a built-in function in Excel that is categorized as a Data Type Conversion Function. It can be used as a VBA function (VBA) in Excel. As a VBA function, you can use this function in macro code that is entered through the Microsoft Visual Basic Editor.
Syntax
The syntax for the CVAR function in Microsoft Excel is:
CVar( expression )
Parameters or Arguments
- expression
- The value to convert to a variant.
Returns
The CVAR function returns a variant.
Applies To
- Excel for Office 365, Excel 2019, Excel 2016, Excel 2013, Excel 2011 for Mac, Excel 2010, Excel 2007, Excel 2003, Excel XP, Excel 2000
Type of Function
- VBA function (VBA)
Example (as VBA Function)
The CVAR function can only be used in VBA code in Microsoft Excel.
Let’s look at some Excel CVAR function examples and explore how to use the CVAR function in Excel VBA code:
Dim LValue As Variant LValue = CVar(8)
The variable called LValue would now contain the variant value of «8».
В этом учебном материале вы узнаете, как использовать Excel функцию CVAR с синтаксисом и примерами.
Описание
Microsoft Excel функция CVAR преобразует значение в тип вариант.
Функция CVAR — это встроенная в Excel функция, которая относится к категории функций преобразования типов данных. Его можно использовать как функцию VBA в Excel. В качестве функции VBA вы можете использовать эту функцию в коде макроса, который вводится через редактор Microsoft Visual Basic Editor.
Синтаксис
Синтаксис функции CVAR в Microsoft Excel:
CVar( expression )
Параметры или аргументы
- expression
- Значение, которое нужно преобразовать в тип вариант.
Возвращаемое значение
Функция CVAR возвращает вариант.
Применение
- Excel для Office 365, Excel 2019, Excel 2016, Excel 2013, Excel 2011 для Mac, Excel 2010, Excel 2007, Excel 2003, Excel XP, Excel 2000
Тип функции
- Функция VBA
Пример (как функция VBA)
Функцию CVAR можно использовать только в коде VBA в Microsoft Excel. Рассмотрим некоторые примеры Excel функции CVAR, чтобы понять, как использовать функцию CVAR в коде Excel VBA:
Dim LValue As Variant LValue = CVar(8) |
Переменная LValue теперь будет содержать значение варианта «8».
Home / VBA / Top VBA Functions / VBA CVAR Function (Syntax + Example)
The VBA CVAR function is listed under the data type conversion category of VBA functions. When you use it in a VBA code, it converts an expression into a variant data type. In simple words, a variant data type value can hold numeric, string, or date values. Learn more about variant data types from here.
CVar(Expression)
Arguments
Expression: The value which you want to convert to a string data type.
Example
To practically understand how to use the VBA CVAR function, you need to go through the below example where we have written a vba code by using it:
Sub example_CVAR()
Range("B1").Value = CVar(Range("A1"))
End Sub
In the above example, we have used the value from cell A1 (Excel Champs) and then we used the CVARvto convert that value into a variant data type and it has returned Excel Champs in the result.