Numbers

When you think of numbers, you need to put them into two classifications:
Numbers that do not have a decimal point (whole numbers, integers)
Numbers that have a decimal point (real numbers, floating point)


Integers (No Decimal Point)

If a variable needs to contain a whole number you can use the Integer or Long data type.

Dim myByte As Byte           'very small: 0 to 255  
Dim myInteger As Integer 'small integers: -32,768 to 32,767
Dim myLong As Long 'large integers: -2.1 E+9 to 2.1 E+9

Floating Point Numbers

If a variable needs to contain a number that has a decimal point you can use the Single or Double data type.
These two data types have Floating Point precision.
The term floating point refers to the fact that the decimal point can "float/move" to the left or right, between the significant digits of the number.

Dim mySingle As Single     '7 significant figures  
Dim myDouble As Double '15 significant figures

The Single data type uses Single Precision Floating Point.
The Double data type uses Double Precision Floating Point.
The Single and Double data types allow us to specify extremely small and large numbers.
Depending on what the numbers represent, you can encounter rounding errors when performing operations with floating-point numbers.
If your calculations need to be very accurate then you might want to use a Scaled Integer / Fixed Point data type.


Currency and Decimal Data Types

If you need a high level of accuracy you can use the Currency or Variant/Decimal data type.
The variant has a special sub type called Variant/Decimal that can be used to hold very large numbers.

Dim myCurrency As Currency    '15 digits on the left, 4 digits on the right  
Dim myVariant As Variant '29 significant figures
myVariant = CDec(123.4562)

Application.WorksheetFunction.Pi

Only available in Excel

Application.WorksheetFunction.Pi() 
Application.Pi

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopNext