Speeding up this once speedy code

Grizlore

Active Member
Joined
Aug 22, 2006
Messages
259
Could anyone suggest how I can speed up this code please?

This used to work really quickly, however on my new quicker 'laptop' it takes ages.

I can see in the bottom Excel bar it displaying Calculating (4 processers: x%). Which I have never seen before!

Anyway.. here is the code


Code:
Sub TrimEVERYTHING()

Dim rng, rngC, rngUnion As Range
 
Set rng = ActiveSheet.UsedRange
Set rngUnion = Nothing
 
For Each rngC In rng
    If Left(rngC.Formula, 1) <> "=" Then
        If rngUnion Is Nothing Then
            Set rngUnion = rngC
        Else
            Set rngUnion = Union(rngUnion, rngC)
        End If
    End If
Next rngC
 
For Each rngC In rngUnion
    rngC.Value = Evaluate("IF(ROW(" & rngC.Address & "),IF(COLUMN(" & rngC.Address & "),TRIM(" & rngC.Address & ")))")
Next rngC
 
MsgBox "TRIMMED"
End Sub

Any help in making this do exactly the same job, but be quicker would be appreciated
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try this

Code:
Sub TrimEVERYTHING()

Dim rng As Range, rngC As Range, rngUnion As Range
With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
End With
Set rng = ActiveSheet.UsedRange
Set rngUnion = Nothing
 
For Each rngC In rng
    If Left(rngC.Formula, 1) <> "=" Then
        If rngUnion Is Nothing Then
            Set rngUnion = rngC
        Else
            Set rngUnion = Union(rngUnion, rngC)
        End If
    End If
Next rngC
 
For Each rngC In rngUnion
    rngC.Value = Evaluate("IF(ROW(" & rngC.Address & "),IF(COLUMN(" & rngC.Address & "),TRIM(" & rngC.Address & ")))")
Next rngC

With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
End With
 
MsgBox "TRIMMED"
End Sub
 
Upvote 0
Thanks, now it is lightening fast

I see the changes, so... .Calculation = xlCalculationManual
was cycling through and slowing it all down?

It is the first time I have seen this.

Thanks again!

Much Appreciated !
 
Upvote 0

Forum statistics

Threads
1,222,749
Messages
6,167,967
Members
452,158
Latest member
MattyM

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