View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.misc
Do what I can, but I'm only one man. Do what I can, but I'm only one man. is offline
external usenet poster
 
Posts: 1
Default Finding (and eliminating) hidden "space" character

Here is a macro I use to replace/remove:
1) many many spaces = one
2) Probably your culprit he the Unicode "character 160" = a normal space.
3) a space & period = just a period .<=corrects this anomoly.
4) and some other annoyances...see below.

Sub TrimNSlenderizeNCleanAll()
Application.ScreenUpdating = False
Application.EnableEvents = False
On Error Resume Next
Dim cell As Range
For Each cell In Selection.Cells
If cell.HasFormula = False Then
cell.Replace What:=" ,", Replacement:=", ", _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
cell.Replace What:=Chr(160), Replacement:=Chr(32), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
cell.Replace What:=" .", Replacement:=". ", _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
cell = Trim(cell)
cell.Value = Application.Clean(cell.Value)
cell.Replace What:=" ", Replacement:=" ", _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
cell.Replace What:=" ", Replacement:=" ", _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
cell.Replace What:=" ", Replacement:=" ", _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
cell.Replace What:=" ", Replacement:=" ", _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
cell.Replace What:=" ", Replacement:=" ", _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
cell.Replace What:=" ", Replacement:=" ", _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
End If
Next cell
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub

It has taken me maybe a few days to figure that one out manually (i.e.
learning Visual Basic through examples); enjoy!

"smartgal" wrote:

I downloaded some numeric data but I have discovered that there is a leading
"space" character. I tried a find and replace and a host of other tricks but
short of going into each cell and deleting I'm at a loss for how to bulk find
and replace this. It's preventing me from sorting, etc. I tried to outfox
it by doing a formula and adding the value to "0" but I got an error and when
I asked it to evaluate it gave me the following:( =" 1069.95"+0) and that's
how I discovered the leading space.

Thanks -

mm