2011-09-21 120 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