Using data types efficiently

Unless otherwise specified, undeclared variables are assigned the Variant data type. This data type makes it easy to write programs, but it is not always the most efficient data type to use.

You should consider using other data types if:

  • Your program is very large and uses many variables.
  • Your program must run as quickly as possible.
  • You write data directly to random-access files.

In addition to Variant, supported data types include Byte, Boolean, Integer, Long, Single, Double, Currency, Decimal, Date, Object, and String.

Use the Dim statement to declare a variable of a specific type, for example:

Dim X As Integer 

This statement declares that a variable X is an integer — a whole number between -32,768 and 32,767. If you try to set X to a number outside that range, an error occurs. If you try to set X to a fraction, the number is rounded. For example:

X = 32768      ' Causes error. 
X = 5.9        ' Sets x to 6. 

See also

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.