删除所有没有值的行:条件1或条件2或D列中的条件3 - >使用VBA宏for excel

时间:2013-10-30 21:40:48

标签: excel vba

我是Macro's / VBA的新手,似乎无法弄清楚如何针对多个条件执行此操作。我正在尝试删除所有没有值的行:识别列D中的失败或识别成功。我编写以下代码,它适用于一个标准“识别失败”:

“的 * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ****

Sub DeleteRows()
'Action 1 --> Delete all Rows without Identify Fail in column D'
Application.ScreenUpdating = False

For i = Range("D" & Rows.Count).End(xlUp).Row To 1 Step -1
    If Range("D" & i).Value <> "Identify Fail" Then Rows(i).Delete shift:=xlUp
Next i

Application.ScreenUpdating = True
End Sub

“的 * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ****

一旦我尝试使用其他标准添加“OR”,它就不起作用:

“的 * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ****

Sub DeleteRows()
'Action 1 --> Delete all Rows without Identify Fail in column D'
Application.ScreenUpdating = False

For i = Range("D" & Rows.Count).End(xlUp).Row To 1 Step -1
    If Range("D" & i).Value <> "Identify Fail" Or "Identify Success" Then   
Rows(i).Deleteshift:=xlUp
Next i

Application.ScreenUpdating = True
End Sub

“的 * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ****

任何建议都会受到赞赏,因为我一直在搜索这个网站,并且没有找到能够解决问题的高效代码。

1 个答案:

答案 0 :(得分:1)

更改此行

 If Range("D" & i).Value <> "Identify Fail" Or "Identify Success" Then 

If Range("D" & i).Value <> "Identify Fail" Or _ 
   Range("D" & i).Value <> "Identify Success" Then 

你必须完整地指定条件..

然而这些条件相互制约,所以它总是会通过..我认为你的意思是使用

If Range("D" & i).Value <> "Identify Fail" And _ 
   Range("D" & i).Value <> "Identify Success" Then

在这个例子中,我将“身份失败”添加到D10

Example screenprint