ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   If ... < or ... < then statement (https://www.excelbanter.com/excel-programming/277320-if-then-statement.html)

zsplash

If ... < or ... < then statement
 
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



Jean-Paul Viel

If ... < or ... < then statement
 
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.






--
JP

http://www.solutionsvba.com


"zsplash" wrote in message
...
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





Chip Pearson

If ... < or ... < then statement
 
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


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
www.cpearson.com


"zsplash" wrote in message
...
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





Doug Glancy[_2_]

If ... < or ... < then statement
 
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


"zsplash" wrote in message
...
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





zsplash

If ... < or ... < then statement
 
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.




Doug Glancy[_2_]

If ... < or ... < then statement
 
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


"zsplash" wrote in message
...
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.







All times are GMT +1. The time now is 05:19 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com