如果输入的日期无效,如何使代码循环到哪里,用户可以重试?

时间:2019-06-04 17:03:31

标签: excel vba

Sub test
    Dim ws As Worksheet: Set ws = ActiveWorkbook.Sheets(“GL”)
    Dim xInput As Date, found As Range
    xInput = Application.InputBox(“Enter Friday date”, Type:=1)
    If IsDate(xInput) Then
        Set found = ws.Range(“F:F”).Find(xInput, searchdirection:=xlPrevious)
         If found Is Nothing Then
             ‘Need code to have user try again
        Else
            found.Offset(1).EntireRow.Insert (xlShiftDown)
        End If
    Else
        ‘Need code to have user try again
    End if
End sub

现在我有只退出sub的代码,但是我需要代码帮助,该代码将在其中插入另一个InputBox再次尝试。

1 个答案:

答案 0 :(得分:0)

您可以使用下面的Loop,也可以使用FormatDateTime来获取InputBox的日期格式

Option Explicit

Sub NextFriday()

Dim FridayDate As Date

Do
    FridayDate = Application.InputBox("Enter Friday's Date :", "Date Selection", FormatDateTime(Date, vbShortDate), Type:=1)
    If Not IsDate(FridayDate) Then MsgBox "Error, not a Date format"
Loop Until IsDate(FridayDate)

End Sub