Excel如果不是无法退出循环

我有以下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 

这个function正常,但它应该退出循环。 当我通过它,它抓住了If Not i并跳过复制命令,但仍然通过下面的行,并通过Selection.PasteSpecial失败。 我似乎无法跳过这些,继续下一步。 下面的工作,但我需要复制之前删除:

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

我如何获得循环注册“#609532”不再存在,并进入下一个循环?

你需要使用If .. Then .. End If语句而不是If ... Then ..

 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 

最好避免SelectActivate语句:

 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