Speed Up Trim Macro

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,786
Office Version
  1. 365
Platform
  1. Windows
I was given the code below to run a Trim Macro. I have 10000 rows and 30 columns of data for it check and it takes forever and sometimes freezes. Is there anyway it can be speeded up please?

Code:
Sub trimCells()
Application.ScreenUpdating = False
  For Each c In ActiveSheet.UsedRange
    c.Value = WorksheetFunction.Trim(c.Value)
  Next c
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
I struggle with the logic as to why this works too, so perhaps someone else who knows can add to this topic. Best way for me to explain is to study a small sample:

Enter some random text in A1:A3

<b>Excel 2010</b><table cellpadding="2.5px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #BBB"><colgroup><col width="25px" style="background-color: #DAE7F5" /><col /></colgroup><thead><tr style=" background-color: #DAE7F5;text-align: center;color: #161120"><th></th><th>A</th></tr></thead><tbody><tr ><td style="color: #161120;text-align: center;">1</td><td style=";">   salsag  dshklsau   khsa sdkl   hsadhk; </td></tr><tr ><td style="color: #161120;text-align: center;">2</td><td style=";">   kljsd sh hsd;kh sdkhsa ;ksahs da; ;   sad I;hhids h;    sdih</td></tr><tr ><td style="color: #161120;text-align: center;">3</td><td style=";">  dhids I;h sd ;    sdihi sdsdh     hkds usdui </td></tr></tbody></table><p style="width:3.6em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #BBB;border-top:none;text-align: center;background-color: #DAE7F5;color: #161120">Sheet2</p>

Now grab all of A5:A7 and type:
=TRIM($A$1:$A$3)
And commit with CONTROL+ENTER

The formula result will be #VALUE!. This is because we have fed TRIM an array of values and it is intended to trim only a single text element.

The use of the IF(ROW() when used with the evaluate is a tip that appears to allow us to process and entire range in one hit, yet the function will not only handle each individual string element and not yield an error.

In worksheet level we can achieve the same with an array formula, to return an array of values:

Grab all of A5:A7 and type:
=TRIM(A1:A3)
And commit with CONTROL+SHIFT+ENTER

But the evaluate method doesn't work the same way as array entered array formulae...
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,253
Members
452,900
Latest member
LisaGo

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