Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何根据Excel vba的条件删除表中的行_Excel_Vba - Fatal编程技术网

如何根据Excel vba的条件删除表中的行

如何根据Excel vba的条件删除表中的行,excel,vba,Excel,Vba,问题很简单,但我找不到任何有效的解决办法。 我有一张桌子。我想在满足某些条件时循环并删除整行。 我使用的代码如下所示: Range("H3").Select Range(Selection, Selection.End(xlDown)).Select Dim cellule As Range For Each cellule In Selection.Cells If cellule.Value = "--" And ...(More condition) Then ce

问题很简单,但我找不到任何有效的解决办法。 我有一张桌子。我想在满足某些条件时循环并删除整行。 我使用的代码如下所示:

Range("H3").Select
Range(Selection, Selection.End(xlDown)).Select
Dim cellule As Range
For Each cellule In Selection.Cells    
If cellule.Value = "--" And ...(More condition) Then
        cellule.EntireRow.Delete
    End If
Next cellule

它实际上是有效的,但是当两个连续的行满足这些条件时,第二行就不会被删除,因为第一行被删除时,第二行已经上升了。基本上跳过了第二个

如果需要,您可以不使用循环执行此操作:

首先,在空列中添加一个公式,如果您的条件为真,该公式将显示#NA错误:

sFormula = "=IF(H:H=""- -"",NA(),"""")" ' could combine conditions using AND Excel worksheet functionn

' put the formula in column K for all rows, if the condition in col H is met, 
' this row will show an #NA error in column K   
Range("K2:K" & Range("H" & rows.count).End(xlUp).Row).Formula = sFormula

' now match all the rows using SpecialCells in one step, and delete!
Range("K5:H" & Range("H" & rows.count).End(xlUp).Row).SpecialCells(xlCellTypeFormulas, xlErrors).entirerow.delete shift:=xlup
这类似于在Excel中按F5键并单击“特殊单元格”,然后选择“错误”


让我们知道你是如何开始的

我实际上找到了使用Do…循环的解决方案,而这种方法在这种情况下更适合。当删除行时,你应该自下而上进行操作,这样你就不会遇到你描述的问题。这也是一个非常简单的好问题。我从来没有想过。感谢tipalso,您可以使用AutoFilter或SpecialCells功能在不使用循环的情况下完成此操作。看一看: