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

Share your thoughts/feedback

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s