error handling不工作的VBA

我试图循环用户回来,如果它找不到你在找什么。 我到目前为止,但我如何循环回来让用户重新input信息,并提示他们与一个MsgBox以及?

 Range("A1").Select Repeat: Findcata = InputBox("Please Input What Section You Want To Add To") If Findcata = "" Then Exit Sub Set RangeObj = Cells.Find(What:=Findcat, After:=ActiveCell, _ LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _ SearchDirection:=xlNext, MatchCase:=False) If RangeObj Is Nothing Then GoTo Repeat Else: RangeObj.Select 

下面的方法使用Application.Inputbox这个

  • 提供了提供默认值的能力
  • 提供了一个方法来更新MsgBox清除是有一个重试

我也整理了SelectFind逻辑。

 Dim strIn As String Dim strNew As String Dim rng1 As Range Repeat: strIn = Application.InputBox("Please Input What Section You Want To Add To (Cancel Exits)", strNew, "Default value", , , , , 2) If strIn = vbNullString Or strIn = "False" Then Exit Sub Set rng1 = Cells.Find(strIn, ActiveSheet.[a1], xlValues, xlPart, xlByRows, xlNext, False) If rng1 Is Nothing Then strNew = "Value not found, please retry" GoTo Repeat End If