重新启动input框

我有一个excel表单,比第一次打开时要求用户在input框中input一个date,并把它放到表单中的单元格中。 如果有人input错误的date,我有一个错误句柄popup一个无效的date错误框。 但是我想要做的是当一个无效的date被inputdate的原始input框再次popup,以便他们可以再次进入。 我的代码如下,但是我一直在收到错误信息。

谢谢

ReShowInputBox: cellvalue = Application.InputBox("Please Enter The Date for Data Extracted (dd/mm/yyyy)") On Error GoTo ErrHandle ErrHandle: MsgBox ("Invalid Date") ReShowInputBox: cellvalue = Application.InputBox("Please Enter The Date for Data Extracted (dd/mm/yyyy)") If cellvalue = "" Then Exit Sub ws.Select ws.Range("A1").Value = DateValue(cellvalue) MsgBox ("Date Entered!") 

怎么样一个简单的Do Until循环:

 Dim cellvalue As Variant Do cellvalue = Application.InputBox("Please Enter The Date for Data Extracted (dd/mm/yyyy)") Loop Until IsDate(cellvalue) And IsNumeric(Right(cellvalue, 4)) And IsNumeric(Left(cellvalue, 2)) And IsNumeric(Mid(cellvalue, 4,2)) ws.Range("A1").Value = cellvalue MsgBox ("Date Entered!") 

我testing了这个非常彻底,它只接受你想要的确切格式的date。

这是一个简单的方法来重复询问一个date,直到你得到一个,但允许用户取消:

 Sub fhskjfs() Dim i As String, d As Date i = "" While Not IsDate(i) i = Application.InputBox(Prompt:="Enter a date", Type:=2) If i = False Then Exit Sub If IsDate(i) Then d = CDate(i) Wend End Sub 

编辑#1:

这是一种实现简单格式检查的方法:

 Public Function CheckFormat(i As String) As String CheckFormat = "junk" ary = Split(i, "/") If UBound(ary) <> 2 Then Exit Function If CLng(ary(2)) < 1900 Or CLng(ary(2)) > 9999 Then Exit Function If CLng(ary(1)) > 12 Then Exit Function If CLng(ary(0)) > 31 Then Exit Function CheckFormat = i End Function Sub GetDate() Dim i As String, d As Date i = "" While Not IsDate(i) i = Application.InputBox(Prompt:="Enter a date", Type:=2) If i = "False" Then Exit Sub i = CheckFormat(i) If IsDate(i) Then d = CDate(i) Wend End Sub