Do…Loop Uses a counter to run statements a specified number of times.
While…. Wend Repeats a group of statements for each item in a collection or each element of an array.
For….Next Loops while a condition is True.
You're Reading a Preview
Unlock full access with a free trial.
Sub Procedures
Sub Procedures
A Sub procedure is a series of VBScript statements (enclosed by Sub and End Sub
A Sub procedure is a series of VBScript statements (enclosed by Sub and End Sub
statements) that perform actions but don't return a value. A Sub procedure can take statements) that perform actions but don't return a value. A Sub procedure can take
arguments (constants, variables, or expressions that are passed by a calling procedure). If a
arguments (constants, variables, or expressions that are passed by a calling procedure). If a
Sub procedure has no arguments, its Sub statement must include an empty set of parentheses
Sub procedure has no arguments, its Sub statement must include an empty set of parentheses
().
().
The following Sub procedure uses two intrinsic, or built-in, VBScript functions,
The following Sub procedure uses two intrinsic, or built-in, VBScript functions, MsgBoxMsgBox
and
and InputBoxInputBox, to prompt a user for information. It then displays the results of a calculation, to prompt a user for information. It then displays the results of a calculation
based on that
based on that information. The calculation is performed in a information. The calculation is performed in a Function procedure created usingFunction procedure created using
VBScript. The Function procedure is shown after the following discussion.
VBScript. The Function procedure is shown after the following discussion.
Sub ConvertTemp()
Sub ConvertTemp()
temp = InputBox("Please enter the temperature in degrees F.", 1)
temp = InputBox("Please enter the temperature in degrees F.", 1)
MsgBox "The temperature is " & Celsius(temp) & " degrees C."
MsgBox "The temperature is " & Celsius(temp) & " degrees C."
End Sub
Function Procedures
Function Procedures
A Function procedure is a
A Function procedure is a series of VBScript statements enclosed by the series of VBScript statements enclosed by the Function andFunction and
End Function statements. A Function procedure is similar to a Sub procedure, but can also
End Function statements. A Function procedure is similar to a Sub procedure, but can also
return a value. A
return a value. A Function procedure can take arguments (constants, variables, or expressionsFunction procedure can take arguments (constants, variables, or expressions
that are passed to it by a calling procedure). If a Function procedure has no arguments, its
that are passed to it by a calling procedure). If a Function procedure has no arguments, its
Function statement must include an empty set of parentheses. A Function returns a value by
Function statement must include an empty set of parentheses. A Function returns a value by
assigning a value to its name in one or more statements of the procedure. The return type of a
assigning a value to its name in one or more statements of the procedure. The return type of a
Function is always a Variant.
Function is always a Variant.
In the following example, the Celsius function calculates degrees Celsius from degrees
In the following example, the Celsius function calculates degrees Celsius from degrees
Fahrenheit. When the function is called
Fahrenheit. When the function is called from the ConvertTemp Sub procedure, a variablefrom the ConvertTemp Sub procedure, a variable
containing the argument value is passed to the function. The result of the calculation is
containing the argument value is passed to the function. The result of the calculation is
returned to the calling procedure and displayed in a message box.
returned to the calling procedure and displayed in a message box.
Sub ConvertTemp()
Sub ConvertTemp()
temp = InputBox("Please enter the temperature in degrees F.", 1)
temp = InputBox("Please enter the temperature in degrees F.", 1)
MsgBox "The temperatu
MsgBox "The temperature is " & re is " & Celsius(temp) & " degrees C."Celsius(temp) & " degrees C."
End Sub End Sub Function Celsius(fDegrees) Function Celsius(fDegrees) Celsius = (fDegrees - 32) * 5 / 9 Celsius = (fDegrees - 32) * 5 / 9 End Function End Function
Getting Data into and out of Procedures
Getting Data into and out of Procedures
Each piece of data is passed into your procedures using an argument . Arguments serve
Each piece of data is passed into your procedures using an argument . Arguments serve
as placeholders for the data you want to
as placeholders for the data you want to pass into your procedure. You can name yourpass into your procedure. You can name your
arguments any valid variable name. When you create a procedure using either the Sub
arguments any valid variable name. When you create a procedure using either the Sub
statement or the Function statement, parentheses must be included
statement or the Function statement, parentheses must be included after the name of theafter the name of the
procedure. Any arguments are placed inside these parentheses, separated by commas. For
procedure. Any arguments are placed inside these parentheses, separated by commas. For
example, in the following example, fDegrees is a placeholder for the value being passed into
example, in the following example, fDegrees is a placeholder for the value being passed into
the Celsius function for conversion.
the Celsius function for conversion.
Function Celsius(fDegrees) Function Celsius(fDegrees) Celsius = (fDegrees - 32) * 5 / 9 Celsius = (fDegrees - 32) * 5 / 9 End Function End Function
To get data out of a
To get data out of a procedure, you must use a Function. Remember, a Functionprocedure, you must use a Function. Remember, a Function
procedure can return a value; a Sub procedure can't.
Using Sub and Function Procedures in Code
A Function in your code must always be used on the right side of a variable assignment
or in an expression. For example:
Temp = Celsius(fDegrees) Or
MsgBox "The Celsius temperature is " & Celsius(fDegrees) & " degrees."
To call a Sub procedure from another procedure, type the name of the procedure along
with values for any required arguments, each separated by a comma. The Call statement is not required, but if you do use it, you must enclose any arguments in parentheses.
The following example shows two calls to the MyProc procedure. One uses the Call statement in the code; the other doesn't. Both do exactly the same thing.
Call MyProc(firstarg, secondarg) MyProc firstarg, secondarg
When you call a subroutine or function, each argument can be passed by reference or by value. When an argument is passed by reference, the called procedure can change the value of the variable. The change persists after the procedure is called. When an argument is passed
by value, any changes that the called procedure makes to the value of the variable do not
persist after the procedure is called.
In a Sub or Function declaration, each parameter can be specified as ByRef or ByVal. If neither is specified, the default is ByRef.
If ByVal is specified, the corresponding argument is always passed by value when the subroutine is called.
If ByRef (or neither) is specified, the argument can be passed by reference or by value
when the subroutine is called. The argument is passed by value if it is enclosed in parentheses, and if the parentheses do not apply to the parameter list. The argument is also passed by
value if the variable sent as an argument is in a class. Otherwise, it is passed by reference. The following table summarizes this.
Keyword specified Argument is passed by
ByVal Value.
Specifying ByRef and ByVal
In the following example, the ByRef keyword is specified for the MyParam variable. When the subroutine is called, a reference is passed to the variable MyArg. When the
subroutine changes the value of MyParam, this also changes the value of MyArg. The local variable MyParam becomes an alias for MyArg.
Sub TestSub(ByRef MyParam)
MyParam = 5
End Sub
Dim MyArg MyArg = 123
TestSub MyArg '
In the following example, the ByVal keyword is used. Therefore, the argument is passed
by value. The subroutine changes the value of MyParam. However, the value of MyArg
remains unchanged.
Sub TestSub(ByVal MyParam) MyParam = 5 End Sub Dim MyArg MyArg = 123 TestSub MyArg ' MyArg is still 123.
You're Reading a Preview
Unlock full access with a free trial.
ByRef Parameters Passed by Value
If the parameter is specified as ByRef, the argument can still be passed by value. This
occurs if one of the following is true:
The argument is in parentheses, and the parentheses do not apply to the argument list.
The variable sent as an argument is in a class.
The argument being passed is not a variable, for example, 12. Argument in Parentheses
If the parameter is specified as ByRef, the argument can be passed by value if it is
enclosed in parentheses. This is shown in the following example. Sub TestSub(ByRef MyParam)
MyParam = 5 End Sub Dim MyArg MyArg = 123 TestSub (MyArg) ' MyArg is still 123.
You're Reading a Preview
Unlock full access with a free trial.
TestFunction (MyArg) ' MyArg is still 123.
Result = TestFunction (MyArg)
' MyArg is changed in TestSub to 5.
In the following example, a Call keyword is used. The parentheses that enclose the argument list do not cause the ByRef parameter to be passed by value.
Sub TestSub(ByRef MyParam)
MyParam = 5 End Sub
Dim MyArg MyArg = 123
Call TestSub ((MyArg))
' MyArg is still 123. Call TestSub (MyArg)
You're Reading a Preview
Unlock full access with a free trial.
Argument in a Class
If the parameter is specified as ByRef, the argument is passed by value if the variable
sent as an argument is in a class.
In the following example, the MyParam parameter is specified as ByRef. The method is called by using the cust.MyValue argument. Because cust.MyValue is inside a class, the
argument is passed by value. Class Customer
Public MyValue End Class
Sub TestMethod (ByRef MyParam) MyParam = 5
End Sub Dim cust
Set cust = New Customer cust.MyValue = 123
TestMethod cust.MyValue ' cust.MyValue is still 123.