سؤال

I have a function that finds the following Monday for a date. When no optional date parameter is passed it goes to a default of zero. I think I've included comments to explain problem

Function NextMondayFromADateOrToday(Optional StartDate As Date) As Date
' objective: if date param not supplied should set StartDate to today
' ??? Problem: with no arg provided StartDate is zero = date value of 1/1/1900
' ??? how to add default to optional paramater of date type?
' ??? or, how to check if an arg was provided if the parameter is date type?
If Not (IsDate(StartDate)) Then StartDate = Date
Select Case Weekday(StartDate)
Case 1:     NextMondayFromADateOrToday = StartDate + 1
Case 2:     NextMondayFromADateOrToday = StartDate + 0
Case 3:     NextMondayFromADateOrToday = StartDate + 6
Case 4:     NextMondayFromADateOrToday = StartDate + 5
Case 5:     NextMondayFromADateOrToday = StartDate + 4
Case 6:     NextMondayFromADateOrToday = StartDate + 3
Case 7:     NextMondayFromADateOrToday = StartDate + 2
End Select
End Function
هل كانت مفيدة؟

المحلول

VBA variables generally have a default value, and any optional parameters are automatically set to their default, so this behavior is expected. Since you already know that the optional parameter will default to 0 (1/1/1900) if not passed, why not just test for that value instead of testing to see if a date was passed in?

If, on the other hand, you think that someone might need to pass in 1/1/1900, then you should set the optional parameter as a Variant type. Variants are not initialized when passed in optionally, so it will not have a default value (unlike dates).

نصائح أخرى

Date cannot be null so perhaps use a variant in this case

Function NextMondayFromADateOrToday(Optional StartDate As Variant) As Date
' objective: if date param not supplied should set StartDate to today
' ??? Problem: with no arg provided StartDate is zero = date value of 1/1/1900
' ??? how to add default to optional paramater of date type?
' ??? or, how to check if an arg was provided if the parameter is date type?
If IsMissing(StartDate) Then StartDate = Date
Select Case Weekday(StartDate)
Case 1:     NextMondayFromADateOrToday = StartDate + 1
Case 2:     NextMondayFromADateOrToday = StartDate + 0
Case 3:     NextMondayFromADateOrToday = StartDate + 6
Case 4:     NextMondayFromADateOrToday = StartDate + 5
Case 5:     NextMondayFromADateOrToday = StartDate + 4
Case 6:     NextMondayFromADateOrToday = StartDate + 3
Case 7:     NextMondayFromADateOrToday = StartDate + 2
End Select
End Function

I know there's an accepted answer and that I'm late to the party, however...

Perhaps provide a default date in the function declaration and test for that:

Function NextMondayFromADateOrToday(Optional StartDate As Date = #12/31/1592#) As Date
' objective: if date param not supplied should set StartDate to today
' ??? Problem: with no arg provided StartDate is zero = date value of 1/1/1900
' ??? how to add default to optional paramater of date type?
' ??? or, how to check if an arg was provided if the parameter is date type?
  If StartDate = #12/31/1592# Then
    StartDate = Date
  End If
  'If Not (IsDate(StartDate)) Then StartDate = Date
  Select Case Weekday(StartDate)
    Case 1:     NextMondayFromADateOrToday = StartDate + 1
    Case 2:     NextMondayFromADateOrToday = StartDate + 0
    Case 3:     NextMondayFromADateOrToday = StartDate + 6
    Case 4:     NextMondayFromADateOrToday = StartDate + 5
    Case 5:     NextMondayFromADateOrToday = StartDate + 4
    Case 6:     NextMondayFromADateOrToday = StartDate + 3
    Case 7:     NextMondayFromADateOrToday = StartDate + 2
  End Select
End Function

If 12/31/1592 isn't sufficiently out of expected range, you could go earlier or later until you start hitting the limits of what a date can handle - a quick test shows Excel 2010 can take date of #1/1/100# and #1/1/9999#

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top