Part | Description | |
Public | Optional. Indicates that the Sub procedure is accessible to all other procedures in all modules. If used in a module that contains an Option Private statement, the procedure is not available outside the project. | |
Private | Optional. Indicates that the Sub procedure is accessible only to other procedures in the module where it is declared. | |
Static | Optional. Indicates that the Sub procedure's local variables are preserved between calls. The Static attribute doesn't affect variables that are declared outside the Sub, even if they are used in the procedure. | |
name | Required. Name of the Sub; follows standard variable naming conventions. | |
arglist | Optional. List of variables representing arguments that are passed to the Sub procedure when it is called. Multiple variables are separated by commas. | |
statements | Optional. Any group of statements to be executed within the Sub procedure. |
Part | Description | |
Optional | Optional. Keyword indicating that an argument is not required. If used, all subsequent arguments in arglist must also be optional and declared using the Optional keyword. Optional can't be used for any argument if ParamArray is used. | |
ByVal | Optional. Indicates that the argument is passed by value. | |
ByRef | Optional. Indicates that the argument is passed by reference. ByRef is the default in Visual Basic. | |
ParamArray | Optional. Used only as the last argument in arglist to indicate that the final argument is an Optional array of Variant elements. The ParamArray keyword allows you to provide an arbitrary number of arguments. ParamArray can't be used with ByVal, ByRef, or Optional. | |
varname | Required. Name of the variable representing the argument; follows standard variable naming conventions. | |
type | Optional. Data type of the argument passed to the procedure; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Decimal (not currently supported), Date, String (variable-length only), Object, Variant. If the parameter is not Optional, a user-defined type, or an object type may also be specified. | |
defaultvalue | Optional. Any constant or constant expression. Valid for Optional parameters only. If the type is an Object, an explicit default value can only be Nothing. |
Private Sub Command0_Click
End Sub
You can then enter the code that you want to execute when that button's Click event occurs.
Example
This example uses the Sub statement to define the name, arguments, and code that form the body of a Sub procedure.
' Sub procedure definition.
' Sub procedure with two arguments.
Sub SubComputeArea(Length, TheWidth)
Dim Area As Double ' Declare local variable.
If Length = 0 Or TheWidth = 0 Then
' If either argument = 0.
Exit Sub ' Exit Sub immediately.
End If
Area = Length * TheWidth ' Calculate area of rectangle.
Debug.Print Area ' Print Area to Debug window.
End Sub