Question

I have a VBA function called getYear that is initialized as follows :

Public Function getYear(ByVal year As Date, Optional ByVal month As Date) As Long

Then, inside the function I have :

msg = MsgBox(IsEmpty(month), vbOKOnly) So I know whether or not the month has been input in the function.

The only thing is whether I have a function call such as : getYear(currentYear) or getYear(currentYear, currentMonth) isEmpty is always returning False

If I am understanding this correctly when I call getYear(currentYear) isEmpty() should return True, but it is not...

I gather that is from my function initialization, but I don't know what else to do.

Any help is appreciated!

Was it helpful?

Solution

From MSDN:

IsEmpty only returns meaningful information for variants

You can also use IsMissing() to check if an optional argument was passed to the function, but this works only on Variants as well. You might want to check if the optional argument has a value equal to his default value, which is zero for dates.

OTHER TIPS

You can create a default value for "month" that is zero or negative, or out of the range of possible entries. Then instead of using IsMissing or IsEmpty, you can just test for that default value. If month = the default, you know that parameter was not passed to the function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top