Dim
- Last UpdatedJul 18, 2023
- 1 minute read
The Dim statement allocates storage for, and declares the data type of, variables and arrays in a module.
The To clause in the array subscript range of a Dim statement provides a more flexible way to control the lower bound of an array. If you don't explicitly set the lower bound with a To clause, the Option Base setting (if used) comes into affect, or defaults to zero (if not used).
Syntax
Dim VariableName[(Subscripts)] [As DataType]
VariableName:
The name of the variable or array being declared (dimensioned).
Subscripts:
The optional subscript range (dimensions) for an array in parentheses.
DataType:
The optional data type declaration for the variable or array.
Related Functions
Example
Dim bytVar As Byte
Dim binVar As Boolean
Dim strVar As String
Dim intVar As Integer
Dim lngVar As Long
Dim sngVar As Single
Dim dblVar As Double
Dim vntVar As Variant
Dim objVar As Object
Dim dtmVar As Date
Dim daysOfWeek() As String ' declares an array variable to hold strings
Dim monthsOfYear(12) As Date ' declares an array variable to hold 12 strings
Dim users(,) As String ' declares a two dimensional array to hold strings
Dim usernames(5,5) As String ' declares a two dimensional 5 x 5 array to hold strings
Dim MyArray(1 To 10, 5 To 15, 10 To 20) ' declares the three dimensional array
MyArray and specifies the upper and lower bounds of each dimension