比较TextBox / Userform

时间:2018-06-18 12:14:55

标签: vba excel-vba excel

我需要确保用户不会输入任何可能早于'fromdate'的日期。问题是用户还应该能够将一个特定文本(“UFN”)写入第二个文本框(validitydate1)。

例如,当用户输入的日期晚于第一个文本框(validitydate)时,他们可以转到下一页,但如果他们输入的日期早于他们输入第一个文本框的日期,则他们不应该除此之外,他们也应该能够将(“UFN”)写入第二个文本框。

当我输入更新日期并将“UFN”写入todate时,我收到相同的msgbox错误。

如果我将“UFN”写入“todate”并且没有收到此msgbox错误,是否有办法转到下一页?

Private Sub GoToNext_Click()


Dim fromdate As Date
Dim todate As Date


On Error Resume Next
fromdate = Me.ValidityDate.value
todate = Me.ValidityDate1.value

If todate < fromdate Then
MsgBox "Check the validity dates please!", vbExclamation
Exit Sub
End If

2 个答案:

答案 0 :(得分:1)

这是一种方法,因为如果输入“UFN”,则无法检查日期比较:

If Me.ValidityDate1.Value <> "UFN" Then

    Dim fromdate As Date
    Dim todate As Date

    fromdate = Me.ValidityDate.Value
    todate = Me.ValidityDate1.Value

    If todate < fromdate Then
        MsgBox "Check the validity dates please!", vbExclamation
        Exit Sub
    End If

End If

答案 1 :(得分:0)

试试这段代码:

Private Sub GoToNext_Click()
    Dim fromdate As Date
    Dim todate As Date

    On Error GoTo errorhandler
    fromdate = CDate(Me.ValidityDate.Value)
    todate = CDate(Me.ValidityDate1.Value)

    If DateDiff("d", todate, fromdate) < 0 Then
        GoTo errorhandler
    End If
    Exit Sub
errorhandler:
    MsgBox "Check the validity dates please!", vbExclamation
End Sub
相关问题