Mar*_*kus 0 excel vba excel-vba
以下代码确定行中的单元格是否包含单词"Done".如果是,则将该行复制到另一个标有"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
Run Code Online (Sandbox Code Playgroud)
提前感谢您的帮助!
问题在于你的其他情况.你已经通过进入else块忽略了那一行.并且你的循环代码负责递增.所以你的代码基本上就是
If Cells(r, 15) = "Done" Then
'process done row
Else
'skip two rows.
End If
Run Code Online (Sandbox Code Playgroud)
我认为你应该省略你的else块,让循环结构迭代r.
'remove this part
Else
r = r + 1
Run Code Online (Sandbox Code Playgroud)