Name
Dim Statement
Syntax
Dimvarname[([subscripts])],varname[([subscripts])]
varnameUse: Required
Your chosen name for the variable.
subscriptsUse: Optional
Dimensions of an array variable.
Description
Declares and allocates storage space in memory for variables.
The Dim statement is used either
at the start of a procedure or the start of a global script block.
In the first case, the variable declared using Dim is local to the procedure. In the
second, it’s available throughout the module.
Rules at a Glance
You can declare multiple variables in a single
Dimstatement, as long as you use a comma to delimit them.When variables are first initialized with the
Dimstatement, they have a value of Empty. In addition, if a variable has been initialized but not assigned a value, the following expressions will both evaluate toTrue:If vVar = 0 Then If vVar = "" Then
To declare array variables, use the following syntax:
- Fixed length, single dimension
Dimarrayname(upper)Example:
DimmyArray(10)- Fixed length, multidimensional
Dimarrayname(upper,upper,...)Example:
DimMyArray(20,30)- Variable length, single or multidimensional
Dimarrayname( )Example:
DimmyArray( )
You can declare a multidimensional array with up to 60 dimensions.
Variable-length arrays can be resized using the
ReDimstatement. Fixed-length arrays can’t be resized.
Example
The example shows how to use the Dim statement to define a variable that
receives an array returned by a function:
Dim Input, NumArray Input = InputBox("Enter three ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access