Writing declaration statements
You use declaration statements to name and define procedures, variables, arrays, and constants. When you declare a procedure, variable, or constant, you also define its scope, depending on where you place the declaration and what keywords you use to declare it.
The following example contains three declarations.
Sub ApplyFormat()
Const limit As Integer = 33
Dim myCell As Range
' More statements
End Sub
The Sub statement (with matching End Sub statement) declares a procedure named ApplyFormat
. All the statements enclosed by the Sub and End Sub statements are executed whenever the ApplyFormat
procedure is called or run.
The Const statement declares the constant limit
specifying the Integer data type and a value of 33.
The Dim statement declares the myCell
variable. The data type is an object, in this case, a Microsoft Excel Range object. You can declare a variable to be any object that is exposed in the application that you are using. Dim statements are one type of statement used to declare variables. Other keywords used in declarations are ReDim, Static, Public, Private, and Const.
See also
- Statements
- Writing a Sub procedure
- Declaring constants
- Declaring variables
- Visual Basic conceptual topics
Support and feedback
Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.