Wednesday, October 12, 2022

Variables Data type Summary in Excel VBA

1.       Boolean
Storage size: 2 bytes
Range: True or False
Declaration:
Dim BlnVar As Boolean

2.       Byte
Storage size: 1 byte
Range: 0 to 255
Declaration:
Dim BytVar As Byte

3.       Collection
Storage size: Unknown
Range: Unknown
Declaration:
Dim ColVar As Collection

4.       Currency (scaled integer)
Storage size: 8 bytes
Range: -922,337,203,685,477.5808 to 922,337,203,685,477.5807
Declaration: Dim CurVar As Currency OR Dim CurVar@

5.       Date
Storage size: 8 bytes
Range: January 1, 100, to December 31, 9999
Declaration: Dim DtVar As Date

6.       Decimal
Storage size: 14 bytes
Range1: +/-79,228,162,514,264,337,593,543,950,335 with no decimal point
Range2: +/-7.9228162514264337593543950335 with 28 places to the right of the decimal
Note: Smallest non-zero number is +/-0.0000000000000000000000000001
Declaration: Dim DecVar As Variant follow by
                       DecVar = Cdec(Number)

7.       Double (double-precision floating-point)
Storage size: 8 bytes
Range1: -1.79769313486231E308 to -4.94065645841247E-324 for negative values
Range2: 4.94065645841247E-324 to 1.79769313486232E308 for positive values
Declaration: Dim DblVar As Double OR Dim DblVar#

8.       Integer
Storage size: 2 bytes
Range: -32,768 to 32,767
Declaration: Dim IntVar As Integer OR Dim IntVar%

9.       Long (Long integer)
Storage size: 4 bytes
Range: -2,147,483,648 to 2,147,483,647
Declaration: Dim LngVar As Long OR Dim LngVar&

10.   LongLong (LongLong integer)
Storage size: 8 bytes
Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Note: Valid on 64-bit platforms only.
Declaration: Dim LngLngVar As LongLong OR Dim LngLngVar^

11.   LongPtr (Long integer on 32-bit systems, LongLong integer on 64-bit systems)
Storage size: 4 bytes on 32-bit systems
Range on 32-bit: -2,147,483,648 to 2,147,483,647 on 32-bit systems
Storage size: 8 bytes on 64-bit systems
Range 64-bit: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 on 64-bit systems
Declaration: Dim LngPtrVar As LongPtr

12.   Object
Storage size: 4 bytes
Range: Any Object reference
Declaration: Dim ObjVar As Object

13.   Single (single-precision floating-point)
Storage size: 4 bytes
Range: -3.402823E38 to -1.401298E-45 for negative values
Range: 1.401298E-45 to 3.402823E38 for positive values
Declaration: Dim SglVar As Single OR Dim SglVar!

14.   String (variable-length)
Storage size: 10 bytes + string length
Range: 0 to approximately 2 billion
Declaration: Dim StrVar As String OR Dim StrVar$

15.   String (fixed-length)
Storage size: Length of string
Range: 1 to approximately 65,400
Declaration: Dim StrVar As String OR Dim StrVar$

16.   Variant (with numbers)
Storage size: 16 bytes
Range: Any numeric value up to the range of a Double
Declaration: Dim VarVar As Variant OR Dim VarVar$

17.   Variant (with characters)
Storage size: 22 bytes + string length (24 bytes on 64-bit systems)
Range: Same range as for variable-length String
Declaration: Dim VarVar As Variant OR Dim VarVar$

18.   User-defined (using Type)
Storage size: Number required by elements
Range: The range of each element is the same as the range of its data type.
Declaration:
Type MyType
    iTem1 as String
    iTem2 as Integer
End type

Dim UdfVar As MyType

Note: Basically we have more than 18 but we are not going to use all. The most frequent use probably String, Integer, Double, Long, Variant and date. To be discuss in detail later on.

Microsoft Reference-Declaring-variables
Microsoft Reference-Data-type-summary
Other Reference-Data-type-summary

Practice makes perfect. Thank You.

macro enabled excel
excel macro
vba coding
vba code

No comments:

Post a Comment