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.
Data Type | CType | Default | Range | Size | Abr. | (Prefix) Comments |
Boolean | CBool | False | True (-1) or False (0) | 2 bytes (16 bits) | (b) The zero value is treated as False All other values are treated as True. Use NOT to toggle | |
Byte | CByte | 0 to 255 | 1 byte (8 bits) | (by) integers often used in conjunction with string operations Use Long instead | ||
Currency | CCur | 0 | - 9.2 E+14 to + 9.2 E+14 | 8 bytes (64 bits) | @ | (c) scaled integer Use Double instead |
Date | CDate | Jan 1 100 to Dec 31 9999 | 8 bytes (64 bits) | (dt) floating point numbers Use Long instead | ||
Decimal* | CDec | - 7.9 E+28 to + 7.9 E+28 | 14 bytes | (dec) Variant subtype Must be declared as a Variant and then converted. | ||
Double | CDbl | 0 | - 1.8 E+308 to + 4.9 E+324 | 8 bytes (64 bits) | # | (db) double precision floating point used for high accuracy |
Integer | CInt | 0 | - 32,768 to + 32,767 | 2 bytes (16 bits) | % | (i) integer Use Long instead |
Long | CLng | 0 | - 2,147,483,648 to 2,147,483,647 | 4 bytes (32 bits) | & | (l) long integer |
LongLong | CLngLng | 0 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 8 bytes (64 bits) | (ll) Added in Office 2010, VBA 7.0 | |
LongPtr | CLngPtr | (lp) Added in Office 2010, VBA 7.0 Long on 32 bit LongLong on 64 bit | ||||
Object | Nothing | 4 bytes (32 bits) | (o) stored as a reference to an object. | |||
Single | CSng | 0 | - 1.3 E+38 to + 1.3 E+38 | 4 bytes (32 bits) | ! | (sng) single precision floating point used when you have fractional numbers Use Double instead |
String | CStr | "" | 10 bytes + length | $ | (s) fixed and variable length | |
User Defined | (ud) for the variables (type_) for the actual user defined type | |||||
Variant | Empty | 16 - 22 bytes | $ | (v) any data type, typically used for arrays or variants |
Abbreviations
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.
© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopNext