Posts Tagged ‘Optional Argument’

If the argument is missing: VBA

Posted: May 1, 2013 by Theodoulus in VBA
Tags: ,

If you have a VBA function written on your code and it has some parameters.
Suppose one of your parameter is optional and you want to check that while calling the function the user is providing the optional argument : Use IsMissing()

The function IsMissing returns a boolean (True/False) which says whether the values for the parameter is given or not.

Function Avg(num1, num2, Optional num3)
    Dim totalNums As Integer
    totalNums = 3
    If IsMissing(num3)  Then
            num3 = 0
            totalNums = totalNums - 1
    End If
    Avg = (num1 + num2 + num3) / totalNums
End Function

Sub avgSub()
           MsgBox Avg(2, 3)
           MsgBox Avg(2, 3, 5)
End Sub