Excel宏循环删除

时间:2016-02-28 05:20:39

标签: excel vba loops macros

我似乎无法使我的循环工作:




  For each c In ActiveWorkbook.Sheets(“约会”)。范围(“N1:N1000000 “)
如果c =“Test”那么c.EntireRow.Delete
 Next
  




我想要的是删除单元格值为Test的行在N栏。




2 个答案:

答案 0 :(得分:2)

Dim i As Long

For i = 1000000 To 1 Step -1
    If Cells(i, 14).Value = "Test" Then
        Rows(i).Delete
    End If
Next i

以上将按照您的要求进行操作,但我建议这样做,因为我怀疑您有一百万行的数据:

Dim i As Long
Dim LastRow As Long

LastRow = ActiveCell.SpecialCells(xlCellTypeLastCell).Row

For i = LastRow To 1 Step -1
    If Cells(i, 14).Value = "Test" Then
        Rows(i).Delete
    End If
Next i

答案 1 :(得分:2)

这是我的答案,而且效果很快:

ActiveWorkbook.Sheets("appointments").Range("N1:N1000000").AutoFilter 1, "=Test"
     ActiveWorkbook.Sheets("appointments").Range("N2:N1000000").SpecialCells(xlCellTypeVisible).EntireRow.Delete
     ActiveWorkbook.Sheets("appointments").AutoFilterMode = False