如何将输入框输入限制为仅限日期

时间:2022-01-18 16:03:17

I would like to implement a control in the code below to only allow dates with mm/dd/yyyy format and to prevent blanks from being entered in the input box. I've tried adding some code but keep running into errors. The first control to check if it's blank works, but the second if Not seems to just be skipped.

我想在下面的代码中实现一个控件,只允许使用mm / dd / yyyy格式的日期,并防止在输入框中输入空格。我尝试添加一些代码,但一直遇到错误。第一个控件检查它是否为空白,但第二个if Not似乎只是被跳过。

'Updates the report date located in report generation tab cell B8
Dim ws1 As Worksheet
Set ws1 = Sheets("Report Generation")
ReTry:
With ws1
    Dim rptdate As Variant
        rptdate = InputBox("Please Enter the Report Date *Must be EOM format mm/dd/yyyy*", "Enter Date")
        If rptdate = "" Then
        MsgBox ("You did not enter a date!"), vbCritical, Error
        GoTo ReTry
        If Not IsDate(rptdate) Then
        MsgBox ("You did not enter the correct format!"), vbCritical, Error
        GoTo ReTry
        End If
        Else
        Range("B8").Value = rptdate
        End If
End With

2 个解决方案

#1


3  

Your If statements are a little off for this. Here's a code that will remove the use of GoTo (which is best practice), and still loop correctly until you get the format you want.

你的If声明对此有点偏僻。这是一个代码,它将删除GoTo的使用(这是最佳实践),并且仍然正确循环,直到您获得所需的格式。

Sub tt()
Dim ws1     As Worksheet
Set ws1 = Sheets("Report Generation")

With ws1
    Dim rptdate As Variant
    rptdate = InputBox("Please Enter the Report Date *Must be EOM format mm/dd/yyyy*", "Enter Date")

    Do While rptdate = "" Or Not IsDate(rptdate)
        MsgBox ("You did not enter a date in the correct format!"), vbCritical, Error
        rptdate = InputBox("Please Enter the Report Date *Must be EOM format mm/dd/yyyy*", "Enter Date")
    Loop

    .Range("B8").Value = rptdate
End With
End Sub

#2


0  

The below accurately does what I wanted. Thanks findwindow.

以下准确地做了我想要的。谢谢findwindow。

'Updates the report date located in report generation tab cell B8
Dim ws1 As Worksheet
Set ws1 = Sheets("Report Generation")
ReTry:
With ws1
Dim rptdate As Variant
    rptdate = InputBox("Please Enter the Report Date *Must be EOM format mm/dd/yyyy*", "Enter Date")
    If rptdate = "" Then
    MsgBox ("You did not enter a date!"), vbCritical, Error
    GoTo ReTry
    ElseIf Not IsDate(rptdate) Then
    MsgBox ("You did not enter the correct format!"), vbCritical, Error
    GoTo ReTry
    Else
    Range("B8").Value = rptdate
    End If
End With

#1


3  

Your If statements are a little off for this. Here's a code that will remove the use of GoTo (which is best practice), and still loop correctly until you get the format you want.

你的If声明对此有点偏僻。这是一个代码,它将删除GoTo的使用(这是最佳实践),并且仍然正确循环,直到您获得所需的格式。

Sub tt()
Dim ws1     As Worksheet
Set ws1 = Sheets("Report Generation")

With ws1
    Dim rptdate As Variant
    rptdate = InputBox("Please Enter the Report Date *Must be EOM format mm/dd/yyyy*", "Enter Date")

    Do While rptdate = "" Or Not IsDate(rptdate)
        MsgBox ("You did not enter a date in the correct format!"), vbCritical, Error
        rptdate = InputBox("Please Enter the Report Date *Must be EOM format mm/dd/yyyy*", "Enter Date")
    Loop

    .Range("B8").Value = rptdate
End With
End Sub

#2


0  

The below accurately does what I wanted. Thanks findwindow.

以下准确地做了我想要的。谢谢findwindow。

'Updates the report date located in report generation tab cell B8
Dim ws1 As Worksheet
Set ws1 = Sheets("Report Generation")
ReTry:
With ws1
Dim rptdate As Variant
    rptdate = InputBox("Please Enter the Report Date *Must be EOM format mm/dd/yyyy*", "Enter Date")
    If rptdate = "" Then
    MsgBox ("You did not enter a date!"), vbCritical, Error
    GoTo ReTry
    ElseIf Not IsDate(rptdate) Then
    MsgBox ("You did not enter the correct format!"), vbCritical, Error
    GoTo ReTry
    Else
    Range("B8").Value = rptdate
    End If
End With