2016-02-28 100 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