Compact Syntax for OR Condition

D

DevDaniel

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)
 
B

BeWyched

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
 
D

Dirk Goldgar

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.
 
D

Douglas J. Steele

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
 
Top