删除所有包含特定信息的单元格

我有下面的代码,因为我想删除所有包含“Something”值的行,但是当我启动它时,它不会删除每一行,只是一些,我已经点击5次删除每一行,如何实现macros一键删除所有包含“Something”的行吗? 提前致谢

Dim cell As Range Worksheets("Something").Activate For Each cell In Cells.SpecialCells(xlTextValues) If cell = "Something" Then cell.EntireRow.Delete End If Next cell 

如果所有的值都在列A中,那么说:

 Worksheets("Something").Activate With ActiveSheet Dim LastRow as Long LastRow = .Cells(.Rows.Count,1).end(xlUp).Row Dim i As Long For i = LastRow to 1 Step -1 If .Cells(i,1).Value = "Something" Then .Rows(i).Delete End If Next End With 

xlTextValues是Range.SpecialCells方法的可选参数,而不是您所拥有的主types

您将需要在xlCellTypeConstantsxlCellTypeFormulas之间selectxlTextValues作为选项。 如果你同时需要,你将不得不运行两次。

 Dim cell As Range with Worksheets("Something") .activate For Each cell In Cells.SpecialCells(xlCellTypeFormulas, xlTextValues) If cell = "Something" Then cell.EntireRow.Delete End If Next cell For Each cell In Cells.SpecialCells(xlCellTypeConstants, xlTextValues) If cell = "Something" Then cell.EntireRow.Delete End If Next cell end with 

xlCellTypeConstantsxlTextValues都共享一个数值2(虽然用于不同的目的)。 对于所有意图和目的,您正在通过xlCellTypeConstants查看任何types的值,包括错误。

VBA的默认比较运算符是vbBinaryCompare ,区分大小写。 使用类似于If Lcase(cell) = "something" Then进行非区分大小写的比较。