removing leading and trailing spaces

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have excel worksheet with some data in the cells. But in each cell the data
has leading and traliling spaces. I don't have any hidden characters. I save
this excel file as CSV file but CSV file is preserving these spaces. Does any
one can help me about the VBA macro how to remove the leading and trailing
spaces from all cells when I create the csv file from the excel file. The
excel should not be modified in anyway.
 
Note there's also a worksheet function TRIM() that has the same
functionality as the VBA one.



Public Sub leadingSpaces()
Dim rng As Excel.Range
Dim wsh As Excel.Range

Application.ScreenUpdating = False

Set wsh = Application.ActiveSheet.UsedRange

For Each rng In wsh
rng.Value = Trim(rng.Value)
Next rng

Application.ScreenUpdating = True
End Sub
 
Back
Top