LTRIM Problem

G

Guest

I am trying to trim the leading spaces from all text in Column U.
I have have the following code but when I run it nothing happens (the
leading spaces are still there). I've tried both LTRIM and TRIM but neither
seems to work.
What am I missing?

Dim Rng as Range
Sheets("Raw Projections").Select
For Each Rng In ActiveSheet.Range("U6:U500")
Rng.Value = LTrim(Rng.Value)
Next Rng

Matt
 
D

David McRitchie

Hi Matt,
Try including this before the loop

Selection.Replace What:=Chr(160), Replacement:=Chr(32), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False

For more information see:
http://www.mvps.org/dmcritchie/excel/join.htm#trimall

Char(160) is the HTML   (non-breaking space)

But if you look at the macro TRIMALL macro you will see some additional
code which will make the macro run faster.
 
G

Guest

Thanks guys I modified to code to the following and it worked great.

Sheets("Raw Projections").Range("U6:U500").Select
Selection.Replace What:=Chr(160), Replacement:=Chr(32), LookAt:=xlPart, _
SearchOrder:=xlByColumns, MatchCase:=False
For Each Rng In ActiveSheet.Range("U6:U500")
Rng.Value = LTrim(Rng.Value)
Next Rng
 

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