Trimming Spaces

awappel

New Member
Joined
Mar 15, 2011
Messages
14
I am having trouble trimming spaces in excel. I have searched the forum at attempted several macro's including the one below:

Sub TrimSpaces()
Dim cell As Range
For Each cell In ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants)
cell = WorksheetFunction.Trim(cell)
Next cell
End Sub

But I still have the extra spaces. I have also just tested a few cells by using the trim () function, and I still get extra spaces after the words. I have done the trim function in the past and not had a problem. Is there something potentially wrong with my excel? or do I have my field in an incorrect format?

Thanks for any help you can give, I am a fairly inexperienced user.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Welcome to MrExcel.

Maybe you have some Chr(160) spaces. Try:

Code:
Sub TrimSpaces()
    Dim cell As Range
    For Each cell In ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants)
        cell = WorksheetFunction.Trim(Replace(cell.Value, Chr(160), ""))
    Next cell
End Sub
 
Upvote 0
Thank you for all your help. That last function worked perfectly. I will make sure to hold onto that!!!!!
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,682
Members
452,937
Latest member
Bhg1984

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top