White space removal

C

camlad

I need to remove white space from a range so I recorded the following which
did not work.



How can I remove all spaces in a range?



Range("A1:A535").Select

Selection.Replace What:="^w", Replacement:="", LookAt:=xlPart, _

SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _

ReplaceFormat:=False



Camlad
 
G

Gord Dibben

What does "^w" mean to you?

Try this to remove all spaces.

Public Sub Strip_WhiteSpace()
Selection.Replace what:=" ", _
replacement:="", lookat:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
End Sub

If the spaces are html non-breaking spaces.

Public Sub Strip_WhiteSpace()
With Selection
.Replace what:=Chr(160), replacement:="", _
lookat:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
.Replace what:=" ", _
replacement:="", lookat:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
End With
End Sub


Gord Dibben MS Excel MVP
 
P

p45cal

camlad;525281 said:
I need to remove white space from a range so I recorded the followin
which
did not work.



How can I remove all spaces in a range?



Range("A1:A535").Select

Selection.Replace What:="^w", Replacement:="", LookAt:=xlPart, _

SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _

ReplaceFormat:=False



Camlad

See if this works for you:

Sub blah()
For Each cll In Range("A1:A535").Cells
cll.Value = Application.WorksheetFunction.Trim(cll.Value)
Next cll
End Su
 
C

camlad

Thanks Gord and p45cal

I had spotted Trim and not used it - I was trying to use the ^w which
replaces multiple spaces used in Word, inc macros..

Camlad
 
C

camlad

Sorry - back again. Both spaces and non-breaking spaces are removed but
there is something remaining in one cell which remains. I paste it, seven
characters on this occasion, here within parenthasis
( )
Pasting into Word and showing hidden characters reveals what look like
spaces. Are there any other forms of space?

This is a spreadsheet which is sent to me occasionally and I need to clear
everything out of the column except text which will be Jan, Feb, Mar,
.......Nov, Dec or the numneral 2009, 2010, etc. Everything else should go in
order for the rest of my programming to work properly.

How can I find what these other characters are or have I to find some way of
testing every character and deleting everything except those which appear in
Jan, Feb, Mar, ......Nov, Dec and 0-9? There must be a better way than that.

Please help.

Camlad
 
C

camlad

Sorry - back again. Both spaces and non-breaking spaces are removed but
there is something remaining in one cell which remains. I paste it, seven
characters on this occasion, here within parenthasis
( )
Pasting into Word and showing hidden characters reveals what look like
spaces. Are there any other forms of space?

This is a spreadsheet which is sent to me occasionally and I need to clear
everything out of the column except text which will be Jan, Feb, Mar,
.......Nov, Dec or the numneral 2009, 2010, etc. Everything else should go in
order for the rest of my programming to work properly.

How can I find what these other characters are or have I to find some way of
testing every character and deleting everything except those which appear in
Jan, Feb, Mar, ......Nov, Dec and 0-9? There must be a better way than that.

Please help.

Camlad
 
G

Gord Dibben

Go to Chip Pearson's site and download his cellview add-in to find
out what's in the cells.

http://www.cpearson.com/excel/CellView.aspx

An indispensable tool if bringing in data from the Internet

The copied data you just posted contains 7 char 32's which are plain old
spaces. Whatever you thought you posted did survive the trip.


Gord Dibben MS Excel MVP
 
G

Gord Dibben

Make that "did NOT survive the trip"


Gord

Go to Chip Pearson's site and download his cellview add-in to find
out what's in the cells.

http://www.cpearson.com/excel/CellView.aspx

An indispensable tool if bringing in data from the Internet

The copied data you just posted contains 7 char 32's which are plain old
spaces. Whatever you thought you posted did survive the trip.


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

Top