VBA & Macro Problems

Fix Excel VBA RTrim Not Removing Trailing Spaces

Huda QurayshiHuda Qurayshi Nov 25, 2026 868 views

Question details

Remove trailing spaces from imported worksheet text with an Excel VBA macro.

Fix Excel VBA RTrim Not Removing Trailing Spaces
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the macro

In Excel, open the Visual Basic Editor from Developer > Visual Basic, then open the module containing the macro.

2
Update the loop

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

3
Run and check

Run the Sub from the Visual Basic Editor and inspect the text cells in r1 to confirm that their trailing ordinary spaces are gone.

Pass Each Cell Value to VBA RTrim
Other whitespace may remain: RTrim removes trailing ordinary spaces, but it does not remove non-breaking spaces. Use the second solution if those remain in the imported text.
Clean imported text in WPS Office

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. 1. Open the workbook: Open the imported workbook in WPS Spreadsheets and locate the column containing the text.
  2. 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. 3. Fill the formula down: Drag the formula cell's fill handle down alongside the remaining imported values.
  4. 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.
Clean trailing ordinary and non-breaking spaces from imported textUse a formula without editing a macroOpen and edit Microsoft Excel XLSX filesWork with a familiar spreadsheet interface
microsoft office alternative - wps office

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.