-1

尝试创建一个函数来比较 VBA 中的两个日期(所需日期和订单日期),如果所需日期早于订单日期,那么这应该会产生错误。

4

3 回答 3

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
于 2013-02-18T17:56:49.787 回答
0

将订单日期作为日期

较早日期为日期

orderdate = InputBox("请输入订单日期")

较早日期 = “12 / 12 / 2011”

如果 orderdate > earlydate 则

  MsgBox "Error"

万一

于 2013-02-19T09:52:03.603 回答
0

尝试这个

If dateRequired < orderDate Then
    Debug.Print "this should generate an error."
End If
于 2013-02-18T17:46:10.087 回答