How can i pass nothing or a blank cell to an Optional argument in VBA?

Posted by user2985990 on Stack Overflow See other posts from Stack Overflow or by user2985990
Published on 2013-11-13T03:52:13Z Indexed on 2013/11/13 3:52 UTC
Read the original article Hit count: 133

Filed under:
|
|

I am trying to set up a function so that whether I pass a blank cell or do not even select a cell for the argument it returns the function I am looking for. Here is my code:

Function FinancialsAge(FirstBirthday As Date, BeginningDate As Date, Optional Second Birthday As Variant) As String

If IsMissing(SecondBirthday) = True Or SecondBirthday = vbNullString Then
    FinancialsAge = Year(BeginningDate - FirstBirthday) - 1900
ElseIf SecondBirthday Then
   FinancialsAge = (Year(BeginningDate - FirstBirthday) - 1900) & "/" & (Year(BeginningDate - SecondBirthday) - 1900)
End If

End Function

This code works fine as long as I select a blank cell for the third argument but when I leave the third argument out I get a "#Value!" error in the cell. Anyway to do this in Excel VBA so that the function works under both circumstances?

Thanks,

© Stack Overflow or respective owner

Related posts about excel

Related posts about vba