How To Convert String To Double In Excel VBA (5 Methods)
Có thể bạn quan tâm
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.
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.
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.
After running the code, you’ll get the following output.
Read More: How to Convert String to Long Using VBA in Excel
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 SubHere, 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 SubThe 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 FunctionNow, 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:
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
Methods to Convert String to Double.xlsm
Từ khóa » Visual Basic Double To String
-
Convert Double To String In VB
-
Convert Double To String In
-
VB.NET Converting Double Value To String = Precision Loss
-
Type Conversion Functions - Visual Basic - Microsoft Docs
-
Double.ToString Method (System) - Microsoft Docs
-
Convert Double To String - Visual Basic .NET
-
CStr: Convert Double To String - VB.Net Tutorial
-
[RESOLVED] Convert Double To String WITHOUT Scientific Notation?
-
Visual Basic - Conversion Double To String [Résolu] - CodeS-SourceS
-
9. Declaring Your Variable Types: Long, Double And String, And ...
-
How To Convert Double To String In VB.NET - CollectiveSolver
-
[PDF] Data Types, Arithmetic, Strings, Input Visual Basic
-
Convert String To Double In C# | How To Convert String To ... - EduCBA
-
Convert String To Double Vb