Results 1 to 7 of 7

Thread: Trim all Cells in a Worksheet - VBA

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,122
    Rep Power
    10

    Lightbulb Trim all Cells in a Worksheet - VBA

    Hi All,

    Here is a VBA routine to Trim as well as Clean all used range in a worksheet.

    Code:
    '// Developed by Kris @ ExcelFox.com
    Sub TRIMALL(ByRef Sht As Worksheet)
        
        Dim aAddr       As String
        Dim MaxLen      As Long
        Dim v           As Variant
        Dim r           As Long
        Dim c           As Long
        Dim Rng         As Range
        Dim a           As Long
        
        On Error Resume Next
        Set Rng = Sht.UsedRange.SpecialCells(2, 6)
        On Error GoTo 0
        
        If Rng Is Nothing Then Exit Sub
        
        For a = 1 To Rng.Areas.Count
            With Rng.Areas(a)
                aAddr = "'" & Sht.Name & "'!" & .Address
                MaxLen = Evaluate("max(len(" & aAddr & "))")
                If MaxLen > 255 Then v = .Value
                .Value = Evaluate("if(len(" & aAddr & "),clean(trim(" & aAddr & "))," & aAddr & ")")
                If MaxLen > 255 Then
                    If IsArray(v) Then
                        For r = 1 To UBound(v, 1)
                            For c = 1 To UBound(v, 2)
                                If Len(v(r, c)) > 255 Then .Cells(r, c).Value = Application.Clean(Application.Trim(v(r, c)))
                            Next
                        Next
                    Else
                        .Cells(1).Value = Application.Clean(Application.Trim(v))
                    End If
                End If
            End With
        Next
        
    End Sub
    and call the routine like..


    Code:
    Sub Test()
    
        TRIMALL ThisWorkbook.Worksheets("Sheet1")
    
    End Sub
    Enjoy !!
    Last edited by Admin; 08-21-2015 at 08:22 AM. Reason: code edited
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

Similar Threads

  1. Replies: 13
    Last Post: 06-10-2013, 09:05 AM
  2. Print Nth Worksheet To Mth Worksheet using VBA
    By Ryan_Bernal in forum Excel Help
    Replies: 2
    Last Post: 02-28-2013, 06:57 PM
  3. Search a last digit e.g 0 and trim to give rest
    By excel_learner in forum Excel Help
    Replies: 4
    Last Post: 01-22-2013, 07:02 PM
  4. Trim Text after 3rd Underscore but retain format
    By trankim in forum Excel Help
    Replies: 4
    Last Post: 05-13-2012, 10:44 AM
  5. Sort Worksheet by Color VBA
    By Admin in forum Excel and VBA Tips and Tricks
    Replies: 0
    Last Post: 10-25-2011, 02:25 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •