Splitting


Removing the last element in a delimited string

The following three functions all do the same thing which is remove the last element from a delimited string.


Public Function RemoveLastElement(ByVal sConCat As String, _ 
                         ByVal sSeparatorChar As String) _
                         As String
Dim sreverse As String
Dim ifind As Integer
   RemoveLastElement = CVErr(xlErrNA)
   sreverse = StrReverse(sConCat)
   ifind=Instr(1,sreverse,sSeparatorChar)
   If (ifind > 0) Then
      RemoveLastElement = StrReverse(Left(sreverse,ifind - 1))
   End If
End Function


Public Function RemoveLastElement(ByVal sConCat As String, _ 
                         ByVal sSeparatorChar As String) _
                         As String
Dim arArray As Variant
   arArray = Split(sConCat, sSeparatorChar)
   RemoveLastElement = arArray(UBound(arArray))
End Function


Public Function RemoveLastElement(ByVal sConCat As String, _ 
                         ByVal sSeparatorChar As String) _
                         As String
   RemoveLastElement = Mid(sConCat, InstrReverse(sConCat, sSeparatorChar) + 1)
End Function



© 2017 Better Solutions Limited. All Rights Reserved. © 2017 Better Solutions Limited

PrevNext