在Excel上使用VBA脚本来删除具有特定字符串的行

时间:2017-06-06 23:34:15

标签: excel vba excel-vba

我目前正在使用excel编写宏来删除包含特定字符串的整行。我正在进行一些数据分析,并希望过滤掉不需要信息的电子表格。

一个任意的例子:

我如何制作一个删除包含红色,蓝色和黄色的行的脚本?

1 个答案:

答案 0 :(得分:0)

我使用此代码。

Sub test()
    Dim rngDB As Range, rngU As Range, rngF As Range
    Dim vColor, i As Integer, Adr As String

    Set rngDB = ActiveSheet.UsedRange
    vColor = Array("red", "yellow", "blue")

    For i = 0 To UBound(vColor)
        With rngDB
            Set rngF = .Find(vColor(i), LookIn:=xlValues, Lookat:=xlWhole)
            If rngF Is Nothing Then
            Else
                Adr = rngF.Address
                Do
                    If rngU Is Nothing Then
                        Set rngU = rngF
                    Else
                        Set rngU = Union(rngU, rngF)
                    End If
                    Set rngF = .FindNext(rngF)
                Loop Until Adr = rngF.Address Or rngF Is Nothing
            End If
        End With
    Next i
    If rngU Is Nothing Then
    Else
        rngU.EntireRow.Delete
    End If
End Sub