Macro to remove space at front and end of a cell

  • Thread starter Thread starter yhoy
  • Start date Start date
Y

yhoy

Similar to the TRIM() function but I would like to find a more automated way..

Thanks!
 
There are two TRIM functions. In the worksheet TRIM removes leading and
trailing spaces. It also removes "extra" internal spaces.

In VBA, TRIM leaves the "internal" spaces alone. If you want to do the
automatic worksheet-style TRIM, then select the cells and run:

Sub dural()
For Each r In Selection
a = r.Address
r.Value = Evaluate("Trim(" & a & ")")
Next
End Sub

This will convert the values in-place
 
If it is possible you may have any formulas in the selected range you mat want
to protect them from being wiped out.

Sub dural_hematoma()
For Each R In Selection
If Not R.HasFormula Then
a = R.Address
R.Value = Evaluate("Trim(" & a & ")")
End If
Next
End Sub


Gord Dibben MS Excel MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top