423,846 Members | 2,048 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

VBA Data Types

MMcCarthy
Expert Mod 10K+
P: 14,534
The Boolean Data Type (–1 or 0)
Use the Boolean numeric data type to store logical data that contains only two values: on and off, true and false, yes and no, and so on. The keywords True and False are predefined constants and are interchangeable with the values –1 and 0, respectively.

The Byte Data Type
(0 to 255)
Byte is VBA's smallest numeric data type and holds a numeric value from 0 to 255. This data type doesn't include any negative values. If you attempt to assign one, VBA returns an error.

The Currency Data Type

Use the Currency numeric data type to store monetary values from –922,337,203,477.5808 to 922,337,203,685,477.5807.

The Date Data Type

The Date data type stores a specially formatted numeric value that represents both the date and time. You don't have to store both the date and time value. The Date data type accepts either the date or the time, or both. Possible values range from January 1, 100 to December 31, 9999.

The Decimal Data Type
The Decimal data type is a subtype of Variant and not a truly separate data type all its own, accommodating values from –79,228,162,514,264,337,593,543,950,335 to 79,228,162,514,264,337,593,543,950,335 if the value contains no decimal places. The data type maintains precision up to 28 decimal places with values from –7.9228162514264337593543950335 to 7.9228162514264337593543950335.

The Double Data Type
Use the Double data type to store precision floating point numbers from –1.79769313486232E308 to –4.94065645841247E-324 or 1.79769313486232E308 to 4.94065645841247E-324.

The Integer Data Type

This is probably the most common data type in use, besides String. Use this data type to store only whole numbers that range from –32,768 to 32,767.

The Long Data Type
The Long data type is also an Integer data type storing only whole numbers, but the range is much larger than the traditional Integer data type. Use Long to store values from –2,147,483,648 to 2,147,483,647.

The Object Data Type
An Object variable is actually a reference to an Access object, such as a form, report, or control. Or, the data type can reference an ActiveX component, or a class object created in a class module.

The Single Data Type
The Single data type stores precision numbers—numbers with decimal places or fractional numbers. The data type is similar to Double, but the range is smaller. Use this data type to store values from –3402823E38 to –1.401298E–45 or from 1.401298E–45 to 3.402823E38.

The String Data Type
String is another very common data type; it stores values or numbers, but treats them as text. There are two varieties: fixed and variable. A fixed string can handle from 1 to 65,400 characters. To declare a fixed string, use the Dim statement in the form

Dim variablename As String * stringlength

In contrast, the variable String data type grows and shrinks as required to fit its stored value. By default, all String variables are of this type. To declare this type, use the Dim statement in the form

Dim variablename As String


The Variant Data Type
The Variant data type stores numeric and non-numeric values. This data type is the most flexible of the bunch because it stores very large values of almost any type (matches the Double numeric data type). Use it only when you're uncertain of the data's type or when you're accommodating foreign data and you're not sure of the data type's specifications.

The Variant data type is VBA's default, so the following code interprets varValue as a Variant:

Dim varValue

Although the Variant data type is flexible, VBA processes these data types a little slower because it must determine the most accurate data type for the assigned value. However, most likely, you'll never notice the performance hit.

The biggest disadvantage is the data type's lack of readability. By that, we mean that you can't easily determine the appropriate data type by viewing the code, and that can be a problem.

Note:
Please see Matthew's post for information on updates in VBA7 for 64-bit arithmetic and pointer compatibility
Feb 13 '07 #1
Share this Article
Share on Google+
5 Comments


P: 20
The Long Data Type
2,147,486,647 should read 2,147,483,647
(Open Windows Calculator, select scientific view, and try 2^31 - 1, to get this result. It's a 32-bit signed integer.)
Aug 2 '10 #2

MMcCarthy
Expert Mod 10K+
P: 14,534
Thanks, I'll edit this in the original :) Probably just a type originally.

Mary
Aug 4 '10 #3

P: n/a
Appu George
Good notes, thanks
Nov 5 '10 #4

P: n/a
Matthew Slyman
Updates in VBA7 for 64-bit arithmetic and pointer compatibility:

http://msdn.microsoft.com/en-us/libr...ice.14%29.aspx

http://msdn.microsoft.com/en-us/libr...ice.11%29.aspx

Note, for example, the new 64-bit LongLong type and various Ptr types.
Nov 23 '10 #5

MMcCarthy
Expert Mod 10K+
P: 14,534
Thank you for that Matthew. I've edited the original article to add a link to your post.
Nov 25 '10 #6