Macro to remove leading space

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
 
G

Graham Yetton

Andrew
Why use a macro, when a formula will do?
=if(code(A1)=32,right(A1,len(A1)-1),A1)
HTH
Graham Yetton
 
T

Tom Ogilvy

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
 
A

Alex

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
 
A

Andrew

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

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

Top