简体   繁体   English

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

[英]Delete all cells which contain specific information

I have code like below because I would like to delete all rows which contain "Something" value but when I start it, it does not delete every row just some, I have click 5 times to delete every row, how to implement macro which will be deleted all rows which contain "Something" by one click ? 我有如下代码,因为我想删除所有包含“ Something”值的行,但是当我启动它时,它并不会删除每一行,而是单击5次以删除每一行,如何实现宏一键删除包含“某些内容”的所有行? Thanks in advance 提前致谢

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

If all the values are in column A, say, then: 如果所有值都在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

The xlTextValues is for the optional value parameter of the Range.SpecialCells method , not the primary Type where you have it. xlTextValues用于Range.SpecialCells方法的可选value参数,而不是包含它的主Type

You will need to choose between xlCellTypeConstants and xlCellTypeFormulas with xlTextValues as the option. 您将需要在xlCellTypeConstantsxlCellTypeFormulas之间进行选择,并带有xlTextValues作为选项。 If you need both, you will have to run it twice. 如果两者都需要,则必须运行两次。

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

The xlCellTypeConstants and xlTextValues both share a numerical value of 2 (although used for different purposes). xlCellTypeConstantsxlTextValues都共享数值2(尽管用于不同目的)。 For all intents and purposes, you were looking through the xlCellTypeConstants for any type of value, including errors. 出于所有目的和目的,您正在通过xlCellTypeConstants查找任何类型的值,包括错误。

VBA's default comparison operator is vbBinaryCompare which is case sensitive. VBA的默认比较运算符是vbBinaryCompare ,区分大小写。 Use something like If Lcase(cell) = "something" Then for a non-case sensitive comparison. 使用类似If Lcase(cell) = "something" Then进行不区分大小写的比较。

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

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