简体   繁体   English

VBA基于多个条件删除行-代码缺少某些条件

[英]vba delete rows based on multiple criteria - code is missing some of the criteria

I have a vba code that deletes rows based on multiple criteria. 我有一个VBA代码,可以根据多个条件删除行。 It works very well and I like how short and simple the code is. 它工作得很好,我喜欢代码的简短程度。 However, I have noticed that it somehow misses some of the cells that contain the criteria. 但是,我注意到它以某种方式错过了某些包含该标准的单元格。 Typically just three or so of the listed criteria I would like deleted. 通常,我只想删除所列标准中的三个左右。 Any clue why this may be happening?? 任何线索为什么会发生这种情况?

Here is the code: 这是代码:

Sub DeleteMyRows()

Dim Rng As Range, Cell As Range
Set Rng = Range(Range("B2"), Range("B" & Rows.Count).End(xlUp))
With Application
    .ScreenUpdating = False
    .EnableEvents = False
For Each Cell In Rng

If Cell = "ADD" Or Cell = "ANFR" Or Cell = "CADV" Or Cell = "DEF" Or Cell = "DEFD" Or Cell = "OIL" Or Cell = "PROP" Or Cell = "STAX" Or Cell = "UREA" Or Cell = "WWFL" Or Cell = "NGAS" Then
    Cell.EntireRow.Delete
End If

Next Cell
End With
End Sub

Thanks! 谢谢!

[Answer taken from a previous question here: IF-THEN w/ strings in VBA ] [此处的上一个问题的答案: 如果在VBA中使用字符串则为IF- ]

How 'identical' are the two strings? 这两个字符串有多“相同”? Is one capitalized and the other not? 一个大写,另一个大写吗? Does one have leading/trailing zeros? 一个是否有前导/尾随零? Does one have non-printable characters? 是否有不可打印的字符?

Try this, which turns Cell into CleanedCell before using as a comparison: 试试这个,将Cell变成CleanedCell,然后再进行比较:

CleanedCell = AllCleanedUp(Cell)

If CleanedCell = "ADD" Or CleanedCell = "ANFR" Or CleanedCell = "CADV" Or CleanedCell = "DEF" Or CleanedCell = "DEFD" Or CleanedCell = "OIL" Or CleanedCell = "PROP" Or CleanedCell = "STAX" Or CleanedCell = "UREA" Or CleanedCell = "WWFL" Or CleanedCell = "NGAS" Then
    Cell.EntireRow.Delete
End If

... ...

Which refers to the following function: 指的是以下功能:

Function AllCleanedUp (DirtyString byVal) As String

    AllCleanedUp = Trim(Application.Clean(Ucase(DirtyString)))

End Function

it may be that the values in Cell are in a different case, or have leading or trailing spaces. 可能是Cell中的值处于不同的情况下,或者具有前导或尾随空格。 (I often find this problem in manually input data) (我经常在手动输入数据中发现此问题)

try using: 尝试使用:

If UCase(Trim(Cell.Value)) = "ADD" Or UCase(Trim(Cell.Value)) = "ANFR" Or ....... Then

It could be one of the orther answers, but my guess is that the problem is that when you delete a row, "cell" becomes the next one under and you then hit the "next" and skip this row completly. 可能是其他答案之一,但我的猜测是问题是,当您删除一行时,“单元格”成为下一个下一行,然后您单击“下一个”并完全跳过该行。 For exemple, if you do: 例如,如果您这样做:

Sub test()
Dim cell
For Each cell In Range("A1", "A10")
    cell.EntireRow.Delete
Next cell
End Sub

You will only delete rows 1,3,5,7,9 您将只删除1,3,5,7,9行

You should loop in reverse, which as far as i'm aware is not possible with a for each cell in range(...) loop, but can be done by iterating over numbers like so: 您应该反向循环,据我所知,对于range(...)循环中的每个单元格都不可能用a来实现,但是可以通过像这样遍历数字来完成:

for i=cells(rows.count,"B").end(xlup).row to 2 step -1
    If Cells(i,"B") = "ADD" Or Cells(i,"B") = "ANFR" Or Cells(i,"B") = "CADV" Or Cells(i,"B") = "DEF" Or Cells(i,"B") = "DEFD" Or Cells(i,"B") = "OIL" Or Cells(i,"B") = "PROP" Or Cells(i,"B") = "STAX" Or Cells(i,"B") = "UREA" Or Cells(i,"B") = "WWFL" Or Cells(i,"B") = "NGAS" Then
        Cell.EntireRow.Delete
    End If
next i

i like to do : 我喜欢做 :

If Cell LIKE "*ADD*" Or Cell LIKE "*ANFR*" Or Cell LIKE "*CADV*" Or Cell LIKE "*DEF*" Or Cell LIKE "*DEFD*" Or Cell LIKE "*OIL*" Or Cell LIKE "*PROP*" Or Cell LIKE "*STAX*" Or Cell LIKE "*UREA*" Or Cell LIKE "*WWFL*" Or Cell LIKE "*NGAS*" Then
    Cell.EntireRow.Delete
End If

the star (*) means any letters, numbers, spaces... can be before or after (in my example) your given values. 星号(*)表示任何字母,数字,空格...可以在给定值的前面或后面(在我的示例中)。

  • this is case unsensitive, that's why i like it! 这是不区分大小写的,这就是为什么我喜欢它!

Hope this help. 希望能有所帮助。

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

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