我有一个包含表格数据的电子表格。在某些行中,同一列中存在特定文本。我怎样才能删除所有那些拥有该文本的行?
答案 0 :(得分:1)
如果我理解正确,则以下代码会删除活动电子表格第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
希望有所帮助