繁体   English   中英

如果单元格/列不包含文本,如何删除Excel中的行?

[英]How to I delete rows in Excel if a cell/column does not contain text?

我有一个表格中包含数据的电子表格。 在某些行中,同一列中有特定的文本。 我如何删除所有期望包含该文本的行?

如果我理解正确,下面的代码将删除活动电子表格的第2列中不包含TEXT行。

Sub deleteit()
    Dim colNo As Long:      colNo = 2             ' hardcoded to look in col 2
    Dim ws    As Worksheet: Set ws = ActiveSheet  ' on the active sheet
    Dim rgCol As Range
    Set rgCol = ws.Columns(colNo)                          ' full col range (huge)
    Set rgCol = Application.Intersect(ws.UsedRange, rgCol) ' shrink to nec size
    Dim rgZeroCells As Range ' range to hold all the "0" cells (union of disjoint cells)
    Dim rgCell      As Range ' single cell to iterate
    For Each rgCell In rgCol.Cells
        If Not IsError(rgCell) Then
            If rgCell.Value <> "TEXT" Then                 ' Your TEXT string goes here
                If rgZeroCells Is Nothing Then
                    Set rgZeroCells = rgCell ' found 1st one, assign
                Else
                    Set rgZeroCells = Union(rgZeroCells, rgCell) ' found another, append
                End If
            End If
        End If
    Next rgCell
    If Not rgZeroCells Is Nothing Then
        rgZeroCells.EntireRow.Delete ' deletes all the target rows at once
    End If
End Sub

希望能帮助到你

暂无
暂无

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

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