You are reading the article How To Use Vba Double Data Type In Excel? updated in September 2023 on the website Lifecanntwaitvn.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 How To Use Vba Double Data Type In Excel?
Excel VBA Double Data TypeIn VBA, we have different types of data types that are used as per the need. Like, Integer data type is used for numbers, the String data type is used for alphabets and text and a Long data type is used when we need to use numbers or text without any limit. Similarly, we have a Double data type that is used for decimal numbers. Although, we can use Single for decimals but this will retain only values to 2 points of the decimal. If we want to use decimal values without any limit then we have a Double data type for this.
If we use Integer instead of Double data type for decimal values then it will convert the decimal value into the nearest whole number integers. And also, there is some limit of using Double data type as well. Double can accommodate negative values from -1.79769313486231E308 to -4.94065645841247E324 and for positive values from 4.94065645841247E-324 to 1.79769313486232E308. Beyond this, the value cannot be accepted.
Watch our Demo Courses and Videos
Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.
How to Use VBA Double Data Type in Excel?Below are the different examples to use Double Data Type in Excel VBA.
You can download this VBA Double Excel Template here – VBA Double Excel Template
VBA Double – Example #1In this example we will see, how different data types such as Integers and Single give the output for any decimal value and on the other hand how Double data type gives the output of the same decimal value. For this, follow the below steps:
Step 1: Open a Module from the Insert menu tab.
Step 2: Write the subprocedure for VBA Double. We can choose any other name for defining the name of VBA Double as per our choice.
Code:
Sub
VBA_Double()End Sub
Step 3: Now define a variable, let’s say it be A as Integer as shown below.
Code:
Sub
VBA_Double()Dim
AAs Integer
End Sub
Step 4: Assign a decimal value to the defined variable. Let’s say that the decimal value is 1.23456789 as shown below.
Code:
Sub
VBA_Double()Dim
AAs Integer
A = 1.23456789End Sub
Step 5: Now we need a platform where we can see the output. Here, we can use Msgbox or Debug.Print function. We will use Msgbox which is the most traditional way of doing.
Code:
Sub
VBA_Double()Dim
AAs Integer
A = 1.23456789 MsgBox AEnd Sub
We will see, the output message box with value “1”. This means the Integer data type has converted the decimal value to the nearest whole number as 1.
Step 7: Now let’s change the data type from Integer to Single and see what Single data type will return us.
Code:
Sub
VBA_Double()Dim
AAs Single
A = 1.23456789 MsgBox AEnd Sub
We will see, the Single data type has returned the decimal value which is 1.234568 whereas we fed the input value as 1.23456789.
This means, the data type Single converts the fed decimal value into the nearest possible decimal value by last digit number conversion to nearest value which should be less than 5.
Step 9: Now we will use data type Double in place of Single and see what output we would get.
Code:
Sub
VBA_Double()Dim
AAs Double
A = 1.23456789 MsgBox AEnd Sub
We will see, the message box has exactly the same value which we fed which means Double didn’t convert the input value if it falls under its limit.
VBA Double – Example #2Follow the below steps:
Step 1: Write the subprocedure of VBA Double as shown below.
Code:
Sub
VBA_Double2()End Sub
Step 2: First, we will start with Integer. So define a variable as Integer as shown below. This would be our input variable.
Code:
Sub
VBA_Double2()Dim
AAs Integer
End Sub
Step 3: After that, we will again define a variable as an Integer. This is a variable in which we will store the output.
Code:
Sub
VBA_Double2()Dim
AAs Integer
Dim
DeciAs Integer
End Sub
Step 4: Open a For-Next loop as shown below. This is where we will write the condition to get the data from a column to another column.
Code:
Sub
VBA_Double2()Dim
AAs Integer
Dim
DeciAs Integer
For
Next
AEnd Sub
Step 5: Select the range of cells in variable A. Here, our data is from cell A1 to A10.
Code:
Sub
VBA_Double2()Dim
AAs Integer
Dim
DeciAs Integer
For
A = 1To
10Next
AEnd Sub
Step 6: Now select the cell values which we want to place. Here the cell values are in the first column.
Sub
VBA_Double2()Dim
AAs Integer
Dim
DeciAs Integer
For
A = 1To
10 Deci = Cells(A, 1).ValueNext
AEnd Sub
Step 7: Now we will put the selected values into column 2 which is B in defined variable Deci.
Code:
Sub
VBA_Double2()Dim
AAs Integer
Dim
DeciAs Integer
For
A = 1To
10 Deci = Cells(A, 1).Value Cells(A, 2).Value = DeciNext
AEnd Sub
As we had selected the Integer data type for output variable Deci, so it converted the decimal numbers into Integer whole numbers in Column B.
Step 9: Let’s change the data type of Deci which is the output variable from Integer to Single as shown below.
Code:
Sub
VBA_Double2()Dim
AAs Integer
Dim
DeciAs Single
For
A = 1To
10 Deci = Cells(A, 1).Value Cells(A, 2).Value = DeciNext
AEnd Sub
Step 11: Let’s change the output variable data type from Single to Double. And see, what we will get in Column B.
Code:
Sub
VBA_Double2()Dim
AAs Integer
Dim
DeciAs Double
For
A = 1To
10 Deci = Cells(A, 1).Value Cells(A, 2).Value = DeciNext
AEnd Sub
Pros & Cons of VBA Double
VBA Double converts the same number into decimal numbers in a specific range.
It is quite easy to use.
We can use Double data type instead of Integer or Single, in a similar way.
It doesn’t consider the decimal number beyond the limit which we have seen in the above introduction section.
Things to Remember
Limit of using VBA Double for negative values is from -1.79769313486231E308 to -4.94065645841247E324 and for positive values is from 94065645841247E-324 to 1.79769313486232E308.
VBA Double can hold up to 14 digit values if it is under the above-given limit.
VBA Double uses 8 Byte of system memory each type it is used.
Recommended ArticlesThis is a guide to Excel VBA Double. Here we discuss how to use Double data types in Excel VBA along with practical examples and downloadable excel template. You can also go through our other suggested articles –
You're reading How To Use Vba Double Data Type In Excel?
Update the detailed information about How To Use Vba Double Data Type In Excel? on the Lifecanntwaitvn.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!