ISLIKE function in excel

116 views Asked by At

I created in VBA the following custom function and I'm using it in excel.

Public Function ISLIKE(ByVal sText As String, ByVal sPattern As String) As Boolean 

   If sText Like sPattern Then 

ISLIKE = True 

   Else 

ISLIKE = False 

   End If 

End Function 

The problem I have now is that I would like to check if a string is in the form of:

  1. First character is a number
  2. Second character is either a 'b' or a 's'
  3. Characters from the third onwards are optional

I can use '#' for the first character and '*' for the third character onwards but how can I specify whether the second character is either a 'b' or a 's'?

Thanks who those will reply.

Gabriele

1

There are 1 answers

0
Tim Williams On

Like "#[abc]*" would match any string starting with a digit followed by any one of the characters inside the square brackets, and then any other [optional] content.

See docs: https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/wildcard-characters-used-in-string-comparisons

Note your function can be simpler as:

Public Function ISLIKE(ByVal sText As String, ByVal sPattern As String) As Boolean 
    ISLIKE = sText Like sPattern
End Function