Sub Statement

See AlsoCD5IPK                 Example3XXR0UG>Low

Declares the name, arguments, and code that form the body of a Sub procedureQDBVHN.


[Static] [Private] Sub subname [(argumentlist)]


          [Exit Sub]


End Sub


All executable code must be in Sub or Function proceduresK6LBMC.  You can't define a Sub procedure inside another Sub or Function procedure.

The Sub statement has these parts:

Part                 Description


Static               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 procedure, even if they are used in the procedure.

Private             Indicates that the Sub procedure is accessible only to other procedures in the module2JU32VW in which it exists.  No other procedure in any other module has access to it.  The Private keyword has no effect when declaring Sub procedures in a form since they are not available outside the form anyway.

Sub                  Marks the beginning of a Sub procedure.

subname           Name of the procedure.  Sub procedure names follow the same rules that constrain the names of other variables but cannot include a type-declaration character4TVC9NP.  Because Sub names are recognized by all procedures in all modules, subname cannot be the same as any other globally recognized name in the program.  Such names include the names of procedures (in Visual Basic or declared dynamic-link libraryDEFDLL [DLL]) procedures, Global variables, and Global constantsTD9WSV.  To avoid a name conflict, you can use the Private reserved word43US84 to make the Sub local to the module in which it appears, but access to it will not be available from any procedure in any other module.  Even if you use the Private reserved word, subname cannot be the same as any other module-level variable4OG2C0B, constant, or procedure name in the same module.

argumentlist      List of variables, representing arguments, that are passed to the Sub procedure when it is called.  Multiple variables are separated by commas.  Unless identified with the ByVal reserved word, arguments are passed by reference, so changing an argument's value inside the Sub procedure changes its value in the calling procedure.

statementblock  Any group of statements that are executed within the body of the Sub procedure.

Exit Sub           Causes an immediate exit from a Sub procedure.  Program execution continues with the statement following the statement that called the Sub procedure.  Any number of Exit Sub statements can appear anywhere in a Sub procedure.

End Sub           Marks the end of a Sub procedure.


The argument argumentlist has this syntax:

          [ByVal] variable[( )] [As type] [, [ByVal]variable[( )] [ As type] ] . . .

It has these parts:

Part                 Description


ByVal               Indicates the argument is passed by value rather than by reference.  The ByVal reserved word cannot be used with a variable of a user-defined type, an object type, or a variable that is an array.

variable            Name of the variable to pass as an argument.  For arrayYPCGZO variables, use the parentheses but omit the number of dimensions.

As type            Declares the data type3GYXY7 of variable.  The type may be Integer, Long, Single, Double, Currency, String (variable-length strings only), Variant8PHEAW3, a user-defined type, or an object type9DGMAH.  Use a separate As type clause for each argument.


Like a Function procedure, a Sub procedure is a separate procedure that can take arguments, perform a series of statements, and change the value of its arguments.  However, unlike a Function procedure, which returns a value, a Sub procedure can't be used in an expression.

You can call a Sub procedure using the procedure name followed by the argument list.  See the Call statement for specific information on how to call Sub procedures.


Caution          Sub procedures can be recursive; that is, they can call themselves to perform a given task.  However, recursion can lead to stack overflow. The Static reserved word usually is not used with recursive Sub procedures.


Variables used in Sub procedures fall into two categories: those that are explicitly declared within the procedure and those that are not.  Variables that are explicitly declared in a procedure (using Dim or the equivalent) are always local to the procedure.  Other variables used but not explicitly declared in a procedure are also local unless they are explicitly declared at some higher level outside the procedure.


Caution          A procedure can use a variable that is not explicitly declared in the procedure, but a name conflict can occur if anything you have defined in the Declarations section has the same name.  If your procedure refers to an undeclared variable that has the same name as another procedure, a Global or module-level constant or variable, or an object, Visual Basic assumes that your procedure is referring to that module-level name.  Explicitly declare variables to avoid this kind of conflict.  You can use an Option Explicit statement to force explicit declaration of variables.



Note   You can't use GoSub, GoTo, or Return to enter or exit a Sub procedure.


See Also

Call Statement4K6MQKK

Dim StatementLANDIM

Function StatementEZ33BF

Global StatementTDN897

Option Explicit StatementIJUJAQ

Static StatementCC4JTI

Sub Statement Example

The example uses the Sub statement to declare the name of two Sub procedures.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.


Sub Form_Click ()

  Dim A, B                               ' Declare local variables.

  A = InputBox("What is the length?")    ' Ask for length of rectangle.

  B = InputBox("What is the width?")     ' Ask for width of rectangle.

  SubDemo A, B                           ' Call Sub with arguments.

End Sub

Sub SubDemo(RLen, RWid)                  ' Sub with two arguments.

  Dim Area                               ' Declare local variable.

  Area = RLen * RWid                     ' Calculate area of rectangle.

  MsgBox "Total area is " & Area         ' Display result.

End Sub