Excel VBA删除包含特定文本的行。代码无效

时间:2018-01-14 17:47:17

标签: excel vba

我有一个打开工作簿的宏。我希望它然后查看C列,如果它找到任何带有文本" Draft"的内容,则删除整行。这是我的代码,似乎没有给我任何错误,但它不会删除我想要的行。我错过了什么?

enter code here
Dim i As Long
Dim FinalRow As Long
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row

With Worksheets("Archer Search Report")
For i = 2 To FinalRow
If Range("C" & i).Value = "Draft" Then
    Rows(i).Delete
End If
Next i
End With

1 个答案:

答案 0 :(得分:3)

试试这个:

E_Modifier

注意:

  • 最好在删除时向后工作(Sub DeleteRows() Dim i As Long, finalRow As Long finalRow = Cells(Rows.Count, 1).End(xlUp).Row With Worksheets("Archer Search Report") For i = finalRow To 2 Step -1 If Range("C" & i).Value = "Draft" Then Range("C" & i).EntireRow.Delete End If Next i End With End Sub )否则会因行数而混乱
  • 我更喜欢Step -1