尝试创建一个函数来比较VBA中的两个日期(需要日期和订单日期), 如果要求的日期早于订单日期,那么这应该会产生错误。
答案 0 :(得分:3)
继我的第一个评论之后,这里有四个日期比较的例子
'~~> Direct Date Comparision
Sub Sample1()
Dim dt1 As Date, dt2 As Date
dt1 = #12/12/2014#
dt2 = #12/12/2013#
Debug.Print IsGreater(dt1, dt2)
End Sub
'~~> Converting string to date and directly comparing
Sub Sample2()
Dim dt1 As String, dt2 As String
dt1 = "12/12/2014"
dt2 = "12/12/2013"
Debug.Print IsGreater(CDate(dt1), CDate(dt2))
End Sub
'~~> Using DateDiff with direct date comparision
Sub Sample3()
Dim dt1 As Date, dt2 As Date
dt1 = #12/12/2014#
dt2 = #12/12/2013#
If DateDiff("d", dt2, dt1) > 0 Then
MsgBox "Greater"
Else
MsgBox "Smaller or Equal"
End If
End Sub
'~~> Using DateDiff with converting string to date and directly comparing
Sub Sample4()
Dim dt1 As Date, dt2 As Date
dt1 = "12/12/2014"
dt2 = "12/12/2013"
If DateDiff("d", CDate(dt2), CDate(dt1)) > 0 Then
MsgBox "Greater"
Else
MsgBox "Smaller or Equal"
End If
End Sub
Function IsGreater(d1 As Date, d2 As Date) As Boolean
IsGreater = d1 > d2
End Function
答案 1 :(得分:0)
试试这个
If dateRequired < orderDate Then
Debug.Print "this should generate an error."
End If
答案 2 :(得分:0)
Dim orderdate As Date
Dim earlydate As Date
orderdate = InputBox(“输入orderdate”)
earlydate =“12/12/2011”
如果orderdate&gt;提前日期
MsgBox "Error"
结束如果