PDA

View Full Version : Solved: VBA If statement using and OR function



JimS
06-21-2012, 07:06 AM
Trying to get an IF statement to test for 2 different condition using an OR function.

Basically delete the row if the contents of the cell does not contain "test1" or "test2" in my example below.

I loop through the rows using x (looping code is not included below).

Thanks for any quick help...

JimS




If Cells(x, "A") <> "test1" Or Cells(x, "A") <> "test2" Then
Cells(x, "A").EntireRow.Delete

End If

VoG
06-21-2012, 07:18 AM
You need to change Or to And

snb
06-21-2012, 07:36 AM
sub snb()
with cells(1).currentregion.columns(1)
.autofilter 1, "<>test1", xland, "<>test2"
.offset(1).specialcells(12).entirerow.delete
.autofilter
end with
end sub

JimS
06-21-2012, 07:38 AM
VoG,

Thanks. I feel pretty stupid now - so everything is normal...

Thanks again...

JimS

CodeNinja
06-21-2012, 08:09 AM
JimS,
Don't feel stupid... I argued with the lead programmer at my company for almost a half an hour trying to convince him he had a bug doing almost the exact same thing... this is a guy that has programmed for 30 + years, and he made the same mistake. Those <> statements get confusing...
I did finally convince him he was wrong :)