Why an optional argument returns "Missing" in a user defined function (UDF) instead of empty?

makiwara

Board Regular
Joined
Mar 8, 2018
Messages
171
Question1: The default value of a variant is empty, so why not empty in a UDF, if I don't define it?
Question2: So I test every datatype by its default value to see if it has been given or not, except variant, where I use IsMissing instead of empty. Is this correct?

Thank you very much! Have a nice day!
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
There is a difference between supplying an empty variant, and not supplying anything.
 
Upvote 0
Solution
Function Sum2 (optional Number as variant) as variant

If IsEmpty(Number) then
msgbox "empty"
end if

End function

This throws error, I have to test it with IsMissing(Number), empty does not work. Optional variant status is missing right at the beginning of the function.
 
Upvote 0
It seems that when passing an optional variant argument , the argument is given a default value pointing to some error value "Error" meaning that the optional variant argument is actually not Empty that's why IsEmpty(Argument) returns False.
VBA Code:
Sub Func(Optional ByVal arg As Variant)
    MsgBox TypeName(arg)  '<== returns Error
End Sub

Sub Test()
    Call Func
End Sub

It seems from the above that IsError can be used as an alternative to IsMissing

VBA Code:
Sub Func(Optional ByVal arg As Variant)
    If IsError(arg) Then
        MsgBox "IsError - argument not passed"
    End If
    If IsMissing(arg) Then
        MsgBox " IsMissing - argument not passed"
    End If
End Sub

Sub Test()
    Call Func
End Sub

Why an optional variant argument is given the Error default value while a non initialized variant variable is given the Empty value , I don't know.
 
Upvote 0
But that would also be triggered by you passing an error value, rather than not passing a value at all. I think that the value Missing is a specific error value, and should therefore be tested for using IsMissing.
 
Upvote 0
It seems that when passing an optional variant argument , the argument is given a default value pointing to some error value "Error" meaning that the optional variant argument is actually not Empty that's why IsEmpty(Argument) returns False.
VBA Code:
Sub Func(Optional ByVal arg As Variant)
    MsgBox TypeName(arg)  '<== returns Error
End Sub

Sub Test()
    Call Func
End Sub

It seems from the above that IsError can be used as an alternative to IsMissing

VBA Code:
Sub Func(Optional ByVal arg As Variant)
    If IsError(arg) Then
        MsgBox "IsError - argument not passed"
    End If
    If IsMissing(arg) Then
        MsgBox " IsMissing - argument not passed"
    End If
End Sub

Sub Test()
    Call Func
End Sub

Why an optional variant argument is given the Error default value while a non initialized variant variable is given the Empty value , I don't know.
Thank you very much for both of you, for the explanation.
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,913
Members
449,093
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top