使用VBA删除具有特定单词的行

时间:2015-03-17 10:55:21

标签: vba excel-vba excel

目前我的excel数据包含特定的单词和#N / A,单词就像" build One" " proj ex" ..我准备了一个代码,它只删除了一个条件,但我想要它用于很多单词。下面是我的代码。欢迎任何帮助。感谢。

Sub del()
Dim rng1 As Range
Set rng1 = Range([B2], Cells(Rows.Count, "B").End(xlUp))
ActiveSheet.AutoFilterMode = False
With rng1
.AutoFilter Field:=1, Criteria:=("#N/A")
.Delete xlUp
End With
End Sub

2 个答案:

答案 0 :(得分:4)

使用变量数组作为单词列表的构造函数。

Sub del()
    Dim rng1 As Range, vDELs As Variant

    vDELs = Array("#N/A", "proj ex", "build One")
    Set rng1 = Range([B2], Cells(Rows.Count, "B").End(xlUp))

    ActiveSheet.AutoFilterMode = False
    With rng1
        .AutoFilter Field:=1, Criteria1:=(vDELs), Operator:=xlFilterValues
        With .Offset(1, 0)
            If CBool(Application.Subtotal(103, .Cells)) Then _
                .EntireRow.Delete
        End With
        .AutoFilter
    End With
End Sub

Criteria1:=(vDELs)中包围数组的好方法。这很重要。在提交操作之前检查是否有要删除的行也是个好主意。

答案 1 :(得分:0)

你可以尝试类似的东西:

sFormula = "=IF(OR(ISERROR(B:B),B:B=""proj ex"", B:B=""build One""),NA(),"""")"

Set rng1 = Range("A2:A" & Cells(Rows.Count, "B").End(xlUp).Row)
rng1.Formula = sFormula

'现在使用SpecialCells删除行:

rng1.SpecialCells(xlCellTypeFormulas, xlErrors).EntireRow.Delete shift:=xlUp

SO: How to delete multiple rows in Excel without a loopRon de Bruin Excel VBA Top Banana: Special Cells limit bug

上有关此类技术的更多信息
相关问题