Excel VBA Double Data Type
VBA Double is a kind of data type we assign to declare variables, which is an improved or longer version of the “Single” data type variable and is usually used to store longer decimal places.
VBA IntegerVBA IntegerIn VBA, an integer is a data type that may be assigned to any variable and used to hold integer values. In VBA, the bracket for the maximum number of integer variables that can be kept is similar to that in other languages. Using the DIM statement, any variable can be defined as an integer variable. data type always converts decimal values to the nearest integer value. The single data type can show up to two digits of decimal places. On the other hand “Double” data type can store values from -1.79769313486231E308 to -4.94065645841247E324 for negative values and for positive numbers it can store values from 4.94065645841247E-324 to 1.79769313486232E308.
More importantly, it consumes 8 bytes of memory.
Examples to use VBA Double Data Type
Before we see the example of the “Double” data type, let’s look at the example codes of “Integer” and “Single” data type in VBA. Look at the below VBA codeVBA CodeVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task..
Sub Integer_Ex() Dim k As Integer k = 2.569999947164 MsgBox k End Sub
I have declared the variable “k” as Integer, and for this variable, I have assigned the value as 2.569999947164.
Let us run this code manually or using excel shortcut keyExcel Shortcut KeyAn Excel shortcut is a technique of performing a manual task in a quicker way. F5, to see the final value in the message box in VBAMessage Box In VBAVBA MsgBox function is an output function which displays the generalized message provided by the developer. This statement has no arguments and the personalized messages in this function are written under the double quotes while for the values the variable reference is provided..
The result is showing as 3 instead of the supplied number of 2.569999947164. The reason because is VBA has converted the number to the nearest integer value i.e., 3.
When the decimal value is more than 0.5, then it will convert to the next integer value, and when the decimal value is less than 0.51, then it will convert to below integer value.
Now I will change the data type from Integer to Single.
Sub Integer_Ex() Dim k As Single k = 2.569999947164 MsgBox k End Sub
Run the code through shortcut key F5, and see what number we get this time around.
This time we got the result as 2.57, so this time we got two decimal places. The original value we have assigned was 2.569999947164, so in this case, third, the placed decimal value is 9, so since this is more than 5, it has converted the second place decimal value 6 to 7.
Now change the data type from Single to Double.
Sub Integer_Ex() Dim k As Double k = 2.569999947164 MsgBox k End Sub
Now run the code manually and see how many digits we get in the message box result.
This time got all the decimal values. We can supply up to 14 digits of decimal places under Double data type.
Suppose you supply any value greater than 14 decimal positions will be converted to the nearest value. For example, look at the below image.
I have typed 15 decimal places instead of 14. If I hit the enter key, it will be back to 14 digits only.
Instead of 59 (last two digits), we got 6 i.e., since the last digit is 9, which is greater than 5 previous number 5 is converted to the next integer value i.e., 6
Now I will show how to work with cell referenceHow To Work With Cell ReferenceCell reference in excel is referring the other cells to a cell to use its values or properties. For instance, if we have data in cell A2 and want to use that in cell A1, use =A2 in cell A1, and this will copy the A2 value in A1. in a worksheet. Below are the numbers I have entered in the worksheet.
Let us initiate capturing the same values to next by using INTEGER data type, SINGLE data type, and DOUBLE type.
Below is the code to retain values from column A to B by using the INTEGER data type.
Sub Double_Ex() Dim k As Integer Dim CellValue As Integer For k = 1 To 6 CellValue = Cells(k, 1).Value Cells(k, 2).Value = CellValue Next k End Sub
Let’s run the code through shortcut key F5 to see what values we get in column B.
When we have used Integer as data type, we got all the whole numbers i.e., without decimals.
Now I will just change the VBA data typeVBA Data TypeThe data type is the core character of any variable. It represents what type and range of value we can store in the variable. Data types are built-in VBA, and users or developers need to know which kind of value can be held in which data type. of a variable from integer to Single.
Sub Double_Ex() Dim k As Integer Dim CellValue As Single For k = 1 To 6 CellValue = Cells(k, 1).Value Cells(k, 2).Value = CellValue Next k End Sub
This code will give the below result.
This time we got only two decimal places.
Now changes the data type from single to double.
Sub Double_Ex() Dim k As Integer Dim CellValue As Double For k = 1 To 6 CellValue = Cells(k, 1).Value Cells(k, 2).Value = CellValue Next k End Sub
This will return the below result.
We have got exact values from column A.
Things to Remember
- Double is an improved data type of Single data type.
- It can hold up to 14 decimal places.
- It consumes 8 bytes of system memory.
This has been a guide to VBA Double. Here we discuss how to use and declare VBA double data type to store longer decimal places along with practical examples and a downloadable excel template. Below you can find some useful excel VBA articles –