1

我有一个函数可以找到下一个星期一的日期。当没有传递可选的日期参数时,它会默认为零。我想我已经包含了解释问题的评论

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
4

3 回答 3

4

VBA 变量通常具有默认值,并且任何可选参数都会自动设置为其默认值,因此这种行为是意料之中的。既然您已经知道如果未通过可选参数将默认为 0 (1/1/1900),为什么不只测试该值而不是测试是否传入了日期?

另一方面,如果您认为有人可能需要传入 1/1/1900,那么您应该将可选参数设置为Variant类型。变量在可选传入时不会被初始化,因此它不会有默认值(与日期不同)。

于 2013-08-28T21:13:16.303 回答
1

日期不能为空,因此在这种情况下可能使用变体

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
于 2013-08-28T21:11:20.540 回答
1

我知道有一个公认的答案,但我迟到了,但是......

也许在函数声明中提供一个默认日期并对其进行测试:

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

如果 1592 年 12 月 31 日没有充分超出预期范围,您可以提前或推迟,直到您开始达到日期可以处理的限制 - 快速测试显示 Excel 2010 可以采用 #1/1/100 的日期# 和 #1/1/9999#

于 2015-02-27T15:23:49.100 回答