VBA跳过循环?

以下代码确定行中的单元格是否包含单词“完成”。 如果是,则将该行复制到另一张标有“Log”并从列表中删除的表。 为什么下面的循环会跳过一些标记为“完成”的行?

Sub LogAndDelete() Dim r As Integer Dim Emptyrow As Integer r = 1 For r = 5 To 70 If Cells(r, 15) = "Done" Then 'copying the row marked "Done" rng = Range(Cells(r, 1), Cells(r, 7)) 'determining the next empty row on log sheet Emptyrow = _ Application.WorksheetFunction.CountA(Worksheets("Log").Range("A:A")) + 1 'Pasting onto log sheet Range(Worksheets("Log").Cells(Emptyrow, 2), Worksheets("Log").Cells(Emptyrow, 8)) _ = rng 'Adding the date removed from log sheet in column A Worksheets("Log").Cells(Emptyrow, 1) = Date 'Removing row marked "done" from list Range(Cells(r, 1), Cells(r, 10)).ClearContents Else r = r + 1 End If Next End Sub 

预先感谢您的帮助!

问题在于你的情况。 您已经通过进入else块来忽略该行。 你的循环代码负责增加。 所以你的代码基本上是

 If Cells(r, 15) = "Done" Then 'process done row Else 'skip two rows. End If 

我想你应该省略你的else块,并让循环结构迭代r。

 'remove this part Else r = r + 1 

我想知道增加一个For循环内可能是问题。 还没有testing过。

按照线路和testing注释掉。

  i = i + 1