简体   繁体   English

删除所有不包含特定文本的行:使用InStr(范围)定义搜索范围

[英]Delete all rows that do not contain specific text : Define search range using InStr(Range)

I want to delete any row that does not contain a specific text (Federation). 我想删除任何不包含特定文本的行(联合)。 The term may be in numerous columns, and may be prefaced by either LOI-, COC- or REA- (ie LOI-Federation). 该术语可以在许多列中,并且可以以LOI-,COC-或REA-(即LOI-联合)开头。
My range of columns has the error 我的列范围有错误

Method of 'Range' of Object'_Global' Failed Object'_Global'失败的'范围'方法

I want to search column O through column IH. 我想通过列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

You can use Find : 您可以使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM