• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Efficient way to trim data

ThrottleWorks

Excel Ninja
Hi,

I am using below code to trim a range. However this code is taking time.
Can anyone please suggest me a better code to trim data.

Thanks.

Code:
Sub Trim_Raw_Data()
    Dim RawSht As Worksheet
    Dim TempLr As Long
    Dim TempRng As Range
    Dim TRng As Range
    Dim LastCol As Long
   
    LastCol = 17 'Q
    Set RawSht = ActiveSheet
    TempLr = RawSht.Cells(RawSht.Rows.Count, 1).End(xlUp).Row
    Set TempRng = RawSht.Range(RawSht.Cells(1, 1), RawSht.Cells(TempLr, LastCol))
   
    For Each TRng In TempRng
        TRng = Trim(TRng)
    Next TRng
End Sub
 
You can try following as well.

In place of
Code:
For Each TRng In TempRng
    TRng = Trim(TRng)
Next TRng

you can use:
Code:
TempRng.Value = Application.Trim(TempRng.Value)

Note: there's a limitation of string length(256) when Application.Trim is used.
 
Hi @vletm sir, thanks for the help. I am using screen updating.
However was not using manual calculation mode. Will try going forward.

Have a nice day ahead. :)
 
Back
Top