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

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

如果我理解你的话,下面的代码将删除你的活动电子表格第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 

希望能帮助到你