Sub()

Declare the name, arguments, and code that form the body of a Sub procedure.

Syntax
      [Public [Default] | Private] Sub Name [(arglist)]
         [statements]
         [Exit Sub]
         [statements]
      End Sub


Public Indicates that the Sub procedure is accessible to all other procedures in all scripts.

Default Used only with the Public keyword in a Class block to indicate that the Sub procedure is the default method for the class. An error occurs if more than one Default procedure is specified in a class.

Private Indicates that the Sub procedure is accessible only to other procedures in the script where it is declared.

name Name of the Sub; follows standard variable naming conventions.

arglist List of variables representing arguments that are passed to the Sub procedure when it is called. Commas separate multiple variables.

statements Any group of statements to be executed within the body of the Sub procedure.

The arglist argument has the following syntax and parts: [ByVal | ByRef] varname[( )]

ByVal Indicates that the argument is passed by value.
ByRef Indicates that the argument is passed by reference. If ByVal and ByRef are omitted, the default is ByRef.
varname Name of the variable representing the argument; follows standard variable naming conventions.

Call a Sub procedure by using the procedure name followed by the argument list.

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.A Sub procedure does not return a value, so it cannot be used in an expression.

If not explicitly specified using either Public or Private, Sub procedures are public by default.

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

Examples

Demo 64, 6

Sub Demo(value1, value2)
    Dim intAnswer

    intAnswer = value1 + value2
    MsgBox "The sum is:  " & intAnswer
End Sub

“A man practices the art of adventure when he breaks the chain of routine and renews his life through reading new books, traveling to new places, making new friends, taking up new hobbies and adopting new viewpoints” ~ Wilfred Peterson

Related:

Exit - Exit a subroutine.
Function - Define a function procedure.


 
Copyright © 1999-2024 SS64.com
Some rights reserved