I need to be able to check whether some strings are in an array in VBA. I've seen this question and tried both the brute force method:
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
and this shorter method:
Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = Not IsError(Application.Match(stringToBeFound, arr, 0))
End Function
My problem is that both result in a ByRef argument type mismatch error.
Here's the segment where I call the function:
Dim ar As Variant
Dim curAr As Variant
Dim curLastRow As Integer
With ThisWorkbook.Worksheets("B Averages")
lastRowAverages = .Range("A" & .Rows.Count).End(xlUp).row
End With
ar = ThisWorkbook.Worksheets("B Averages").Range("A2:A" & lastRowAverages).Value
For Each Sheet In ThisWorkbook.Sheets
If InStr(Sheet.Name, "B") And InStr(Sheet.Name, "Chart") Then
With Sheet
curLastRow = .Range("A" & .Rows.Count).End(xlUp).row
End With
curAr = Sheet.Range("A2:A" & curLastRow).Value
For Each part In curAr
If Not isInArray(part, ar) Then ' part not in main list of parts, append it
ThisWorkbook.Worksheets("B Averages").Range("A" & lastRowAverages + 1) = part
End If
Next part
End If
Next Sheet
I've tried printing the Types of part and ar with VarType(), and it tells me that the part is 8, which does correspond to String and that ar is 8204. I'm not not sure what to make of this last piece of information, however. The list of return types for VarType() says that arrays should be 8192, but I don't know how to get an array to have that exact type or if that's even what I need to do. I've also seen this question but don't really know what to do with the information.
I've seen lots of other questions about ByRef argument type mismatch errors but haven't found any hints as to how to get around this.
Is there a way around these errors?

If Not isInArray(CStr(part), ar) Then