Objective of VBA CDbl Function:
VBA CDbl Function is categorized as a Data Type Conversion function. It is a built-in function in Excel VBA. This VBA CDbl function converts an expression to a Double data type.
This function can be used in either procedure or function in a VBA editor window in Excel. We can use this VBA CDbl 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 CDbl function, where we can use this VBA CDbl Function and it’s real-time examples.
- Overview of VBA CDbl Function
- Syntax of VBA CDbl Function
- Parameters or Arguments
- Where we can apply or use VBA CDbl Function?
- Example 1: Convert a String(10000.12345) to Double Data Type
- Example 2: Convert a Value(54321.12345) to Double Data Type
- Example 3: Convert a Value(1000.12345) to Double Data Type
- Example 4: Convert a Value(12345.12345) to Double Data Type
- Example 5: Convert a string(Test) to Double Data Type
- Instructions to use Macro Codes
Syntax of VBA CDbl Function:
The syntax of the CDbl Function in VBA is
CDbl(Expression)
The CDbl function returns a double data type value.
Parameters or Arguments:
The CDbl function has one argument in Excel VBA.
where
Expression:It is a mandatory argument. An expression argument represents a numeric or string value. It is used to convert the numeric or string value to a double value.
Where we can apply or use VBA CDbl Function?
We can use this CDbl 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(10000.12345) to Double Data Type
Here is a simple example of the VBA CDbl function. This below example converts an expression(10000.12345) to Double Date type.
'Convert a String(10000.12345) to Double Data Type Sub VBA_CDbl_Function_Ex1() 'Variable declaration Dim sValue As String Dim dResult As Double sValue = 10000.12345 dResult = CDbl(sValue) MsgBox "String(10000.12345) to Double Data Type : " & dResult, vbInformation, "VBA CDbl Function" End Sub
Output: Here is the screen shot of the first example output.
Example 2: Convert a Value(54321.12345) to Double Data Type
Here is a simple example of the VBA CDbl function. This below example converts an expression(54321.12345) to Double Date type.
'Convert a Value(54321.12345) to Double Data Type Sub VBA_CDbl_Function_Ex2() 'Variable declaration Dim sValue Dim dResult As Double sValue = 54321.12345 dResult = CDbl(sValue) MsgBox "Value(54321.12345) to Double Data Type : " & dResult, vbInformation, "VBA CDbl Function" End Sub
Output: Here is the screen shot of the second example output.
Example 3: Convert a Value(1000.12345) to Double Data Type
Here is a simple example of the VBA CDbl function. This below example converts an expression(1000.12345) to Double Date type.
'Convert a Value(1000.12345) to Double Data Type Sub VBA_CDbl_Function_Ex5() 'Variable declaration Dim sValue As Long Dim dResult As Double sValue = 1000.12345 dResult = CDbl(sValue) MsgBox "Value(1000.12345) to Double Data Type : " & dResult, vbInformation, "VBA CDbl Function" End Sub
Output: Here is the screen shot of the third example output.
Example 4: Convert a Value(12345.12345) to Double Data Type
Here is a simple example of the VBA CDbl function. This below example converts an expression(12345.12345) to Double Date type. It returns an error as Overflow. Because the result is exceded the double maximum value.
'Convert a Value(12345.12345) to Double Data Type Sub VBA_CDbl_Function_Ex3() 'Variable declaration Dim sValue As Integer Dim dResult As Double sValue = 12345.12345 dResult = CDbl(sValue * sValue) MsgBox "Value(12345.12345) to Double Data Type : " & dResult, vbInformation, "VBA CDbl Function" End Sub
Output: Here is the screen shot of the fourth example output.
Example 5: Convert a String(Test) to Double Data Type
Here is a simple example of the VBA CDbl function. This below example converts an expression(Test) to Double Date type. It returns an error as Type mismatch. Because the specified string can’t convert to double data type.
'Convert a string(Test) to Double Data Type Sub VBA_CDbl_Function_Ex4() 'Variable declaration Dim sValue As String Dim dResult As Double sValue = "Test" dResult = CDbl(sValue) MsgBox "String(Test) to Double Data Type : " & dResult, vbInformation, "VBA CDbl Function" End Sub
Output: Here is the screen shot of the fifth example output.
Instructions to use Macro Codes
Here are the step by step instructions to use the CDbl Function in Excel VBA.
- Open an Excel workbook.
- Press Alt+F11 to open VBA Editor window.
- Go to Insert menu and click on module from the available options.
- Copy above specified macros to the VBA editor.
- Select any procedure from above codes and click on ‘Run’ command or use Keyboard shortcut ‘F5’.
List of VBA Functions:
Please click on below link to go back to see all List of VBA Functions.