Data Types
A Data Type refers to how the data is stored in memory and how many bytes that data needs.
The data type of a variable (or object) tells the compiler the size of the object.
You should always try and use the data type that uses the smallest number of bytes.
All variables must have an associated data type which determines what data can be stored in it.
Array | (a) array of Integer, Double, String (av) array as a single variant |
Boolean (True or False / 0) | (b) zero (and default value) is False, everything else is True, use NOT to toggle 2 bytes (16 bits) Conversion - CBool |
Byte (0 to 255) | (by) very small positive integers, use Long instead 1 byte (8 bits) Conversion - CByte |
Currency (- 9.2 E+14 to + 9.2 E+14) | (c) scaled integer, use Double instead 8 bytes (64 bits) Conversion - CCur, Declaration Character - @ |
Date (Jan 1 100 to Dec 31 9999) | (dt) floating point numbers 8 bytes (64 bits) Conversion - CDate |
Decimal* (- 7.9 E+28 to + 7.9 E+28) | (dv) Variant subtype Must be declared as a Variant and then converted using the CDec function 14 bytes (112 bits) Conversion - CDec |
Double (- 1.8 E+308 to + 4.9 E+324) | (db) double precision floating point for decimals 8 bytes (64 bits) Conversion - CDbl, Declaration Character - # |
Integer (- 32,768 to + 32,767) | (i) small integers, use Long instead 2 bytes (16 bits) Conversion - CInt, Declaration Character - % |
Long (- 2.1 E+9 to + 2.1 E+9) | (l) very large integers 4 bytes (32 bits) Conversion - CLng, Declaration Character - & |
LongLong | (ll) Added in Office 2010, VBA 7.0 Conversion - CLngLng, Declaration Character - ^ |
LongPtr | (lp) Added in Office 2010, VBA 7.0 LongLong on 64 bit, Long on 32 bit Conversion - CLngPtr |
Object | (o) stored as a reference to an object, default value is Nothing 4 bytes (32 bits) |
Single (- 1.3 E+38 to + 1.3 E+38) | (sg) single precision floating point for decimals, use Double instead 4 bytes (32 bits) Conversion - CSng, Declaration Character - ! |
String | (s) fixed and variable length, default value "" 10 bytes + length Conversion - CStr, Declaration Character - $ |
User Defined | (ud) for the variables (type_) for the actual user defined type |
Variant | (v) any data type, often used for arrays, default value is Empty 16 - 22 bytes |
Array (a) array of Integer, Double, String (av) array as a single variant |
Boolean (True or False / 0) (b) zero (and default value) is False, everything else is True, use NOT to toggle 2 bytes (16 bits) Conversion - CBool |
Byte (0 to 255) (by) very small positive integers, use Long instead 1 byte (8 bits) Conversion - CByte |
Currency (- 9.2 E+14 to + 9.2 E+14) (c) scaled integer, use Double instead 8 bytes (64 bits) Conversion - CCur, Declaration Character - @ |
Date (Jan 1 100 to Dec 31 9999) (dt) floating point numbers 8 bytes (64 bits) Conversion - CDate |
Decimal* (- 7.9 E+28 to + 7.9 E+28) (dv) Variant subtype Must be declared as a Variant and then converted using the CDec function 14 bytes (112 bits) Conversion - CDec |
Double (- 1.8 E+308 to + 4.9 E+324) (db) double precision floating point for decimals 8 bytes (64 bits) Conversion - CDbl, Declaration Character - # |
Integer (- 32,768 to + 32,767) (i) small integers, use Long instead 2 bytes (16 bits) Conversion - CInt, Declaration Character - % |
Long (- 2.1 E+9 to + 2.1 E+9) (l) very large integers 4 bytes (32 bits) Conversion - CLng, Declaration Character - & |
LongLong (ll) Added in Office 2010, VBA 7.0 Conversion - CLngLng, Declaration Character - ^ |
LongPtr (lp) Added in Office 2010, VBA 7.0 LongLong on 64 bit, Long on 32 bit Conversion - CLngPtr |
Object (o) stored as a reference to an object, default value is Nothing 4 bytes (32 bits) |
Single (- 1.3 E+38 to + 1.3 E+38) (sg) single precision floating point for decimals, use Double instead 4 bytes (32 bits) Conversion - CSng, Declaration Character - ! |
String (s) fixed and variable length, default value "" 10 bytes + length Conversion - CStr, Declaration Character - $ |
User Defined (ud) for the variables (type_) for the actual user defined type |
Variant (v) any data type, often used for arrays, default value is Empty 16 - 22 bytes |
Data Type Characters
These are also known as Type Declaration Suffixes
It is possible declare variables by appending a special character to the end of the variable name.
Dim iNumber% 'integer
This method of declaring variables should not be used and is only available for backwards compatibility purposes.
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopNext