If ... <> or ... <> then statement

  • Thread starter Thread starter zsplash
  • Start date Start date
Z

zsplash

I think I must not understand the "or" (and <>) qualities of code, in an
If-Then statement.



Here's my code:

If MyType <> "DIV" Or MyType <> "UNM" Or MyType <> "MOD" Then
blah, blah, blah

End If



When MyType = "DIV", the code progresses to the [blah, blah, blah]. My
little brain is trying to think that if MyType = "DIV", the code should
bypass the [blah, blah, blah].



Please enlighten me.



TIA
 
Hi,



With "or" at least one of them must be true to get an over all result to be
true.



So if MyType ="DIV" then the first condition is false but the other two are
true then the true is executed.
 
When MyType equals "DIV", MyType does not equal "UNM" and it does not equal
"MOD", so the entire statement becomes

If FALSE Or TRUE Or TRUE Then
blah blah
End If

or

If TRUE Then
blah blah
End If

In fact, your code will always go in to the Then statement, regardless of
the contents of MyType. In spoken English, we tend to use Or in this sort
of statement when we really mean And. You don't want Or's here, you want
And's.

If MyType <> "DIV" And MyType <> "UNM" And MyType <> "MOD" Then
 
If you want it to bypass the blah in each of the three cases, you need to
change the ORs to ANDs:

If MyType <> "DIV" And MyType <> "UNM" And MyType <> "MOD" Then
blah, blah, blah
End If

In your code, when MyType is equal to DIV it's not equal to the other two,
and so the overall statement evaluates to True and blah is executed.

Sometimes I find a Select Case statement to be clearer:

Select Case MyType
Case "DIV", "UNM", "MOD"
'do nothing
Case Else
blah
End Select

hth,

Doug
 
You guys are too wonderful. When explained so well (as you each did), it
all sounds so reasonable/logical. Thanks so much (and for the quick
response).

st.
 
Yes, this group is amazing! I've learned so much in the last year, it's
great to be able to return the favor.

Doug
 
Back
Top