Hidden characters

D

Diana

I am using this trimall macro for my data, the problem is
that it works fine for all the data except for cells that
have a value of 0032569. It is deleting the values 00
which is not good because if have several match formulas
in place. Is there any way to trimall and still keep the
00?


Sub TrimALL()
Application.ScreenUpdating = False
Cells.Select
'David McRitchie 2000-07-03 mod 2000-08-16 join.htm
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim cell As Range
'Also Treat CHR 0160, as a space (CHR 032)
Selection.Replace What:=Chr(160), Replacement:=Chr(32),
_
LookAt:=xlPart, SearchOrder:=xlByRows,
MatchCase:=False
'Trim in Excel removes extra internal spaces, VBA does
not
On Error Resume Next 'in case no text cells in
selection
For Each cell In Intersect(Selection, _
Selection.SpecialCells(xlConstants, xlTextValues))
cell.Value = Application.Trim(cell.Value)
Next cell
On Error GoTo 0
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
 
D

Dave Peterson

When you do that mass change (chr(160) to chr(32)--the non-breaking space to a
regular space), excel sees it as you typing in a numeric entry. And it forgives
the extra leading or trailing space.

This might be better for you:

Option Explicit

Sub TrimALL2()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim cell As Range

On Error Resume Next 'in case no text cells in Selection
For Each cell In Intersect(Selection, _
Selection.SpecialCells(xlConstants, xlTextValues))
cell.Value = Application.subsitute(cell.Value, Chr(160), Chr(32))
cell.Value = Application.Trim(cell.Value)
Next cell
On Error GoTo 0
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
 

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