What is the CDbl Function?
The VBA CDbl function converts an expression to a double. You’ll see the following scenario if you insert the function in the VBA.
The syntax of the CDbl function is:
CDbl(Expression)
The dataset has the GDP growth of some countries given in string data type. We need to convert the string to a double.
Method 1 – Convert String to Double and Display Using the MsgBox
1.1. Convert String to Double Using the CDbl Function
Step 1: Creating a Module
- To use VBA, create a module in the following ways.
- Open a module by clicking Developer > Visual Basic.
- Go to Insert > Module.
Step 2: Copying the VBA Code
- Copy the following code into the newly created module.
Sub StringToDouble()
Dim Str As String
Dim Dbl As Double
Str = "-19.1234567890123456"
Dbl = CDbl(Str)
MsgBox "Converted Value (in Double Data Type) is" & Dbl
End Sub
In the above code, I declared Str and Dbl as String and Double data types. Then, I assigned the GDP growth of Barbados, i.e., -19.1234567890123456, in place of Str. Later, I used CDbl(Str) for the Dbl value to convert the string into a double data type. Lastly, I utilized the MsgBox to display the output.
Step 3: Running the Code
- Run the code (the keyboard shortcut is F5 or Fn + F5), and you’ll get the following output.
In the case of the double data type, there are 13 digits after the decimal point, but there are 16 digits in this position of the string data type.
The double data type cannot contain more than 13 digits after the decimal point. So, this is a way of verifying whether string data is converted into double.
1.2. Convert String to Integer and String to Double
- Declare the string as an integer.
- To do this, copy the code.
Sub StrToInt()
Dim A As Integer
A = "-19.1234567890123456"
MsgBox A
End Sub
Here, I declared A as an integer data type and assigned a string value to the integer.
If you run the code, you’ll get the output of only -19.
- Declare the string as double.
Sub StrToDbl()
Dim A As Double
A = "-19.1234567890123456"
MsgBox A
End Sub
After running the code, you’ll get the following output.
Method 2 – Using the VBA Sum Function
The VBA SUM function aggregates the arguments. Luckily, you can use the function by summing up zero and string.
Before running the code, copy the following code:
Sub StringToDouble()
Dim Str As String
Dim Dbl As Double
Str = "1.9456"
Dbl = WorksheetFunction.Sum(0 & Str)
MsgBox "Converted Double Type Value is " & Dbl
End Sub
Here, I use the WorksheetFunction.Sum (0 & Str) to convert the string into double data types after declaring data types and assigning the value of Str.
Method 3 – Utilizing the VBA Text Function
The Text function can be used along with the Worksheet—function object.
The function converts numbers to text in a specified format.
Copy the following code to convert the string into a double data type.
Sub StringToDouble()
Dim Str As String, DoubleValue As Double
Str = "-19.1234567890123456"
MsgBox WorksheetFunction.Text(Str, "General")
End Sub
The Text function has two arguments. Here, I assigned Str as the first argument because I wanted to convert the string. Then, I specified the General format.
If you run the code, you’ll get the following output.
Method 4 – Creating a Function to Convert String to Double
If you have a larger dataset, you can create a function to convert a string into double quickly. The function will be useful for repetitive tasks.
Copy the following code to create the function.
Function StrToDbl(GDP_Growth As Variant)
'Converting String to Double Data Type
'Creating StrToDbl Function Through Using the VBA CDbl Function
StrToDbl = CDbl(GDP_Growth)
End Function
Now, the function, i.e. StrToDbl, is automatically created. Instead of running the code, go to the main working sheet. Then, type StrToDbl in the D5 cell. You’ll see the function in the following screenshot. Click on the function.
Press Enter and use the Fill Handle Tool to copy the formula for the below cells.
You’ll get the following output.
Read More: How to Convert String to Number in Excel VBA
Method 5 – Converting a Range of Strings to Double
- Copy the following code into the code module:
Sub Convert_Range_StrToDbl()
Dim k As Integer
Dim Dbl As Double
For k = 5 To 10
Dbl = Cells(k, 3).Value
Cells(k, 4).Value = Dbl
Next k
End Sub
In the above code, I used a For Loop to get the output in the D5:D10 cell range. To do this, I declared k as an Integer and assigned its value to 5 to 10. I also specified the input and output cell range using the Cells(row, column).Value function.
- Run the code, and the output will look as follows.
Here, I stored the cell’s value in a double-type variable, Dbl, which is why each of the values has been converted into double.
Common Errors
- Be careful about the Run-time error ‘13’: Type Mismatch. You’ll get such an error if you input any values except numerical ones.
- You may get #VALUE! error if the Double data type exceeds the digit limits.
Download the Practice Workbook
Further Readings
- How to Convert Date from String Using VBA
- How to Convert Text to Number in Excel with VBA
- How to Convert Text to Date with Excel VBA