Example 1: Function Grade (Marks As Integer) As String
Example 1: Function Grade (Marks As Integer) As String
End Function
Sub callGrade()
Dim i As Integer
Dim str As String
'call the Grade function and the result is assgined to the local variable str:
str = Grade(i)
End Sub
Example 2
End Function
Sub callMarksGrades()
'call the marksPercent function and the result is assgined to the local variable dPcnt:
dPcnt = marksPercent(Marks, TotalMarks)
End Sub
When you pass an argument by value in a procedure, only a copy of a variable is passed and
any change in the value of the variable in the current procedure will not affect or change the
variable in its original location because the variable itself is not accessed. To pass an
argument by value, use the ByVal keyword before the argument to its left.
Example 3
End Function
Sub salesmanPeter()
'call the computeCommission function and the result is assgined to the local variable:
commPeter = computeCommission(commRate, Sales)
When you pass an argument by reference in a procedure, the variable itself is accessed by the
procedure in its location or address in memory. The value of the variable is changed
permanently by the procedure in this case. To pass an argument by reference, use the ByRef
keyword before the argument to its left. Passing arguments by reference is also the default in
vba, unless you explicity specify to pass an argument by value.
i=5
number = i
End Function
Sub chkNumber()
'message returned is 0, because after the number function ends the value of the variable n
reverts to the value when it was declared where it was set to 0:
MsgBox n
End Sub
i=5
number1 = i
End Function
Sub chkNumber1()
'message returned is 5 because calling the number1 function, assigns value to the variable n:
MsgBox number1(n)
'message returned is 5, because the variable has been passed by reference in the number1
function, and the variable n has permanently assumed the new assigned value by calling the
number1 function in the preceding line of code:
MsgBox n
End Sub
Optional Arguments
Arguments can be specified as Optional by using the Optional keyword before the argument
to its left. When you specify an argument as Optional, all other arguments following that
argument to its right must also be specified as Optional. Note that specifying the Optional
keyword makes an argument optional otherwise the argument will be required.
The Optional argument should be (though not necessary) declared as Variant data type to
enable use of the IsMissing function which works only when used with variables declared as
Variant data type. The IsMissing function is used to determine whether the optional argument
was passed in the procedure or not and then you can adjust your code accordingly without
returning an error. If the Optional argument is not declared as Variant in which case the
IsMissing function will not work, the Optional argument will be assigned the default value
for its data type which is 0 for numeric data type variables (viz. Integer, Double, etc) and
Nothing (a null reference) for String or Object data type variables.
End Sub
Sub getFullName1()
End Sub
ActiveSheet.Range("A1") = firstName
ActiveSheet.Range("B1") = secondName
End Sub
Sub getFullName2()
Call employeeName2(strName1)
End Sub
Example 7 - declare the Optional argument as Variant data type, and use the
IsMissing function.
If IsMissing(secondNumber) Then
dResult = firstNumber
Else
dResult = firstNumber / secondNumber
End If
MsgBox dResult
End Sub
Sub getDivide1()
Call divide1(iNumber1)
End Sub
If IsMissing(secondNumber) Then
dResult = firstNumber
Else
dResult = firstNumber / secondNumber
End If
MsgBox dResult
End Sub
Sub getDivide2()
Call divide2(iNumber1)
End Sub
If IsMissing(secondNumber) Then
dResult = firstNumber
Else
dResult = firstNumber / secondNumber
End If
MsgBox dResult
End Sub
Sub getDivide3()
Call divide3(iNumber1)
End Sub
You can specify a Default value for an Optional argument which will be used if the Optional
argument is not passed to the procedure. This way you can declare optional arguments of any
data type and specify a default value which will be used if the Optional argument is omitted,
obviating the use of the IsMissing function which works only with the Variant data type.
MsgBox dResult
End Sub
Sub getCalculation3()
Call divide(iNumber1)
End Sub
Pass an Arbitrary or Indefinite Number of Arguments - Parameter Arrays
(ParamArray)
A procedure uses information in the form of variables, constants & expressions to perform
actions whenever it is called. The procedure's declaration defines a parameter which allows
the calling code (code which calls the procedure) to pass an argument or value to that
parameter so that every time the procedure is called the calling code may pass a different
argument to the same parameter. A Parameter is declared like a variable by specifying its
name and data type. By declaring a parameter array, a procedure can accept an array of
values for a parameter. A parameter array is so defined by using the ParamArray keyword.
Only one ParamArray can be defined in a procedure and it is always the last parameter in the
parameter list. A ParamArray is an optional parameter and it can be the only optional
parameter in a procedure and all parameters preceding it must be required. ParamArray
should be declared as an array of Variant data type. Irrespective of the Option Base setting
for the module, LBound of a ParamArray will always be 0 ie. index values for the array will
start from 0. ByVal, ByRef or Optional keywrods cannot be used with ParamArray.
To access a parameter array's value: use the UBound function to determine the array length
which will give you the number of elements or index values in the array. In the procedure
code you can access a parameter array's value by typing the array name followed by an index
value (which should be between 0 and UBound value) in parantheses
MsgBox lSum
End Sub
Sub getAddNums()
End Sub
'Commission for each element of the ParamArray is computed and added here:
For Each v In sales
MsgBox v
dTotalComm = dTotalComm + comm * v
Next v
MsgBox dTotalComm
End Sub
Sub getComm()
Dim d As Double
d = 0.3
'you can pass an arbitrary or indefinite number of arguments in a procedure using
ParamArray:
Call calcComm(d, 100, 200, 300)
End Sub
Example 13 - Two arguments are required and then allow to pass an arbitrary
number of arguments to the procedure using a ParamArray parameter.
If Len(strAllGrades) = 0 Then
strAllGrades = strGrade
Else
strAllGrades = strAllGrades & ", " & strGrade
End If
Next v
MsgBox student & " has grades of " & strAllGrades & " and average marks of " & avg
End Sub
Sub getAvgMarksGrades()
strName = "Peter"
End Sub