删除包含多个文本的行

时间:2018-02-06 10:55:25

标签: excel vba excel-vba

我需要删除以下文本位于A列和Z列之间的行:

  • "客户声明"
  • "客户编号:'
  • "日期"

这应该从第11行开始搜索

这似乎不起作用:

Sub Deleterowsmultiplename()
    Cells.Find(What:="Customer Statement").Offset(1, 0).Resize(5, 1).EntireRow.Delete
End Sub

2 个答案:

答案 0 :(得分:2)

试试这个:

Sub DeleteRows()
Dim row As Long

For row = Rows.Count To 11 Step -1
    If Not Range(Cells(row, 1), Cells(row, 26)).Find("Customer Statement") Is Nothing Then
    If Not Range(Cells(row, 1), Cells(row, 26)).Find("Customer No:") Is Nothing Then
    If Not Range(Cells(row, 1), Cells(row, 26)).Find("date") Is Nothing Then
        Rows(row).entireRow.Delete
    End If
    End If
    End If
Next

End Sub

由于您希望它在表单中的所有行中运行,因此需要一段时间(大约10秒)。

答案 1 :(得分:0)

使用On Error Resume Next并不是最干净的,但它会完成工作。请注意,没有说明应该应用哪个工作表,因此将在Activesheet上完成。

Sub Deleterowsmultiplename()
    Dim i As Long
    Dim WhattoFind As Variant
    Dim myRng As Range


    WhattoFind = Array("Customer Statement", "Customer No:", "date")
    Set myRng = Range(Cells(11, 1), Cells(1048576, 26))
    On Error Resume Next
    For i = LBound(WhattoFind) To UBound(WhattoFind)
        myRng.Find(What:=WhattoFind(i)).EntireRow.Delete
    Next i
End Sub

编辑: 改进并正确处理错误

Sub Deleterowsmultiplename()
    Dim i As Long
    Dim WhattoFind As Variant
    Dim myRng As Range


    WhattoFind = Array("Customer Statement", "Customer No:", "date")
    Set myRng = Range(Cells(11, 1), Cells(1048576, 26))
    For i = LBound(WhattoFind) To UBound(WhattoFind)
        Do
        On Error GoTo NextName
        myRng.Find(What:=WhattoFind(i)).EntireRow.Delete
        Loop
NextName:
    Resume NextName2
NextName2:
    Next i
End Sub