Search code examples
excelvbastringsubstring

Is it possible to use cells in vba function in arrays place?


I saw a function here that allowed me to identify substrings in a given string, however I don´t know how to use that function in a worksheet with cells I select. Here is the code:

Function IsInArray2(StringToBeFound As String, MyArray As Variant) As Boolean
IsInArray2 = False
For i = LBound(MyArray) To UBound(MyArray)
    If LCase(StringToBeFound) Like LCase("*" & MyArray(i) & "*") Then
    IsInArray2 = True 'will match MyArray to any substring of StringToBeFound
    Exit Function
    End If
Next
End Function

I want to select a cell to define my "StringToBeFound" and a range to define "MyArray"

Any help gratefully appreciated :)


Solution

  • You could use:

    Function IsInArray2(StringToBeFound As String, MyArray As Range) As Boolean
    IsInArray2 = False
    dim cell as range
    For each cell in myarray.cells
        If LCase(StringToBeFound) Like LCase("*" & cell.value & "*") Then
        IsInArray2 = True 'will match MyArray to any substring of StringToBeFound
        Exit Function
        End If
    Next
    End Function