Find an Element In an Array

Search

 by Remas Wojciechowski

This function returns for a given array [aSource] the index of a string [strValue] starting from [intStartIndex]. If the string is not found, the value -1 is returned.

Function ARRAY_GetIndex(ByVal aSource, ByVal strValue, ByVal intStartIndex)
   Dim intLB, intUB
   Dim i
   Dim intMatch
   intMatch = -1
   intLB = LBound(aSource)
   intUB = UBound(aSource)
   For i = Max(intLB, intStartIndex) To intUB
      If CStr(aSource(i)) = CStr(strValue) Then
         intMatch = i
         Exit For
      End If
   Next
   ARRAY_GetIndex = intMatch
End Function

Important note

For some reason the Max function is gone in the latest VBScript version. For the function ARRAY_GetIndex to work, you'll need to add an implementation for Max. Below is my implementation:

Function Max(ByVal a, ByVal b)
  If a > b Then
    Max = a
  Else
    Max = b
  End If
End Function