Dim Statement |
Named Arguments
No
Syntax
Dim varname[([subscripts])] [As [New] type] [, varname[([subscripts])] [As [New] type]] . . .
varname
Use: Required
Your chosen name for the variable.
subscripts
Use: Optional
Dimensions of an array variable.
New
Use: Optional
Keyword that creates an instance of an object.
type
Use: Optional
The data type of varname.
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 module to declare a variable of a particular data type.
Rules at a Glance
In addition to the Visual Basic data types listed in Chapter 3, type can be an Object, an object type, or a user-defined type. The default data type, when no type is explicitly declared, is Variant.
Variable-length strings are declared using the syntax:
Dim variablename As String
Fixed-length strings, on the other hand, are declared using the syntax:
Dim variablename As String * length
You can declare multiple variables in a single Dim statement, but each variable you declare must use a separate As type clause.
Variables have the following values when they are first initialized with the Dim statement:
Data Type Initial Value Numeric 0 Variable-length string Zero-length string ("") Fixed-length string Padded with zeros Variant Empty Object Nothing To use an object variable that has not been declared using the New keyword, the Set statement must assign an object to the variable before it can be used, as ...
Get VB & VBA in a Nutshell: The Language now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.