How can I test if a specific string is in an array (of strings) in VBA?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mattk62586
    New Member
    • Jul 2010
    • 1

    How can I test if a specific string is in an array (of strings) in VBA?

    So say I have an array which contains an arbitrary number of strings:

    Code:
    Dim someArray() As String
    Dim someStr As String
    Dim c as Boolean
    Now I want to test someStr for membership to someArray.
    Is there some method like this I can use?

    Code:
    c = someStr.IsMemeber(someArray)
    or maybe:

    Code:
    c = someArray.Contains(someStr)
    Thanks.
  • vb5prgrmr
    Recognized Expert Contributor
    • Oct 2009
    • 305

    #2
    Looks like you have been looking at a bunch of .NET code...

    You will need to create a function for this in VB6.0...

    Something like...

    Code:
    Private Function DoesStringExistInArray(StringToCheckFor, As String, ArrayToCheck() As String) As Boolean
    
    On Error GoTo DoesStringExistInArrayError
    
    Dim LB As Integer, UB As Integer, LoopCnt As Integer
    
    'Check to see if the array is a dynamic array that has not been dimensioned yet
    If Not ArrayToCheck Then Exit Function 'no need to see function to false as a function is by default = to false
    
    'okay, if we are here then the array has been dimensioned, so retrieve its bounds
    LB = LBound(ArrayToCheck)
    UB = UBound(ArrayToCheck)
    
    'now loop through array to see if string exists...
    For LoopCnt = LB To UB
      If StringToCheckFor = ArrayToCheck(LoopCnt) Then
        DoesStringExistInArray = True
        Exit Function
      End If
    Next LoopCnt
    
    Exit Function
    DoesStringExistInArrayError:
    
    MsgBox "FormModuleName.DoesStringExistInArray " & Err.Number & ":" & Err.Description
    
    End Function


    Good Luck

    Comment

    Working...