Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Imitating the "IN" Operator

How can one achieve:

if X in (1,2,3) then 

instead of:

if x=1 or x=2 or x=3 then 

In other words, how can one best imitate the IN operator in VBA for excel?

like image 899
Allan Bowe Avatar asked Oct 01 '09 17:10

Allan Bowe


2 Answers

I don't think there is a very elegant solution.

However, you could try:

If Not IsError(Application.Match(x, Array("Me", "You", "Dog", "Boo"), False)) Then 

or you could write your own function:

Function ISIN(x, StringSetElementsAsArray)     ISIN = InStr(1, Join(StringSetElementsAsArray, Chr(0)), _     x, vbTextCompare) > 0 End Function  Sub testIt()     Dim x As String     x = "Dog"     MsgBox ISIN(x, Array("Me", "You", "Dog", "Boo")) End Sub 
like image 160
Kredns Avatar answered Oct 26 '22 09:10

Kredns


You could also try the CASE statement instead of IF

Select Case X   Case 1 To 3      ' Code to do something  Case 4, 5, 6   ' Code to do something  Case 7   ' Code to do something  Case Else     ' More code or do nothing  End Select 
like image 41
Robert Mearns Avatar answered Oct 26 '22 07:10

Robert Mearns