If you want to find out if a string is found in the array at all, try this function:
Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1) End Function
As Sean Cheshire points out, this should be a 1-dimensional array.
Example:
Sub Test() Dim arr As Variant arr = Split("abc,def,ghi,jkl", ",") Debug.Print IsInArray("ghi", arr) End Sub
(Below code updated based on HansUp comment)
If you need the index of the corresponding element in the array, try the following:
Function IsInArray(stringToBeFound As String, arr As Variant) As Long Dim i As Long ' default return value if value not found in array IsInArray = -1 For i = LBound(arr) To UBound(arr) If StrComp(stringToBeFound, arr(i), vbTextCompare) = 0 Then IsInArray = i Exit For End If Next i End Function
It also assumes a 1-dimensional array. Keep in mind that LBound and UBound are zero based, so index 2 means the third element, not the second.
Example:
Sub Test() Dim arr As Variant arr = Split("abc,def,ghi,jkl", ",") Debug.Print (IsInArray("ghi", arr) > -1) End Sub
If you have a specific example, please update your question, otherwise the example code may not match your situation.