我有以下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
并跳过复制命令,但仍会逐步执行以下行并在Selection.PasteSpecial
上失败。我似乎无法跳过这些并转到下一个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”不再存在并继续下一个循环?
答案 0 :(得分:5)
您需要使用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
最好避免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