Dim (statement)

<< Click to Display Table of Contents >>

Dim (statement)

Dim Name [(Subscripts)][As Type] [, ...]

Allocates memory for a variable and defines its type.

Name is the name of the variable.

Subscripts indicates the number and size of the dimensions, in case an array is created (see the section Arrays). Use the following syntax:

[LowerLimit To] UpperLimit [, [LowerLimit To] UpperLimit ] ...
For LowerLimit and UpperLimit, you should give integer values that determine the largest and smallest allowed values for the array index, thereby specifying the array size. Only fixed values are allowed, variables are not acceptable. If LowerLimit is omitted, it will take the value specified through the Option Base command (0 or 1).
To declare dynamic arrays (see the ReDim statement), omit all limits:
Dim a()

Type specifies the data type (Integer, Long, Single, Double, String, String*n, Boolean, Variant, Object or a user-defined type). Alternatively, the type can be indicated by appending a type suffix (e.g. % for Integer) to the variable name (see the section Data types).

Dim Value As Integer

is identical to:

Dim Value%

If neither a data type nor a type suffix is given, a Variant variable will be created.

See also: Option Base, ReDim, section Variables

Example:

Sub Main

 Dim a As Integer          ' (alternatively: Dim a%)

 Dim b As Long

 Dim c As Single

 Dim d As Double

 Dim e As String

 Dim f As Variant          ' (alternatively: Dim f)

 Dim g(10,10) As Integer        ' Array of variables

 .

 .

 .