Trim Function As Macro

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,786
Office Version
  1. 365
Platform
  1. Windows
Is it possible to select an entire worksheet and run the trim function as a macro? I have about 10,000 rows and 20 columns of data so doing each column individually and copy/pasting etc will be very time consuming. I have do it on several files too.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Have a look at downloading ASAP Utilities. There are a ton of valuable tools within there. I couldn't imagine not having it. But you could create a macro to do this as well as such:

Code:
sub trimCells()
for each c in activesheet.usedrange
c.value = trim(c.value)
next c
end sub
This is untested, but should work, I can't test it at the moment.

Hope that helps.
 
Upvote 0
That doesnt seem to work properly. I am correct in saying it gets rid of extra spaces between words etc? If so, I still have extra spaces in a few cells I know of.
 
Upvote 0
Hello

The VBA version of Trim, works differently from the Excel version, try this.

Code:
Sub trimCells()
  For Each c In ActiveSheet.UsedRange
    c.Value = WorksheetFunction.Trim(c.Value)
  Next c
End Sub
 
Upvote 0
That doesnt seem to work properly. I am correct in saying it gets rid of extra spaces between words etc? If so, I still have extra spaces in a few cells I know of.
As stated by Meldoc the vba version of trim does not get rid of excessive spaces between words. He has provided a solution that should worka dn I would still recommend having a look at ASAP Utilities, which is free.
 
Upvote 0
Thanks Meldocs code did work. So what was the other code going to do?
 
Upvote 0
The VBA trim function only trims spaces off the left and right side of the string. It does not remove extra spaces between words, you need the worksheet function TRIM for this.
 
Upvote 0

Forum statistics

Threads
1,224,605
Messages
6,179,860
Members
452,948
Latest member
UsmanAli786

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