Friday, February 15, 2013

EndsWith and StartsWith For VBA

Here are listings for 2 functions that are missing in VBA: string starts with and string ends with. The functions are case-insensitive. They use a simple equality check which will probably make them unsuitable for non-ASCII text.


Public Function EndsWith(str As String, ending As String) As Boolean
     Dim endingLen As Integer
     endingLen = Len(ending)
     EndsWith = (Right(Trim(UCase(str)), endingLen) = UCase(ending))
End Function

Public Function StartsWith(str As String, start As String) As Boolean
     Dim startLen As Integer
     startLen = Len(start)
     StartsWith = (Left(Trim(UCase(str)), startLen) = UCase(start))
End Function