简体   繁体   English

如何搜索包含“文本”的特定列并删除不包含搜索的所有行

[英]How to search specific column that contain “text” and delete all row not containing search

Below VBA code is to find text and delete row. VBA下面的代码是查找文本并删除行。 But it is searching based on the whole sheet. 但它是基于整个工作表进行搜索。

How to I make it to only search "specific column" with the text array listed and delete the rows that contain text . 如何使它仅搜索列出的文本数组的“特定列”删除包含text的行

Based on the below code, it is search the whole sheet which I do not want. 根据下面的代码,它将搜索整个我不想要的表。

Sub DeleteSystemMessage()
    Dim varList As Variant
    Dim varQP As Variant
    Dim lngarrCounter As Long
    Dim rngFound As Range, rngToDelete As Range
    Dim strFirstAddress As String

    Application.ScreenUpdating = False


'delete system message
    varList = VBA.Array("XXXXXX", vbTextCompare)

    For lngarrCounter = LBound(varList) To UBound(varList)
        With Sheet1.UsedRange
            Set rngFound = .Find( _
                                What:=varList(lngarrCounter), _
                                Lookat:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)

            If Not rngFound Is Nothing Then
                strFirstAddress = rngFound.Address

                If rngToDelete Is Nothing Then
                    Set rngToDelete = rngFound
                Else
                    If Application.Intersect(rngToDelete, rngFound.EntireRow) Is Nothing Then
                        Set rngToDelete = Application.Union(rngToDelete, rngFound)
                    End If
                End If

                Set rngFound = .FindNext(After:=rngFound)

                Do Until rngFound.Address = strFirstAddress
                    If Application.Intersect(rngToDelete, rngFound.EntireRow) Is Nothing Then
                        Set rngToDelete = Application.Union(rngToDelete, rngFound)
                    End If
                    Set rngFound = .FindNext(After:=rngFound)
                Loop
            End If
        End With
    Next lngarrCounter

    If Not rngToDelete Is Nothing Then rngToDelete.EntireRow.Delete
Application.ScreenUpdating = True


End Sub

If you change your search code like this it will only search in the column "A". 如果您这样更改搜索代码,它将仅在“ A”列中搜索。

Set rngFound = Sheets(1).Columns("A:A").Find( _
                            What:=varList(lngarrCounter), _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)

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

相关问题 搜索特定文本并删除相应行 - Search for specific text and delete the corresponding row 在Java中,如何删除符合搜索条件的文本文件中的行? - In Java, How to delete a row in a text file that meets search criteria? 如何搜索包含 JSON 数组的 SQL 列 - How to search SQL column containing JSON array 如何在包含特定年份的对象数组中二进制搜索特定年份 - How to binary search for a specific year in an array of objects containing a specific year 如何删除数组中的特定行和列(java) - how to delete a specific row and column in an array (java) 如何在数组中搜索以查找包含特定值的所有子数组 - how to search in array to find all sub arrays that contain a certain value 如何在矩阵/数组中搜索包含特定字符串的子数组 (Javascript) - How to search a matrix/array for a subarray containing a specific string (Javascript) 在字符串中搜索特定文本 - Search string for specific text SQL查询,使用数组列的特定项目搜索所有行 - SQL query, which search all rows with specific items of array column 搜索具有特定列值的数组行并从符合条件的行返回另一个值 - Search for array row with specific column value and return another value from qualifying row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM