Delete last 3 characters if they are equal to something

Viewed 137

(without affecting the char level formatting in the cell)

I've already posted a similar question, but I unfortunately I realized I couldn't use any of the solutions because all of them removed all the formatting in the cells :(

I have a code that replaces various text strings. It looks like this (+ some more IFs):

Sub Fix()
  Dim X As Long, Cell As Range
  For Each Cell In Selection
    For X = Len(Cell.Text) To 1 Step -1
      If Cell.Characters(X, 3).Text = ", ," Then Cell.Characters(X, 3).Text = ","
      If Cell.Characters(X, 3).Text = ", (" Then Cell.Characters(X, 3).Text = " ("
      If Cell.Characters(X, 3).Text = ", [" Then Cell.Characters(X, 3).Text = " ["
      If Cell.Characters(X, 3).Text = ", -" Then Cell.Characters(X, 3).Text = " -"
      If Cell.Characters(1, 3).Text = "abc" Then Cell.Characters(1, 3).Text = ""
    Next
  Next
End Sub

The last line of my code deletes abc if this is contained at the beginning of the selected cell(s).

I am looking to find out how to delete abc when this is contained at the end of the selected cell(s).

But it's really important for me to preserve all the original formatting in the cells (text size, colors, bold/italic/underline letters etc).

Please bear in mind that I am a complete beginner and I don't know anything about programming. I've copied the code above from the internet and just changed the values.

Thank you in advance for your help.

5 Answers

Something like this would probably work:

Sub FixData()
Dim X As Long, Y As Long, Cell As Range, FindChars As Variant, ReplaceChars As Variant, StartRemove As Variant, EndRemove As Variant
FindChars = Array(", ,", ", (", ", [", ", -") 'must have at least 2 elements, if you only have one then just put something it will never have like "zzzzzzzzzz"
ReplaceChars = Array(",", " (", " [", " -") 'must have at least 2 elements, if you only have one then just put something it will never have like "zzzzzzzzzz"
StartRemove = Array("something1", "somthingelse1") 'must have at least 2 elements, if you only have one then just put something it will never have like "zzzzzzzzzz"
EndRemove = Array("something2", "somthingelse2") 'must have at least 2 elements, if you only have one then just put something it will never have like "zzzzzzzzzz"
For Each Cell In Selection
    For Y = LBound(FindChars) To UBound(FindChars)
        If InStr(1, Cell.Text, FindChars(Y)) > 0 Then Cell.Characters(InStr(1, Cell.Text, FindChars(Y)), Len(FindChars(Y))).Delete
    Next
    For Y = LBound(StartRemove) To UBound(StartRemove)
        If Left(Cell.Text, Len(StartRemove(Y))) = StartRemove Then Cell.Characters(1, Len(StartRemove(Y))).Delete
    Next
    For Y = LBound(EndRemove) To UBound(EndRemove)
        If Right(Cell.Text, Len(EndRemove(Y))) = EndRemove Then Cell.Characters(Len(Cell) - Len(EndRemove(Y)) + 1, Len(EndRemove(Y))).Delete
    Next
Next
End Sub

Edit based on conversations since and updates to reflect these conversations.

This code has 4 arrays, it rips through the find and replace arrays first to find and replace any instance. Then it goes through the start array to find only things at the start then lastly it loops the end array to find anything at the end of the string.

Using cell.characters().delete will remove those chars without altering the individual character formatting around it.

Replace Characters (Preserve Formatting)

Option Explicit

Sub FixIt()

    Const NoC As Long = 3
    Const Special As String = "abc"
    Dim Source As Variant: Source = Array(", ,", ", (", ", [", ", -")
    Dim Target As Variant: Target = Array(",", " (", " [", " -")

    Dim Cell As Range
    Dim UB As Long: UB = UBound(Source)
    Dim i As Long, x As Long
    Dim Current As String

    For Each Cell In Selection
        Current = Cell.Characters(Len(Cell.Text) - NoC + 1, NoC).Text
        If Current = Special Then _
           Cell.Characters(Len(Cell.Text) - NoC + 1, NoC).Delete
        For x = Len(Cell.Text) - NoC + 1 To 1 Step -1
            GoSub replaceCurrent
        Next
    Next

    Exit Sub

replaceCurrent:
    Current = Cell.Characters(x, NoC).Text
    For i = 0 To UB
        If Current = Source(i) Then Current = Target(i): Exit For
    Next
    If i <= UB Then Cell.Characters(x, NoC).Text = Current
    Return

End Sub

If you want to remove the last three characters of a cell if they are abc, then:

Before:

enter image description here

the code:

Sub EasyAsABC()
    Dim s As String, L As Long
    With ActiveCell
        s = .Text
        L = Len(s)
        If Right(s, 3) = "abc" Then
            .Characters(L, 1).Text = ""
            .Characters(L - 1, 1).Text = ""
            .Characters(L - 2, 1).Text = ""
        End If
    End With
End Sub

and after:

enter image description here

Since all your strings are length of 3, you simply need to adjust your loop:

For X = Len(Cell.Text) - 2 To 1 Step -1

Just add a line like:

If Cell.Characters(X, 3).Text = "abc" Then Cell.Characters(X, 3).Text = ""

That would make your last line loking for abc redundant and can be removed. However, it seems you could also use some other construct alltogether if you like too, possibly:

Sub Fixt()

Dim X As Long, Cell As Range
Dim arr1 As Variant: arr1 = Array("abc", ", ,", ", (", ", [", ", -")
Dim arr2 As Variant: arr2 = Array("", ",", " (", ", [", " -")

For Each Cell In Selection
    For X = Len(Cell.Text) - 2 To 1 Step -1
        Match = Application.Match(Cell.Characters(X, 3).Text, arr1, 0)
        If IsNumeric(Match) Then Cell.Characters(X, 3).Text = arr2(Match - 1)
    Next
Next

End Sub

Try,

Sub test()
    Dim rngDB As Range, rng As Range
    Dim i As Integer, x As Integer
    Dim vWhat As Variant, vReplace As Variant
    Dim s As String

    Set rngDB = Selection
    vWhat = Array("abc", ", ,", ", (", ", [", ", -")
    vReplace = Array("", ",", " (", " [", " -")

    For Each rng In rngDB
        x = Len(rng) - 2
        s = rng
        If x > 0 Then
            For i = 0 To UBound(vWhat)
                If Mid(s, x, 3) = vWhat(i) Then
                    rng.Characters(x, 3).Text = vReplace(i)
                End If
            Next i
        End If
    Next rng

End Sub
Related