Is there a Faster Way (trim, clean VBA)

Alkemist

Board Regular
Joined
Nov 26, 2003
Messages
144
Hia...

I've written some code that selects all the text cells in a spreadsheet and cleans up the contents with Trim, Clean:

Range("A1", Range("A1").SpecialCells(xlLastCell)).SpecialCells(xlCellTypeConstants, 2).Select

For Each rng In Selection
rng.Value = Application.WorksheetFunction.Clean(Trim(rng.Value))
Next rng

Is there a faster way of doing this becaise some worksheets have a lot of data?

Cheers...A
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
first of all, don't select a range to work with it.
Second, disable screen updating

You might find performance is much improved.

Code:
application.screenupdating=false
For Each rng In Range("A1", Range("A1").SpecialCells(xlLastCell)).SpecialCells(xlCellTypeConstants, 2)
rng.Value = Application.WorksheetFunction.Clean(Trim(rng.Value))
Next rng
 
Upvote 0
Excellent..thanks for that, it was exactly what I was looking for.

I knew I didn't need to select the range, but, I couldn't work out how to code the VBA. I usually start by recording a macro and re-writting, recording always goes the route of selecting cells.

That was a small chunk of code from a larger script that does turn off screen updating, autocalc and so on, both of which are of course real time savers.

Thanks again

Cheers..A
 
Upvote 0
procedure works well, but don't remove 'non breaking space' (NBSP) useing in Internet (Chr(160)), request of a little supplement as follows:
'--
Sub RemoveAllNonPrintableCharactersInSelection_v1()
Dim rng As Range
Dim c As Range
Dim lngMemoCalculation As Long
Const csBLANK As String = " "
'--
'For complete worksheet
'Set rng = Range("A1", Range("A1").SpecialCells(xlLastCell)).SpecialCells(xlCellTypeConstants, 2)
'... or for selected range only
Set rng = Selection.SpecialCells(xlCellTypeConstants, 2)
'--
Application.ScreenUpdating = False
lngMemoCalculation = Application.Calculation
Application.Calculation = xlCalculationManual
For Each c In rng.Cells
c.Value = Application.WorksheetFunction.Clean( _
Trim(Replace(c.Value, Chr(160), csBLANK)))
Next c
Application.Calculation = lngMemoCalculation ' Restore original Calculation mode
End Sub
'--
Best Regards
andrija vrcan
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,882
Members
452,948
Latest member
Dupuhini

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