Fix Excel VBA RTrim Not Removing Trailing Spaces
Question details
Remove trailing spaces from imported worksheet text with an Excel VBA macro.

- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- A macro processes a range containing text imported from a file.
- Observed behavior
- The macro may finish without removing trailing spaces when the cell value is not passed to RTrim, or when the text contains non-breaking spaces.
Identify the range of imported text cells and confirm that the macro assigns it to the Range variable r1. Apply these examples to text values rather than formula cells, because writing the result back replaces a cell's contents.
Pass Each Cell Value to VBA RTrim
Use this method when the trailing characters are ordinary spaces and the macro is not passing the cell value to RTrim.
In Excel, open the Visual Basic Editor from Developer > Visual Basic, then open the module containing the macro.
Inside the existing Sub, declare cell as a Range and use the already-assigned r1 range: Dim cell As Range If r1 Is Nothing Then Exit Sub For Each cell In r1 cell.Value = RTrim(cell.Value) Next cell
Run the Sub from the Visual Basic Editor and inspect the text cells in r1 to confirm that their trailing ordinary spaces are gone.

Replace Non-Breaking Spaces Before Applying RTrim
Use this method when imported text contains non-breaking spaces that RTrim alone does not remove.
Remove Trailing Spaces with WPS Spreadsheets
Use a worksheet formula in WPS Spreadsheets to replace non-breaking spaces and trim imported text without a VBA macro.
- 1. Open the workbook: Open the imported workbook in WPS Spreadsheets and locate the column containing the text.
- 2. Enter a cleaning formula: In an empty cell beside the first text value, enter =TRIM(SUBSTITUTE(A1,CHAR(160)," ")), replacing A1 with the source cell.
- 3. Fill the formula down: Drag the formula cell's fill handle down alongside the remaining imported values.
- 4. Replace the source values if needed: Copy the cleaned cells, then use Paste Special > Values to paste them over the original text. TRIM also removes leading spaces and reduces repeated internal ordinary spaces to one.

Frequently Asked Questions
Why does RTrim leave spaces in my Excel cells?
Check that the macro passes the value to the function and writes it back, such as cell.Value = RTrim(cell.Value). RTrim also leaves non-breaking spaces unchanged.
How do I remove trailing non-breaking spaces in VBA?
Convert them to ordinary spaces before trimming: cell.Value = RTrim(Replace(cell.Value, ChrW(160), " ")). This also converts non-breaking spaces within the text.
Does RTrim remove leading spaces?
No. VBA RTrim removes ordinary spaces from the end of a string. Use LTrim for leading ordinary spaces or Trim for ordinary spaces at both ends.
Can I run the cell-value loop on formula cells?
Writing cell.Value back to a formula cell replaces its formula with the result. Restrict the range to imported text values if you need to preserve formulas.




