Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 错误处理问题_Loops_Excel_Error Handling_Vba - Fatal编程技术网

Loops 错误处理问题

Loops 错误处理问题,loops,excel,error-handling,vba,Loops,Excel,Error Handling,Vba,使用我的函数(在更大的代码中),我希望找到一个目标列名。在另一个代码中,我检查列名是否为cancel按钮(我也将相应地调整此选项) 但我想做一个for循环,从1到5,在这个循环中,“如果您输入了错误的列名,它会要求您重新提交。如果您按Cancel,它将退出函数,然后退出sub(我会自己解决这个问题) 问题是,我不知道如何清除错误,所以“on error GoTo”第二次就不起作用了。是否有人可以帮助修复此代码或使其更好(以防有我还不太清楚的技巧?) 编辑: 新代码如下: Function Fin

使用我的函数(在更大的代码中),我希望找到一个目标列名。在另一个代码中,我检查列名是否为cancel按钮(我也将相应地调整此选项)

但我想做一个for循环,从1到5,在这个循环中,“如果您输入了错误的列名,它会要求您重新提交。如果您按Cancel,它将退出函数,然后退出sub(我会自己解决这个问题)

问题是,我不知道如何清除错误,所以“on error GoTo”第二次就不起作用了。是否有人可以帮助修复此代码或使其更好(以防有我还不太清楚的技巧?)

编辑:

新代码如下:

Function FindText(Target As String)

 Dim x
 Dim found As Range

'StartLoop:
For x = 1 To 5

    Set found = Rows(1).Find(what:=Target, after:=.Cells(1, 1))

    If found Is Nothing Then
        Target = InputBox("Please Enter Correct Value(Row Name)")
    Else
        found.Activate
        FindText = ActiveCell.Column
        Exit Function
    End If

    Next x

    FindText = 10000
End Function

当遇到after:=.Cells(1,1))区域时,它会突出显示单元格并给出:Error:invalid or unqualified reference。有什么想法吗?

最好的方法是首先停止错误的发生。类似这样:

...
    Dim found As Range

StartLoop:
    For x = 1 To 5
        With Rows(1)     
            Set found = .Find(what:=Target, after:=.Cells(1, 1))
        End with

        If found Is Nothing Then
            MsgBox ("Can't find " & Target)
        Else
            found.Activate
            FindText = ActiveCell.Column
            Exit Function
        End If
...

希望这足以帮助您修复代码。

好吧,有几件事:

  • 您可能希望防止在
    With
    语句中抛出错误。这可能会导致一些奇怪的错误。因此,我将
    行(1)
    Find
    连接;我还将使用工作表名称对其进行限定,如
    Sheet1.Rows(1).Find(…)

  • 一般来说,当你遇到这样的问题时,试着把你的代码分割成更小的函数。在这种情况下,我能够制作一个封装错误处理的函数,这样问题就不再是问题了

  • 只要有可能,您就要避免使用
    Activate
    Select
    。在这种情况下,它们是不需要的,代码更干净

  • 像这样:

    Function FindText(Target As String)
    
        FindText = 1000 ' set default return value
    
    
        Dim foundCell As Range
        Set foundCell = getCellInRow1ByText(Target)
    
        Dim x As Integer
        For x = 1 To 5
            If Not foundCell Is Nothing Then
                FindText = foundCell.Column
                Exit Function
            End If
            Target = InputBox("Please Enter Correct Value(Row Name)")
            Set foundCell = getCellInRow1ByText(Target)
        Next x
    End Function
    
    Private Function getCellInRow1ByText(ByVal Target As String) As Range
        On Error GoTo Catch
        Set getCellInRow1ByText = Sheet1.Rows(1).Find(what:=Target, after:=Sheet1.Rows(1).Cells(1, 1))
        Exit Function
    Catch:
        Set getCellInRow1ByText = Nothing '' not really needed, but explains what's happening
    End Function
    
    可能不需要错误处理,但我已经包含了它,以便您可以学习方法

    顺便说一句,对于原始问题的严格答案,您可以尝试添加

    On Error GoTo 0
    

    您需要使用
    Resume startoop
    而不是Goto(或者您可以在错误Goto-1上使用
    )它给了我错误、无效或不合格的引用。我将编辑我的原始帖子以适应代码properly@user3654055已修复问题,请尝试编辑版本。我是user3654055。请阅读代码下面的内容,因为仍然存在问题。@user3654055是,我在上次编辑中已修复…(从中复制并粘贴我的新代码[With Rows(1)]…to…[End With])你做到了!谢谢!我个人讨厌错误处理,因为它没有找到问题的根源,但我对VBA有点陌生,不知道“什么都不知道”“存在。这回答了我的很多错误检查问题,所以非常感谢!我会投票支持你,但显然我需要15个声誉,现在还没有。对不起,ForkandBeard
    
    On Error GoTo 0