Numeric Data Types

If you know that a variable will always contain whole numbers (such as 12) rather than fractional numbers (such as 3.57), declare it as an Integer or Long type.


Private memSize As Long
Dim loopCounter As Integer

Operations are faster with Integer variables, which consume less memory than Variant variables. Integer variables are especially useful as the counter variables in For...Next loops. For more information about For...Next loops and other control structures, see Chapter 3, "Controlling Program Flow."

If a variable will always contain fractional numbers, declare it as a Single, Double, or Currency type.


Public radius As Single
Private area As Double
Dim costOfGoods As Currency

The Currency data type supports up to four digits to the right of the decimal separator and 15 digits to the left of it, making Currency an accurate fixed-point data type suitable for monetary calculations. Floating-point (Single and Double) numbers have much larger ranges than Currency, but can be subject to small rounding errors.

Note

Floating-point values can be expressed as mmmEeee or mmmDeee, where mmm is the mantissa and eee is the exponent (a power of 10). The highest positive value of a Single data type is 3.402823E+38, or 3.4 times 10 to the 38th power; the highest positive value of a Double data type is 1.79769313486232D+308, or about 1.8 times 10 to the 308th power. Using D to separate the mantissa and exponent in a numeric literal causes the value to be treated as a Double data type. Likewise, using E in the same fashion causes the value to be treated as a Single data type.

A variable or constant of any numeric data type (or the Variant data type) can contain any numeric data. For example, assigning the value 3.14 to a variable of the Integer data type doesn't generate an error, even though 3.14 isn't an integer. Visual Basic rounds off rather than truncates the fractional part of a floating-point number before assigning it to an integer.

A variable with one of the numeric data types is initialized to 0 (zero) after it's declared and before you assign another value to it. When you edit any Visual Basic module, all variables in the workbook that contains that module are reinitialized. Procedure-level variables are reinitialized when the procedure starts running.