删除所有不包含特定文本的行:使用InStr(Range)

时间:2019-05-24 14:53:07

标签: excel vba

我想删除任何不包含特定文本的行(联合身份验证)。该术语可能在许多栏中,并且可能以LOI-,COC-或REA-(即LOI联盟)开头。
我的列范围有错误

  

对象“ _Global”的“范围”方法失败

我想在IH列中搜索O列。

Sub CYJLMasterReportMacro()
    SeparateDesignation ("Federation")
End Sub

Sub SeparateDesignation(ByVal DesignationName As String)

    Worksheets.Add().Name = DesignationName
    Worksheets("Results").Activate
    ActiveSheet.Cells.Copy
    Worksheets(DesignationName).Activate
    ActiveSheet.Paste

    Dim i As Integer
    For i = 743 To 2 Step -1
        If InStr(Range("O:IH" & i), DesignationName) = 0 Then
            Range("O:IH" & i).EntireRow.Delete
        End If
    Next

End Sub

1 个答案:

答案 0 :(得分:0)

您可以使用Find

Sub SeparateDesignation(ByVal DesignationName As String)

    Dim f As Range, i As Long, wsNew as Worksheet

    Worksheets("Results").Copy After:= Worksheets("Results")
    Set wsNew = Worksheets(Worksheets("Results").Index + 1)
    wsNew.Name = DesignationName

    For i = 743 To 2 Step -1
        Set f = nothing
        Set f = wsNew.Range("O" & i & ":IH" & i).Find(DesignationName, lookat:=xlPart)
        If f is nothing Then
            wsNew.Rows(i).Delete
        End If
    Next

End Sub