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完全忽略了这一点。谢谢