2011-09-21 164 views
0

我編寫了此腳本以刪除包含與「201103」不同的列C中的值的行。當我使用這大膽它,它的工作原理,但當我用它與.Delete它表現奇怪,並不能正常工作。根據值選擇/刪除某些行

我試圖得到選定的行,並使用UNION合併它並使用.SELECT(多個),所以我可以手動刪除它,但不知道如何做到這一點。

Sub test() 

    Dim Cell As Range 

    For Each Cell In Range("C2:C2308").Cells 
     If (Cell.Value <> "201103" And Cell.Value <> "") Then 
      Cell.EntireRow.Font.Bold = True 
      'Cell.EntireRow.Delete 
     End If 
    Next Cell 

End Sub 

有誰知道如何解決它,所以它工作正常?

+0

檢查此鏈接http://www.ozgrid.com/VBA/row-delete-criteria.htm –

+0

有可能是別的鎖定該範圍(取決於這些一些公式線,例如)。嘗試在新的Excel工作簿中使用它,它可能會起作用。剛剛在這裏試了一下(Excel 2007)。此外,「行爲奇怪,不能正常工作」是什麼? –

回答

3

試試這個:

Sub test() 
' 

With ActiveSheet 
    .AutoFilterMode = False 
    With Range("C2", Range("C" & Rows.Count).End(xlUp)) 
     .AutoFilter 1, "<>201103" 
     On Error Resume Next 
     .Offset(1).SpecialCells(12).EntireRow.Delete 
    End With 
    .AutoFilterMode = False 
End With 
End Sub 
+0

+1非常有效地完成 – brettdj