Sub (statement)

<< Click to Display Table of Contents >>

Sub (statement)

Sub Name [(ArgumentList)]

 [Dim Variable(s)]

 [Statements]

 [Exit Sub]

End Sub

Begins the definition of a subroutine.

Name is the name of the subroutine.

ArgumentList is a comma-separated list of parameter declarations (see below).

The subroutine definition is ended with the End Sub command.

The Exit Sub command can be used to exit a subroutine prematurely.

Declaring parameters

[ByVal | ByRef] Variable [As Type]

The keywords ByVal or ByRef (default value) are used to indicate whether the parameter is passed by value or by reference (see the section Passing parameters via ByRef or ByVal).

Type specifies the data type (String, Integer, Double, Long, Variant). Alternatively, the type can be indicated by appending a type suffix (e.g. % for Integer) to the variable name (see the section Data types).

See also: Call, Dim, Function

Example:

Sub Main

 Dim Var1 as String

 Var1 = "Hello"

 MsgBox "Test"

 Test Var1

 MsgBox Var1

End Sub

Sub Test(wvar1 as String)

 wvar1 = "Bye!"

End Sub