How can I make this faster?

pholt33

Board Regular
Joined
Jan 4, 2005
Messages
206
Office Version
  1. 365
Platform
  1. Windows
I often receive data files that need some cleanup so I have created a macro to trim and remove carriage returns. It works well and is pretty quick (few seconds) when the range is, say, 1500 cells but its much slower (60+ secs) if there are 5-10k+. How can I speed this up? Or does someone have a different macro that accomplishes the same/similar thing?

Sub DeleteLeadingTrailingSpaces()

Application.ScreenUpdating = False

Set W = Application.Selection
Set W = Application.InputBox("Select one range that you want to remove leading and trailing spaces:", "RemoveLeadingAndTrailingSpaces", W.Address, Type:=8)
For Each R In W
R.Value = VBA.Trim(R.Value)
R.Value = Application.WorksheetFunction.Clean(R.Value)
Next

End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Try setting calculation to manual if you have formulas. Also disable events if you have and (worksheet change, etc).
 
Upvote 0
No formulas or anything at the time when I am doing the cleanup. Any other options?
 
Upvote 0
Looping through each cell in a range is VERY slow.

Instead, load the range to an array, loop through the array & load (either to the same array or a new array) the modified values, then write the modified array back to the range.

Chip Pearson's website has a write-up with the basics here: Arrays And Ranges In VBA
 
Upvote 0
Just for fun...

VBA Code:
Sub DeleteLeadingTrailingSpaces()

  Application.ScreenUpdating = False

  If TypeName(Selection) = "Range" Then
    Dim sSelection As String
    sSelection = Selection.Address
  End If
  On Error Resume Next
  Dim rSelection As Range
  Set rSelection = Application.InputBox("Select one range that you want to remove leading and trailing spaces:", "RemoveLeadingAndTrailingSpaces", sSelection, Type:=8)
  If rSelection Is Nothing Then GoTo ExitSub
  On Error GoTo 0
 
  If rSelection.Cells.Count > 1 Then
    Dim vSelection As Variant
    vSelection = rSelection.Value
  Else
    ReDim vSelection(1 To 1, 1 To 1)
    vSelection(1, 1) = rSelection.Value
  End If
 
  Dim iRow As Long, iCol As Long
  For iRow = LBound(vSelection, 1) To UBound(vSelection, 1)
    For iCol = LBound(vSelection, 2) To UBound(vSelection, 2)
      vSelection(iRow, iCol) = WorksheetFunction.Clean(Trim(vSelection(iRow, iCol)))
    Next
  Next
 
  rSelection.Value = vSelection

ExitSub:
  Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution
On a file I needed to cleanup, my code took 57 seconds and yours was done in a split second. ?
Jon, what makes your solution so much faster?
 
Upvote 0
It takes time for VBA to access the worksheet, reading values and especially writing values.

Your code worked cell by cell, reading each value from the worksheet, changing the value, then writing the value back into the worksheet. Many read and write operations, so it's very slow.

My code reads the entire range of values in one step, changes the values one by one, then writes the entire range of values back into the worksheet in one step. One read and one write operation, so it's very fast.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,022
Latest member
RobertV1609

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