嗨我的代码正在努力工作新的一年然后我在想它何时必须通过使用字符串而不是日期对象来比较2016年12月和1月1日我遇到了问题。这是正确的还是我正在使用的逻辑,是否有更好的方法来做到这一点?
Dim MAX_DATE As String
MAX_DATE = Format(Now, "mmddyy")
Dim MIN_DATE As String
MIN_DATE = Format(Now - 20, "mmddyy")
If Not objP.CollDateFormat >= MIN_DATE And objP.CollDateFormat <= MAX_DATE Then
Dim szMessage As String
szMessage = "Collection Date is in the past"
Exit Function
End If
答案 0 :(得分:0)
这将比较日期是否在20天前至今的一天内。
Option Explicit
Sub ff()
Dim SomeDate As Date
'Here you will insert the code for the actual date
'or just change the SomeDate text in the if statement to you objP.CollDateFormat
SomeDate = DateSerial(2016, 12, 30)
Dim MAX_DATE As Date
MAX_DATE = Now
Dim MIN_DATE As Date
MIN_DATE = Now - TimeSerial(480, 0, 0) '480 hours is 20 days
If Not SomeDate >= MIN_DATE And SomeDate <= MAX_DATE Then
Dim szMessage As String
szMessage = "Collection Date is in the past"
'Exit Function
End If
End Sub