【发布时间】:2017-06-28 17:20:04
【问题描述】:
所以这让我发疯了,它应该非常容易做到。我不知道为什么我不能让它工作。 :( 目前我的函数正在计算开始日期和今天日期之间的差异,它工作得很好,但现在我需要实现一个可选的结束日期。所以如果有可用的 EmpEndDate,则应该使用它,否则为今天的日期应该使用。
这是我的功能之一:
Public Function RetentionMonths(ByVal EmpStartDate As Date) As Integer
Dim RetentionStartdate As Date
Dim FollowingMonth As Date
RetentionStartdate = DateAdd("ww", 14, DateAdd("d", vbSaturday - Weekday(EmpStartDate), EmpStartDate))
FollowingMonth = DateSerial(Year(RetentionStartdate), Month(RetentionStartdate) + 1, 1)
RetentionMonths = Months(FollowingMonth, Date)
End Function
编辑:Months() 函数
Public Function Months( _
ByVal datDate1 As Date, _
ByVal datDate2 As Date, _
Optional ByVal booLinear As Boolean) _
As Integer
Dim intDiff As Integer
Dim intSign As Integer
Dim intMonths As Integer
intMonths = DateDiff("m", datDate1, datDate2)
If DateDiff("d", datDate1, datDate2) > 0 Then
intSign = Sgn(DateDiff("d", DateAdd("m", intMonths, datDate1), datDate2))
intDiff = Abs(intSign < 0)
Else
intSign = Sgn(DateDiff("d", DateAdd("m", -intMonths, datDate2), datDate1))
If intSign <> 0 Then
intDiff = Abs(booLinear)
End If
intDiff = intDiff - Abs(intSign < 0)
End If
Months = intMonths - intDiff
End Function
我尝试解决这个问题的一种方法:
Public Function RetentionMonths(ByVal EmpStartDate As Date, _
Optional ByVal EmpEndDate As Date) _
As Integer
Dim RetentionStartdate As Date
Dim FollowingMonth As Date
RetentionStartdate = DateAdd("ww", 14, DateAdd("d", vbSaturday - Weekday(EmpStartDate), EmpStartDate))
FollowingMonth = DateSerial(Year(RetentionStartdate), Month(RetentionStartdate) + 1, 1)
RetentionMonths = Months(FollowingMonth, Nz(EmpEndDate, Date))
End Function
我要么在过去有用的字段中得到疯狂的负数,要么#Name?错误。
【问题讨论】:
-
“休息”是什么意思?你得到什么错误? EmpEndDate 未在任何表达式中引用。也许您需要使用 Nz() 函数。你有一个自定义函数 Months()?内在函数是 Month()。
-
我没有看到你在你提供的代码中的任何地方声明 EmpEndDate 那么这个变量是从哪里来的?
-
@Sorceri 它也没有在函数的任何地方引用。也许OP的意思是把它作为参数添加?
-
对于没有提供足够的信息,我深表歉意。我已经更新了我的问题。