How to trim last two characters from a cell? (Not standard spaces)

datanewb

Board Regular
Joined
May 8, 2009
Messages
76
I found this snippet on the web to trim the leading and trailing spaces:

Code:
Sub TrimRText()
' This module will trim extra spaces from RIGHT SIDE.
    Dim MyCell As Range
    On Error Resume Next
        Selection.Cells.SpecialCells(xlCellTypeConstants, 23).Select
        For Each MyCell In Selection.Cells
            MyCell.Value = Application.Trim(MyCell.Value)
        Next
    On Error GoTo 0
End Sub

However, stepping through it I found it not to work. I think this is because the characters are not standard spaces. (What exactly they are, I don't know:confused:).

The good news is, there is always two of these characters on the right side of the value that needs to be trimmed. Unfortunately, I am unfamiliar with VBA. How would I trim the last two characters from MyCell?

In c++ parlance:
MyCell = MyCell.substr(MyCell.begin(),MyCell.end()-2);

Thank you.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hi

When you read text from the net (html docs) sometimes you get a html non breaking space, character 160.

Try replacing the characters 160 by ascii spaces (character 32) and then you can use Trim().
 
Upvote 0
Try:
Rich (BB code):
Sub HaircutAndSides()
Dim cell As Range
Application.ScreenUpdating = False
For Each cell In Cells.SpecialCells(xlCellTypeConstants, 23)
    If Right(cell, 2) = "AA" Then cell.Value = Left(cell, Len(cell) - 2)
Next cell
Application.ScreenUpdating = True
End Sub
Replace AA with whatever the 2 charachters to the right are that you want removed
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top