Chris
0
Q:

excel vba check if multiple substrings in string

'VBA function to check if ALL of a list of substrings is contained
'within a string:

Function AllIn(s$, ParamArray checks()) As Boolean
    Dim e
    For Each e In checks
        If 0 = InStrB(s, e) Then Exit Function
    Next
    AllIn = True
End Function
  
'-------------------------------------------------------------------
  
MsgBox AllIn("abcde", "d", c, "a")   '<--displays: True
MsgBox AllIn("abcde", "d", c, "z")   '<--displays: False
5
'VBA function to check if ANY of a list of substrings is contained
'within a string:

Function AnyIn(s$, ParamArray checks()) As Boolean
    Dim e
    For Each e In checks
        If InStrB(s, e) Then AnyIn = True: Exit Function
    Next
End Function
  
'-------------------------------------------------------------------
  
MsgBox AnyIn("abcde", "o", 5, "z", "a")   '<--displays: True
MsgBox AnyIn("abcde", "o", 5, "z", "p")   '<--displays: False
3

New to Communities?

Join the community