删除VBA中的行

时间:2014-04-13 12:18:14

标签: excel vba excel-vba

我在Excel 2013中编写VBA代码。 我需要过滤工作表并留下某些行(比如说过滤列B并留下包含" Apple"在该列中的行) - 但是,我想要删除除#&34之外的所有行;苹果"

所以我的问题是 - 在VBA代码中是否有一种聪明的方法可以做到这一点,如果有,那么如何? 或者我应该过滤掉除#34; Apple"之外的所有内容,将剩余的行复制粘贴到另一张纸上,然后处理新的纸张?

谢谢!

1 个答案:

答案 0 :(得分:0)

没有自动筛选:

Sub RowKiller2()
    Dim N As Long, rDel As Range, cl As String
    Dim kv As Variant, r As Range, rBig As Range
    cl = "B"
    kv = "Apple"
    N = Cells(Rows.Count, cl).End(xlUp).Row
    Set rBig = Range(cl & 1 & ":" & cl & N)
    For Each r In rBig
    If r.Value <> kv Then
        If rDel Is Nothing Then
            Set rDel = r
        Else
            Set rDel = Union(rDel, r)
        End If
    End If
    Next r

    If Not rDel Is Nothing Then
        rDel.EntireRow.Delete
    End If

End Sub