|
One
way to determine if an array contains an element is to use a simple
'For ... Next' loop to iterate through each element of the
array, checking to see if the element is equal to a particular
string. For example:
Dim
iLoop, bolFound
bolFound = Fals
For
iLoop = LBound(SomeArray) to UBound(SomeArray)
If CStr(SomeArray(iLoop)) = CStr(SomeString) then
bolFound = True
End If
Next
When
this code completes, bolFound will be True if SomeString is found
in SomeArray, and False if not found. This code performs a case-sensitive
comparison, however. So, if you are searching for "FooBar" and
the array contains an element equal to "Foobar," the
bolFound will be False.
We
can create a code to determine the position of a particular string
element in an array by using the PositionInArray function:
Function
ElementInArray(aMyArray, strLookingFor, compare)
'Returns True if strLookingFor is in aMyArray, False otherwise
ElementInArray = (PositionInArray(aMyArray, strLookingFor, compare) > -1)
End Functio
We
can also use the Filter function to accomplish a similar task.
Function
ElementInArray(aMyArray, strLookingFor, compare)
'Returns True if strLookingFor is in aMyArray, False otherwise
ElementInArray = (UBound(Filter(aMyArray, strLookingFor, True, compare)) > -1)
End Functio
These
three techniques will help you to quickly determine if a particular string exists
in an array.
|