vba data types code example
Example 1: excel vba second byte from INTEGER
LoByte = n And &HFF '<-- if n is a 2-byte Integer (left byte)
HiByte = (n And &HFF00&) \ &H100 '<-- if n is a 2-byte Integer (right byte)
Example 2: excel vba data types
VBA Numeric Data Types Decimal
Digits of
Largest CONSECUTIVE INTEGER Value Type Bytes Precision Description Minimum Negative Value Maximum Negative Value Default Value Minimum Positive Value Maximum Positive Value Type
---------------------------------------------- --------------- ------- ---------- ------------------------------------- ------------------------------ ------------------------------- ------------- ------------------------------- ------------------------------- ---------------
2^8-1 = 255 Byte 1 8-bit unsigned power-of-two integer N/A N/A 0 +1 +255 Byte
2^15-1 = 32767 Integer (%) 2 16-bit signed 2's complement integer -32768 -1 0 +1 +32767 Integer (%)
2^24 = 16777216 Single (!) 4 7 Floating Point IEEE 754 32-bit -3.402823 x 10^38 -1.401298 x 10^-45 0 +1.401298 x 10^-45 +3.402823E38 Single (!)
2^32-1 = 2147483647 Long (&) 4 32-bit signed 2's complement integer -2147483648 -1 0 +1 +2147483647 Long (&)
(2^63-1)/10k = 922337203685477 Currency (@) 8 4 Scaled Integer/10k -922337203685477.5808 -0.0001 0 + +922337203685477.5807 Currency (@)
2^53 = 9007199254740992 Double (#) 8 15 Floating Point IEEE 754 64-bit -1.79769313486231 x 10^308 -4.94065645841247 x 10^-324 0 +4.94065645841247 x 10^-324 +1.79769313486232 x 10^308 Double (#)
2^63-1 = 9223372036854775807 LongLong (^)* 8 64-bit signed 2's complement integer -9223372036854775808 -1 0 +1 +9223372036854775807 LongLong (^)*
2^96-1 = 79228162514264337593543950335 Decimal 12 0 - 28 Scaled Integer -79228162514264337593543950335 -0.0000000000000000000000000001 0 +0.0000000000000000000000000001 +79228162514264337593543950335 Decimal
Variant Variant
Subtype Subtype
Note(*): The LongLong is only available in 64-bit VBA.
Note: The Variant data type can hold any of the above types as
subtypes of the Variant, as well as Strings, pointers and
error values.
Note: it is also possible
to store an integer as digits in the String data type:
2^16 characters is the length limit for a fixed-length string.
2^31 characters is the length limit for a variable-length string.
Example 3: excel vba long integer to bits
Function LongToBits$(ByVal n&)
Dim i&
LongToBits = "00000000000000000000000000000000"
If n And &H80000000 Then
Mid$(LongToBits, 1, 1) = "1"
n = n And Not &H80000000
End If
For i = 32 To 2 Step -1
If n And 1 Then Mid$(LongToBits, i, 1) = "1"
n = n \ 2
Next
End Function
'------------------------------------------------------------------------------
MsgBox LongToBits(0) '<--displays: 00000000000000000000000000000000
MsgBox LongToBits(293781237) '<--displays: 00010001100000101011111011110101
MsgBox LongToBits(-1) '<--displays: 11111111111111111111111111111111
Example 4: vba data types
VBA Data Types Decimal
Digits of
Largest CONSECUTIVE INTEGER Value Type Bytes Precision Description Minimum Negative Value Maximum Negative Value Default Value Minimum Positive Value Maximum Positive Value Type
---------------------------------------------- --------------- ------- ---------- ------------------------------------- ------------------------------ ------------------------------- ------------- ------------------------------- ------------------------------- ---------------
2^8-1 = 255 Byte 1 8-bit unsigned power-of-two integer N/A N/A 0 +1 +255 Byte
2^15-1 = 32767 Integer (%) 2 16-bit signed 2's complement integer -32768 -1 0 +1 +32767 Integer (%)
2^24 = 16777216 Single (!) 4 7 Floating Point IEEE 754 32-bit -3.402823 x 10^38 -1.401298 x 10^-45 0 +1.401298 x 10^-45 +3.402823E38 Single (!)
2^32-1 = 2147483647 Long (&) 4 32-bit signed 2's complement integer -2147483648 -1 0 +1 +2147483647 Long (&)
(2^63-1)/10k = 922337203685477 Currency (@) 8 4 Scaled Integer/10k -922337203685477.5808 -0.0001 0 + +922337203685477.5807 Currency (@)
2^53 = 9007199254740992 Double (#) 8 15 Floating Point IEEE 754 64-bit -1.79769313486231 x 10^308 -4.94065645841247 x 10^-324 0 +4.94065645841247 x 10^-324 +1.79769313486232 x 10^308 Double (#)
2^63-1 = 9223372036854775807 LongLong (^)* 8 64-bit signed 2's complement integer -9223372036854775808 -1 0 +1 +9223372036854775807 LongLong (^)*
2^96-1 = 79228162514264337593543950335 Decimal 12 0 - 28 Scaled Integer -79228162514264337593543950335 -0.0000000000000000000000000001 0 +0.0000000000000000000000000001 +79228162514264337593543950335 Decimal
Variant Variant
Subtype Subtype
Note(*): The LongLong is only available in 64-bit VBA.
Note: The Variant data type can hold any of the above types as
subtypes of the Variant, as well as Strings, pointers and
error values.
Note: it is also possible
to store an integer as digits in the String data type:
2^16 characters is the length limit for a fixed-length string.
2^31 characters is the length limit for a variable-length string.