excel - 错误处理不工作 VBA

标签 excel vba goto

如果找不到您要查找的内容,我正在尝试将用户循环回来。到目前为止我有这个,但是我如何循环返回让用户重新输入信息并用 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 的方法要清楚有重试

  • 我整理了Select , Find逻辑也是如此。
    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
    

    关于excel - 错误处理不工作 VBA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25393623/

    相关文章:

    ruby-on-rails - 如何用roo打开xls文件

    excel - 动态添加标签到用户表单 = 空白用户表单

    Excel公式返回非连续单元格中的第一个值

    excel - 使用循环的替代方法使代码运行得更快

    javascript - VBA IE导航触发Javascript

    c - 多个 goto 标签如何工作

    sql - 在 SQL 中遇到异常后如何继续运行我的程序?

    batch-file - 在 if 语句 = 上做 2 件事

    python - 我该如何将二维列表的内容复制到 Excel 格式的剪贴板?

    vba - 从 A 列复制范围并写入 B 列