Compact Syntax for OR Condition

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have: a long list of OR conditions
e.g., var = apples OR var = bananas OR var = oranges

Is there a compact way of expressing this, like in SQL
e.g., var In (apples, bananas, oranges)
 
Where are you using the OR line?

If it is in an 'If' statement then use Select instead:

Select Case var
Case "apples", "bananas", "oranges"
....coding goes here
Case Else
.... more coding here
End Select

Cheers.

BW
 
In
DevDaniel said:
I have: a long list of OR conditions
e.g., var = apples OR var = bananas OR var = oranges

Is there a compact way of expressing this, like in SQL
e.g., var In (apples, bananas, oranges)

In VBA there's no "In" operator, unfortunately. A Select statement
might be somewhat more compact than a long list of "Or"s:

Select Case var
Case apples, bananas, oranges
' do something
End Select

Or you could use an InStr expression like this:

If InStr("/" & var & "/", "/apples/bananas/oranges/") > 0 Then

In such an expression, you have to choose a delimiter ("/" in the
example above) that doesn't occur in any of the values to be checked.
 
Dirk Goldgar said:
Or you could use an InStr expression like this:

If InStr("/" & var & "/", "/apples/bananas/oranges/") > 0 Then

In such an expression, you have to choose a delimiter ("/" in the example
above) that doesn't occur in any of the values to be checked.

Shouldn't that be

If InStr("/apples/bananas/oranges/", "/" & var & "/") > 0 Then
 

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

Back
Top