Reputation: 5769
I've got some code below, that is supposed to be checking if a value is in an Array or not.
Sub test()
vars1 = Array("Examples")
vars2 = Array("Example")
If IsInArray(Range("A1").Value, vars1) Then
x = 1
End If
If IsInArray(Range("A1").Value, vars2) Then
x = 1
End If
End Sub
Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function
If the cell A1
contains the word Examples
for some reason both of the IsInArray
detects it as existing for both Arrays when it should only find it existing in the vars1
array
What do I need to change to make my IsInArray
function to make it an exact match?
Upvotes: 37
Views: 308904
Reputation: 49
The below function returns '0' if there is no match and a 'positive integer' in case of matching:
Function IsInArray(stringToBeFound As String, arr As Variant) As Integer
IsInArray = InStr(Join(arr, ""), stringToBeFound)
End Function
Note: the function first concatenates the entire array content to a string using 'Join' (not sure if the join method uses looping internally or not) and then checks for a match within this string using InStr
.
Upvotes: 4
Reputation: 79
I searched for this very question and when I saw the answers I ended up creating something different (because I favor less code over most other things most of the time) that should work in the vast majority of cases. Basically turn the array into a string with array elements separated by some delimiter character, and then wrap the search value in the delimiter character and pass through instr.
Function is_in_array(value As String, test_array) As Boolean
If Not (IsArray(test_array)) Then Exit Function
If InStr(1, "'" & Join(test_array, "'") & "'", "'" & value & "'") > 0 _
Then is_in_array = True
End Function
And you'd execute the function like this:
test = is_in_array(1, array(1, 2, 3))
Upvotes: 7
Reputation: 4745
I would like to provide another variant that should be both performant and powerful, because
Match
)String
, Integer
, Boolean
etc. (not String
-only)...
'-1 if not found
'https://stackoverflow.com/a/56327647/1915920
Public Function IsInArray( _
item As Variant, _
arr As Variant, _
Optional nthOccurrence As Long = 1 _
) As Long
IsInArray = -1
Dim i As Long: For i = LBound(arr, 1) To UBound(arr, 1)
If arr(i) = item Then
If nthOccurrence > 1 Then
nthOccurrence = nthOccurrence - 1
GoTo continue
End If
IsInArray = i
Exit Function
End If
continue:
Next i
End Function
use it like this:
Sub testInt()
Debug.Print IsInArray(2, Array(1, 2, 3)) '=> 1
End Sub
Sub testString1()
Debug.Print IsInArray("b", Array("a", "b", "c", "a")) '=> 1
End Sub
Sub testString2()
Debug.Print IsInArray("b", Array("a", "b", "c", "b"), 2) '=> 3
End Sub
Sub testBool1()
Debug.Print IsInArray(False, Array(True, False, True)) '=> 1
End Sub
Sub testBool2()
Debug.Print IsInArray(True, Array(True, False, True), 2) '=> 2
End Sub
Upvotes: 3
Reputation: 3263
While this is essentially just @Brad's answer again, I thought it might be worth including a slightly modified function which will return the index of the item you're searching for if it exists in the array. If the item is not in the array, it returns -1
instead.
The output of this can be checked just like the "in string" function, If InStr(...) > 0 Then
, so I made a little test function below it as an example.
Option Explicit
Public Function IsInArrayIndex(stringToFind As String, arr As Variant) As Long
IsInArrayIndex = -1
Dim i As Long
For i = LBound(arr, 1) To UBound(arr, 1)
If arr(i) = stringToFind Then
IsInArrayIndex = i
Exit Function
End If
Next i
End Function
Sub test()
Dim fruitArray As Variant
fruitArray = Array("orange", "apple", "banana", "berry")
Dim result As Long
result = IsInArrayIndex("apple", fruitArray)
If result >= 0 Then
Debug.Print chr(34) & fruitArray(result) & chr(34) & " exists in array at index " & result
Else
Debug.Print "does not exist in array"
End If
End Sub
Then I went a little overboard and fleshed out one for two dimensional arrays because when you generate an array based on a range it's generally in this form.
It returns a single dimension variant array with just two values, the two indices of the array used as an input (assuming the value is found). If the value is not found, it returns an array of (-1, -1)
.
Option Explicit
Public Function IsInArray2DIndex(stringToFind As String, arr As Variant) As Variant
IsInArray2DIndex= Array(-1, -1)
Dim i As Long
Dim j As Long
For i = LBound(arr, 1) To UBound(arr, 1)
For j = LBound(arr, 2) To UBound(arr, 2)
If arr(i, j) = stringToFind Then
IsInArray2DIndex= Array(i, j)
Exit Function
End If
Next j
Next i
End Function
Here's a picture of the data that I set up for the test, followed by the test:
Sub test2()
Dim fruitArray2D As Variant
fruitArray2D = sheets("Sheet1").Range("A1:B2").value
Dim result As Variant
result = IsInArray2DIndex("apple", fruitArray2D)
If result(0) >= 0 And result(1) >= 0 Then
Debug.Print chr(34) & fruitArray2D(result(0), result(1)) & chr(34) & " exists in array at row: " & result(0) & ", col: " & result(1)
Else
Debug.Print "does not exist in array"
End If
End Sub
Upvotes: 1
Reputation: 1975
Use Match() function in excel VBA to check whether the value exists in an array.
Sub test()
Dim x As Long
vars1 = Array("Abc", "Xyz", "Examples")
vars2 = Array("Def", "IJK", "MNO")
If IsNumeric(Application.Match(Range("A1").Value, vars1, 0)) Then
x = 1
ElseIf IsNumeric(Application.Match(Range("A1").Value, vars2, 0)) Then
x = 1
End If
MsgBox x
End Sub
Upvotes: 7
Reputation: 12245
You can brute force it like this:
Public Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
Dim i
For i = LBound(arr) To UBound(arr)
If arr(i) = stringToBeFound Then
IsInArray = True
Exit Function
End If
Next i
IsInArray = False
End Function
Use like
IsInArray("example", Array("example", "someother text", "more things", "and another"))
Upvotes: 62
Reputation:
This Question was asked here: VBA Arrays - Check strict (not approximative) match
Sub test()
vars1 = Array("Examples")
vars2 = Array("Example")
If IsInArray(Range("A1").value, vars1) Then
x = 1
End If
If IsInArray(Range("A1").value, vars2) Then
x = 1
End If
End Sub
Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = Not IsError(Application.Match(stringToBeFound, arr, 0))
End Function
Upvotes: 17
Reputation: 124
You want to check whether Examples exists in Range("A1").Value If it fails then to check Example right? I think mycode will work perfect. Please check.
Sub test()
Dim string1 As String, string2 As String
string1 = "Examples"
string2 = "Example"
If InStr(1, Range("A1").Value, string1) > 0 Then
x = 1
ElseIf InStr(1, Range("A1").Value, string2) > 0 Then
x = 2
End If
End Sub
Upvotes: -1