CutRightString

Cuts a string from a string FROM RIGHT starting from specific character and until a specific character
FromChar is the char to cut from (not included)
ToChar is the char to stop cut to (after FromChar is found) (not included)
CurFrom is the string to cut from
If FromChar is not given or not found, it will be assumed as 1
If ToChar is not given or not found, it will be assumed as the length of the string
Similar to CutString, but search is done from right to left

Public
Tested
My Own Work
Function CutRightString(CutFrom, Optional FromChar = "", Optional ToChar = "")
 Dim Start1, End1
 CutRightString = ""
 Start1 = Len(CutFrom)
 If FromChar = "" Then GoTo Step2
 For i = Len(CutFrom) To 1 Step -1
  If UCase(Mid(CutFrom, i, 1)) = UCase(FromChar) Then
   Start1 = i
   Exit For
  End If
 Next i
Step2:
 If ToChar = "" Then
  CutRightString = Left(CutFrom, Start1 - 1)
 Else
  End1 = Start1 - 1
  For i = End1 To 1 Step -1
   If UCase(Mid(CutFrom, i, 1)) = UCase(ToChar) Then
    End1 = i
    Exit For
   End If
  Next i
  If End1 = Start1 - 1 Then End1 = 1
  If FromChar = "" And Start1 = Len(CutFrom) Then Start1 = Start1 + 1
  CutRightString = Mid(CutFrom, End1 + 1, Start1 - End1 - 1)
 End If
End Function

CutFrom, Optional FromChar = "", Optional ToChar = ""

CutRightString("Hello-World-Cruel", "-") = "Cruel"
CutRightString("Hello-World-Cruel", "-","-") = "World"

Views 3013 Downloads 928

VBA Texts + Strings
ANmarAmdeen
629
Revisions

v2.0