ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Macro to remove leading space (https://www.excelbanter.com/excel-programming/275595-macro-remove-leading-space.html)

Andrew[_19_]

Macro to remove leading space
 
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

Graham Yetton

Macro to remove leading space
 
Andrew
Why use a macro, when a formula will do?
=if(code(A1)=32,right(A1,len(A1)-1),A1)
HTH
Graham Yetton
-----Original Message-----
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
.


Tom Ogilvy

Macro to remove leading space
 
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


--
Regards,
Tom Ogilvy


Andrew wrote in message
om...
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




Alex[_6_]

Macro to remove leading space
 
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

-----Original Message-----
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[_19_]

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


"Tom Ogilvy" wrote in message ...
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


--
Regards,
Tom Ogilvy


Andrew wrote in message
om...
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



All times are GMT +1. The time now is 06:59 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com