2016-02-28 105 views
-1

我不能似乎使我的工作循环:Excel宏循环删除

For Each c In ActiveWorkbook.Sheets("appointments").Range("N1:N1000000") 
    If c = "Test" Then c.EntireRow.Delete 
Next 

我想是删除N列

+2

更改为常规循环和向后循环。本网站和其他网站上有很多例子。 –

+0

对不起,我是新手。你能帮我编辑它吗? –

+0

请参阅[这里](http://stackoverflow.com/questions/33744149/code-in-vba-loops-and-never-ends-how-to-fix-this)它提供了三个很好的方法来做到这一点。 –

回答

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 
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