Thanks Thanks:  0
Likes Likes:  0
Results 1 to 2 of 2

Thread: check if value exists in a vba array

  1. #1
    Board Regular
    Join Date
    Jul 2002
    Posts
    78
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default check if value exists in a vba array

    I want to check if a value exists in an array. And if so, take action.
    I do that with the following code. But I don't think this is an elegant way of doing so :

    For Each k In projects()
    If k = "var" then
    'marker for value in array
    l = 1
    Else
    End If
    Next k

    If l = 1 Then
    **** do something *****
    End If

    anyone ?

  2. #2
    MrExcel MVP
    Moderator
    Andrew Poulsom's Avatar
    Join Date
    Jul 2002
    Posts
    73,092
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    You can use the Match WorksheetFunction like this:

    Code:
    Sub Test()
        Const var As String = "var"
        Dim projects As Variant
        Dim x As Long
        projects = Array("x", "var", "y")
        On Error Resume Next
        x = WorksheetFunction.Match(var, projects, False)
        If Err = 0 Then
    '       **** do something ****
            MsgBox "Found"
        Else
    '       **** clear error ****
            Err.Clear
            MsgBox "Not Found"
        End If
        On Error GoTo 0
    End Sub

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
This website uses cookies
We use cookies to store session information to facilitate remembering your login information, to allow you to save website preferences, to personalise content and ads, to provide social media features and to analyse our traffic. We also share information about your use of our site with our social media, advertising and analytics partners.
     


DMCA.com