If statement with "OR"

L

Laurence Lombard

Is there a shorthand way of working "OR" into an if statement in a
similar as used in the CASE statement

if Person = "Peter", "Paul", "John" then...

along the lines of
Select Case Person
Case "Peter","Paul","John"...

if Person = "Peter" or Person = "Paul" or Person = "John" then...
is a bit bulky
 
J

joeu2004

Laurence Lombard said:
Is there a shorthand way of working "OR" into an
if statement in a similar as used in the CASE statement
if Person = "Peter", "Paul", "John" then...
along the lines of
Select Case Person
Case "Peter","Paul","John"...

Not that I know of. In fact, Select Case __is__ the "shorthand" that I use
for that purpose.

Even if there were a "shorthand" form for an If statement, it probably would
be implemented inefficiently.

In VBA, the If conditional expression is fully evaluated. It is not
evaluated left-to-right only as far as necessary, as is the case in some
other computer languages. So:

If Person = "Peter" Or Person = "Paul" Or Person = "John" Then

performs 3 comparisons and ORs the results before determining whether or not
to execute the Then part.

In contrast:

Select Person
Case "Peter", "Paul", "John"

tests each case in order, and it stops the comparisons and goes to the
appropriate case as soon as a true condition is encountered.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top