Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.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
Excel VBA-满足条件时如何停止查找功能_Vba_Excel_Find - Fatal编程技术网

Excel VBA-满足条件时如何停止查找功能

Excel VBA-满足条件时如何停止查找功能,vba,excel,find,Vba,Excel,Find,我在循环中使用这行代码: Set foundCell = Cells.Find(What:=pasteValue, After:=ActiveCell, LookIn:=xlValues, MatchCase:=False, SearchFormat:=False) 它工作得很好,但由于文档的大小,速度非常慢。 “Find”函数会一直搜索,直到EOF,即使第一行满足条件,我也会在一张非常大的工作表上运行它。有没有办法在第一次满足条件时停止查找功能 谢谢 编辑: 我假设您正在with或for循环

我在循环中使用这行代码:

Set foundCell = Cells.Find(What:=pasteValue, After:=ActiveCell, LookIn:=xlValues, MatchCase:=False, SearchFormat:=False)
它工作得很好,但由于文档的大小,速度非常慢。 “Find”函数会一直搜索,直到EOF,即使第一行满足条件,我也会在一张非常大的工作表上运行它。有没有办法在第一次满足条件时停止查找功能

谢谢

编辑:


我假设您正在with或for循环中运行find函数,如果结果不是空的,则可以退出sub。如果希望宏从另一个特定点运行,则可以使用goto->

If Not foundCell Is Nothing Then
    exit sub


我不太清楚这个问题,但是在你想停止循环的地方添加
Exit Do
。如果我理解正确,在匹配搜索并删除行后,这应该出现在
if
语句中:

 If Not foundCell Is Nothing Then
        ActiveCell.EntireRow.Delete
        Windows("Book3").Activate
        Selection.Offset(0, 4).Select
        ActiveCell.Value = "Y"
        Selection.Offset(1, -4).Select
        Exit Do 'Exit the loop
    End If

你能限制你正在搜索的范围吗?看起来您正在搜索活动工作表中的所有单元格。。。通常情况下,这可能无关紧要……但在您的情况下,可能会如此。让我粘贴整个代码以更好地显示我的问题:我不能限制范围。我不想停止循环。大约有30000排。在Excel中,执行“查找”功能大约需要2秒,即使该值是在搜索的第一行中找到的。
If Not foundCell Is Nothing Then
    GoTo LineNumber or Identifier
 If Not foundCell Is Nothing Then
        ActiveCell.EntireRow.Delete
        Windows("Book3").Activate
        Selection.Offset(0, 4).Select
        ActiveCell.Value = "Y"
        Selection.Offset(1, -4).Select
        Exit Do 'Exit the loop
    End If