Consulting

Results 1 to 3 of 3

Thread: if..............in

  1. #1
    VBAX Tutor
    Joined
    Nov 2007
    Posts
    291
    Location

    if..............in

    Hi, Is there a way in Excel VBA to do somthing like :- If myVariable In ["eggs", "ham", "potato"] or do you have to have lots of "OR" statements Thanks Phil

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]
    Dim myArray As Variant
    Dim myVariable As String
    Dim aryIdx As Long

    myArray = Array("eggs", "ham", "potato")
    myVariable = "eggs"

    On Error Resume Next
    aryIdx = Application.Match(myVariable, myArray, 0)
    On Error GoTo 0

    If aryIdx > 0 Then

    MsgBox "Found in item " & aryIdx
    End If
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Another approach might be

    [VBA]Select Case myVariable
    Case "pear", "apple", "peach"
    MsgBox "vegetable"

    Case "steak", "bacon", "sausage"
    MsgBox "meat"

    End Select[/VBA]xld's method is better if you only want "is it in the bucket, True/False". Select Case would be used if the question is "which bucket is it in"?

Posting Permissions

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