Microsoft Office Tutorials and References
In Depth Information
The syntax to define a Function is similar:
Function functionname argumentlist ) As type (
...
‘ Code is placed here
functionname = returnvalue
End Function
For both types of procedures, the name must follow VBA variable naming rules and also must be
unique within the module where the procedure is located. argumentlist is an optional list of
data passed to the procedure when it is called (as explained in the next section). For a function, the
As type clause specifies the data type of the function’s return value, and the functionname =
returnvalue statement sets the value of that return value.
Passing Arguments to Procedures
Many procedures have arguments that permit data to be passed to the procedure when it is called.
The argument list can include as many arguments as are needed, and has the following syntax:
argname1 As type , argname2 As type , .....
Here’s an example of a function procedure that is passed three numbers and returns the largest one:
Function Largest(n1 as Double, n2 As Double, n3 As Double) _
As Double
Dim temp as Double
If n1 > n2 Then
temp = n1
Else
temp = n2
End If
If n3 > temp Then
Largest = n3
Else
Largest = temp
End If
End Function
Each argument can be any of VBA’s data types, including UDTs, arrays, or enumerations. To specify
an array argument, use an empty set of parentheses:
Sub ProcessArray(array() As String)
...
End Sub
Search JabSto ::




Custom Search