I am confused about this case I came across. I Have a code:
Sub Testing()
Dim UnitArray
UnitArray = Array("Standard", "Jumbo")
Dim Unit As String
Unit = "Standard"
If IsInArray(UnitArray, Unit) Then
MsgBox ("Great success")
End If
End Sub
And a function:
Function IsInArray(arr As Variant, myVal As Variant) As Boolean
Dim mtch
mtch = Application.Match(myVal, arr, True)
If Not IsError(mtch) Then
If mtch = UBound(arr) Then
If arr(UBound(arr)) = myVal Then IsInArray = True: Exit Function
Else
IsInArray = True: Exit Function
End If
End If
IsInArray = False
End Function
If that array has only "Standard" - it works, but else it doesn't find the value. What's the mistake I have made?
If IsInArray(UnitArray, "Jumbo") Thenand it does not returnTrue? "I am confused" does not let us understand what confusion are you talking about... What did you try, what were your expectations against the return?If mtch = UBound(arr) ThenisTruebecausearris 0-based butMatchis 1-based. So it appears that"Standard"is not in the array. But really, this function is overkill for what OP wants to do.Matchis much slower than looping the array elements. stackoverflow.com/questions/18754096/…Integers, if I remember well. Only in such a caseMatchmay return the last array element if not a match. For strings, it should return a match, even if the array is 1D or 2D type...mtch = Application.Match(myVal, arr, 0)probably.