removing leading and trailing spaces


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.
 
Ad

Advertisements

I

ilia

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
 

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