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
Excel如果没有退出循环失败_Excel_Vba - Fatal编程技术网

Excel如果没有退出循环失败

Excel如果没有退出循环失败,excel,vba,Excel,Vba,我有以下Do和嵌套的If Not语句: Do Set i = SrchRng.Find("#609532", LookIn:=xlValues) If Not i Is Nothing Then i.EntireRow.Copy BBsheet.Activate nextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1 Cells(nextRow, 1)

我有以下
Do
和嵌套的
If Not
语句:

Do
        Set i = SrchRng.Find("#609532", LookIn:=xlValues)
        If Not i Is Nothing Then i.EntireRow.Copy
            BBsheet.Activate
            nextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
            Cells(nextRow, 1).Select
            Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, _
                SkipBlanks:=False, Transpose:=False
            srcBook.Activate
            i.EntireRow.Delete
    Loop While Not i Is Nothing
这可以正常工作,但它无法在应该退出循环时退出。当我单步执行时,它会抓取
If Not I
并跳过copy命令,但仍会单步执行下面的行,并且在
选择中失败。我似乎无法让它跳过这些,转到下一个
Do
。以下内容有效,但我需要在删除之前进行复制:

Do
        Set i = SrchRng.Find("#609532", LookIn:=xlValues)
        If Not i Is Nothing Then i.EntireRow.Delete
    Loop While Not i Is Nothing

如何让循环注册“#609532”不再存在并继续下一个循环?

如果。。然后如果。。。然后..

Do
    Set i = SrchRng.Find("#609532", LookIn:=xlValues)
    If Not i Is Nothing Then 
        i.EntireRow.Copy
        BBsheet.Activate
        nextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
        Cells(nextRow, 1).Select
        Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
        srcBook.Activate
        i.EntireRow.Delete
    End If
Loop While Not i Is Nothing
最好避免
Select
Activate
语句:

Do
    Set i = SrchRng.Find("#609532", LookIn:=xlValues)
    If Not i Is Nothing Then
        i.EntireRow.Copy
        With BBsheet
            nextRow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
            .Cells(nextRow, 1).PasteSpecial Paste:=xlValues
        End With
        i.EntireRow.Delete
    End If
Loop While Not i Is Nothing

如果..,则需要使用
。。然后如果。。。然后..

Do
    Set i = SrchRng.Find("#609532", LookIn:=xlValues)
    If Not i Is Nothing Then 
        i.EntireRow.Copy
        BBsheet.Activate
        nextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
        Cells(nextRow, 1).Select
        Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
        srcBook.Activate
        i.EntireRow.Delete
    End If
Loop While Not i Is Nothing
最好避免
Select
Activate
语句:

Do
    Set i = SrchRng.Find("#609532", LookIn:=xlValues)
    If Not i Is Nothing Then
        i.EntireRow.Copy
        With BBsheet
            nextRow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
            .Cells(nextRow, 1).PasteSpecial Paste:=xlValues
        End With
        i.EntireRow.Delete
    End If
Loop While Not i Is Nothing

该死太晚了。。。我将不得不放弃我的答案:D顺便说一句,不需要
。激活
:)或
。选择
@SiddharthRout,我刚刚用代码避免
选择
激活
更新了答案statements@Benjooster,是否使用了这行代码
nextRow=.Cells(.Rows.Count,1).End(xlUp).Row+1
或此
nextRow=Cells(.Rows.Count,1)。End(xlUp)。Row+1
(请注意,在
单元格之前没有
)?@simoco完全忽略了这一点。谢谢,该死!太晚了。。。我将不得不放弃我的答案:D顺便说一句,不需要
。激活
:)或
。选择
@SiddharthRout,我刚刚用代码避免
选择
激活
更新了答案statements@Benjooster,是否使用了这行代码
nextRow=.Cells(.Rows.Count,1).End(xlUp).Row+1
或此
nextRow=Cells(.Rows.Count,1)。End(xlUp)。Row+1
(请注意,在
单元格之前没有
)?@simoco完全忽略了这一点。谢谢