Macro to remove leading space

  • Thread starter Thread starter Andrew
  • Start date Start date
A

Andrew

I am just now getting into writing macros so I think this task may be
above me for now. Could some kind person give me a hint on writing a
macro that would check for, and if it exists, remove a single space
that is the first character in a cell from a selected set of cells.

Many thanks in advance for your time
Andrew
 
Andrew
Why use a macro, when a formula will do?
=if(code(A1)=32,right(A1,len(A1)-1),A1)
HTH
Graham Yetton
 
Sub RemoveSpace()
Dim cell As Range

For Each cell In Selection
If Not cell.HasFormula Then
If Not IsEmpty(cell) Then
If Left(cell.Value, 1) = " " Then
cell.Value = Right(cell.Value, _
Len(cell.Value) - 1)
End If
End If
End If
Next
End Sub
 
Of course there is also the LTrim(), RTrim(), and Trim()
functions that will wipe out all your leading or trailing
spaces, or both. Very handy when dealing with user forms.

Alex
 
Thank you so much. It works great. This is going to be a REAL
motivator to start learning!
 
Back
Top