Export (0) Print
Expand All
Expand Minimize

Using Constants

This page is specific to the Visual Basic for Applications (VBA) Language Reference for Office 2010.

Your code might contain frequently occurring constant values, or might depend on certain numbers that are difficult to remember and have no obvious meaning. You can make your code easier to read and maintain using constants. A constant is a meaningful name that takes the place of a number or string that does not change. You can't modify a constant or assign a new value to it as you can a variable.

There are three types of constants:

Intrinsic constants or system-defined constants are provided by applications and controls. Other applications that provide object libraries, such as Microsoft Access, Microsoft Excel, Microsoft Project, and Microsoft Word also provide a list of constants you can use with their objects, methods, and properties. You can get a list of the constants provided for individual object libraries in the Object Browser.

Visual Basic constants are listed in the Visual Basic for Applications type library, and Data Access Object (DAO) library.

NoteNote

Visual Basic continues to recognize constants in applications created in earlier versions of Visual Basic or Visual Basic for Applications. You can upgrade your constants to those listed in the Object Browser. Constants listed in the Object Browser don't have to be declared in your application.

In earlier versions of Visual Basic, constant names were usually capitalized with underscores. For example:

TILE_HORIZONTAL 

Intrinsic constants are now qualified to avoid the confusion when constants with the same name exist in more than one object library, which may have different values assigned to them. There are two ways to qualify constant names:

  • By prefix

  • By library reference

The intrinsic constants supplied by all objects appear in a mixed-case format, with a 2-character prefix indicating the object library that defines the constant. Constants from the Visual Basic for Applications object library are prefaced with "vb" and constants from the Microsoft Excel object library are prefaced with "xl". The following examples illustrate how prefixes for custom controls vary, depending on the type library.

  • vbTileHorizontal

  • xlDialogBorder

You can also qualify the reference to a constant by using the following syntax:

[libname.] [modulename.]constname

The syntax for qualifying constants has these parts:

Part

Description

libname

Optional. The name of the type library that defines the constant. For most custom controls (not available on the Macintosh), this is also the class name of the control. If you don't remember the class name of the control, position the mouse pointer over the control in the toolbox. The class name is displayed in the ToolTip.

modulename

Optional. The name of the module within the type library that defines the constant. You can find the name of the module by using the Object Browser.

constname

The name defined for the constant in the type library.


For example:

Threed.LeftJustify 

Community Additions

ADD
Show:
© 2014 Microsoft