Harry Flashman
Active Member
- Joined
- May 1, 2011
- Messages
- 361
I have a test to see if a text string is in an array that mostly works, but sometimes fails and I don't understand why. Would someone please look over my code and tell me where I have gone wrong?
The routine calls a Function IsInArray which returns a True/False value depending on whether the search term is in the array.
The above routine correctly gives the message "Yes! The item is in the array"
The above routine correctly gives the message "No! The item is not in the array"
So far so good.
But this is where it fails:
The above routine returns the message "Yes! The item is in the array" when I think it should return the message "No! The item is not in the array.
I am searching for the string "a & z" and as we can see the string "a & z" is NOT in the array.
In a similar vein the following also returns the wrong response:
In this case the first item in my array is "a - b" (note that this is a text string ), but for some reason because my test value is "a" the routine things it must be in the array.
Thus I am thinking that the ampersand and dash symbols are somehow confusing my routine.
What is going wrong? How can I change my routine so that it correctly identifies which text strings are in my array?
Any help would be greatly appreciated.
The routine calls a Function IsInArray which returns a True/False value depending on whether the search term is in the array.
Code:
Sub TestFilterArray()
MyArray = Array("a", "b", "c")
If IsInArray("a", MyArray) = False Then
MsgBox "No! Item is not in the array"
Else
MsgBox "Yes! Item is in the array"
End If
End Sub
Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = UBound(Filter(arr, stringToBeFound)) > -1
End Function
The above routine correctly gives the message "Yes! The item is in the array"
Code:
Sub TestFilterArray()
MyArray = Array("a", "b", "c")
If IsInArray("z", MyArray) = False Then
MsgBox "No! Item is not in the array"
Else
MsgBox "Yes! Item is in the array"
End If
End Sub
Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = UBound(Filter(arr, stringToBeFound)) > -1
End Function
The above routine correctly gives the message "No! The item is not in the array"
So far so good.
But this is where it fails:
Code:
Sub TestFilterArray()
MyArray = Array("a", "b", "c")
If IsInArray("a & z", MyArray) = False Then
MsgBox "No! Item is not in the array"
Else
MsgBox "Yes! Item is in the array"
End If
End Sub
Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = UBound(Filter(arr, stringToBeFound)) > -1
End Function
The above routine returns the message "Yes! The item is in the array" when I think it should return the message "No! The item is not in the array.
I am searching for the string "a & z" and as we can see the string "a & z" is NOT in the array.
In a similar vein the following also returns the wrong response:
Code:
Sub TestFilterArray()
MyArray = Array("a - b", "b", "c")
If IsInArray("a", MyArray) = False Then
MsgBox "No! Item is not in the array"
Else
MsgBox "Yes! Item is in the array"
End If
End Sub
Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = UBound(Filter(arr, stringToBeFound)) > -1
End Function
In this case the first item in my array is "a - b" (note that this is a text string ), but for some reason because my test value is "a" the routine things it must be in the array.
Thus I am thinking that the ampersand and dash symbols are somehow confusing my routine.
What is going wrong? How can I change my routine so that it correctly identifies which text strings are in my array?
Any help would be greatly appreciated.
Last edited: