+ Reply to Thread
Results 1 to 5 of 5

VBA equivalent for SQL 'IN' function

  1. #1
    Mitch
    Guest

    VBA equivalent for SQL 'IN' function

    SQL, SAS and other 'languages' have a set function that allows the developer
    to determine whether a variable's value exists within a set identified in the
    'IN' statement; e.g. If myVar in ("A","B","C") then .....
    Is there a VBA equivalent?

  2. #2
    Tim Williams
    Guest

    Re: VBA equivalent for SQL 'IN' function

    msgbox
    Application.WorksheetFunction.match("help",Array("this","might","help"),0)

    Tim

    --
    Tim Williams
    Palo Alto, CA


    "Mitch" <[email protected]> wrote in message
    news:[email protected]...
    > SQL, SAS and other 'languages' have a set function that allows the

    developer
    > to determine whether a variable's value exists within a set identified in

    the
    > 'IN' statement; e.g. If myVar in ("A","B","C") then .....
    > Is there a VBA equivalent?




  3. #3
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187
    I am not sure of a direct equivalent but three options:
    1. You could use a delimited list and then

    if instr(1, "!A!B!C!" , "!" & myvar & "!") <> 0 then etc

    2. use

    Select Case myvar
    Case "A", "B", "C"
    'do my stuff
    End Select


    3 create your own function to do it such as

    Function myIN(strSearch, ParamArray strIN()) As Boolean
    Dim v As Variant

    myIN = False

    For Each v In strIN()
    If strSearch = v Then
    myIN = True
    Exit For
    End If
    Next
    End Function

    if myin(myvar,"B","C","A") then

    Regards

  4. #4
    Mitch
    Guest

    Re: VBA equivalent for SQL 'IN' function

    Thank you Tim. I'll see if I can make it work.

    "Tim Williams" wrote:

    > msgbox
    > Application.WorksheetFunction.match("help",Array("this","might","help"),0)
    >
    > Tim
    >
    > --
    > Tim Williams
    > Palo Alto, CA
    >
    >
    > "Mitch" <[email protected]> wrote in message
    > news:[email protected]...
    > > SQL, SAS and other 'languages' have a set function that allows the

    > developer
    > > to determine whether a variable's value exists within a set identified in

    > the
    > > 'IN' statement; e.g. If myVar in ("A","B","C") then .....
    > > Is there a VBA equivalent?

    >
    >
    >


  5. #5
    Mitch
    Guest

    Re: VBA equivalent for SQL 'IN' function

    Thanks Tony. I think that the 3rd method will work.

    "tony h" wrote:

    >
    > I am not sure of a direct equivalent but three options:
    > 1. You could use a delimited list and then
    >
    > if instr(1, "!A!B!C!" , "!" & myvar & "!") <> 0 then etc
    >
    > 2. use
    >
    > Select Case myvar
    > Case "A", "B", "C"
    > 'do my stuff
    > End Select
    >
    >
    > 3 create your own function to do it such as
    >
    > Function myIN(strSearch, ParamArray strIN()) As Boolean
    > Dim v As Variant
    >
    > myIN = False
    >
    > For Each v In strIN()
    > If strSearch = v Then
    > myIN = True
    > Exit For
    > End If
    > Next
    > End Function
    >
    > if myin(myvar,"B","C","A") then
    >
    > Regards
    >
    >
    > --
    > tony h
    > ------------------------------------------------------------------------
    > tony h's Profile: http://www.excelforum.com/member.php...o&userid=21074
    > View this thread: http://www.excelforum.com/showthread...hreadid=507480
    >
    >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1