Understanding Data Types

When you create (declare) a variable or function by specifying a data type, you are letting Visual Basic know that the variable will always contain a certain kind of data. This is known as an explicit declaration. Although not technically required in Visual Basic, it is an excellent programming practice because:

• Implicit declarations (creating a variable without specifying its data type) result in the Variant data type. Whereas each of the other data types use varying amounts of memory, Variants always use the same comparatively large amount of memory regardless of the data they store. This can result in an inefficient use of memory. For example, storing True or False information in a Variant instead of a Boolean variable uses eight times as much memory.

• If you always use explicit declarations and mistype the name of a variable, Visual Basic recognizes that it's a mistake and gives you a warning. If you use an implicit declaration, Visual Basic just assumes it's a new variable, which can cause unexpected results in your code that are hard to fix because you might not notice the typo.

• Using the functions included with Visual Basic when converting data from one type to another makes your code easier to understand and maintain.

Tip Avoid implicit declarations

Put the following line at the beginning of every class, user form, or code module to have Visual Basic warn you if you accidentally make an implicit declaration:

Option Explicit

To make sure that you always use explicit declarations, you can select the Require Variable Declaration check box in the Visual Basic Editor. This check box is on the Editor tab of the Options dialog box, which you can open by clicking Tools, Options.

Table 30-1 lists data types. Although it is not a complete list, it is probably sufficient for the majority of code you will write in VBA.

Table 30-1. Data Types




Used to store any kind of alphanumeric information.

Numbers stored in Strings are treated as text, so sorting numbers in a

String variable means that 10 comes before 2.


Used to store numbers in the range -2,147,483,648 to 2,147,483,647.


Used to store numbers in the range -32,768 to 32,767.

Integers use half as much memory as Longs.


Used to store True or False information.

If you pass a nonzero number to a Boolean variable, the variable becomes

True. If you pass a Boolean value of True to a numeric variable, the variable

becomes -1.

False always becomes 0, and vice versa, when you pass values between

Boolean and numeric variables.

Object Used to store references to objects.

Object variables declared as Object are referred to as being late-bound.

Object Used to store references to objects.

Object variables declared as Object are referred to as being late-bound.

Late-bound variables are sometimes necessary, such as when your code must work with several different types of object. Code that uses late-bound references, however, can run slower than references declared for a specific class, such as a Project, Resource, Task, and so on. Binding an object variable to a specific class is known as early binding.

(The term object variable is sometimes used to generically describe a vari-

h able that stores a reference to an object, even if the variable was declared a t as a specific class of object.)

For more information about declaring variables, including prefixes and other naming conventions, see "Naming Conventions" later in this chapter on page 894.

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment