Trim all Cells in a Sheet

tonycm1

New Member
Joined
Aug 25, 2007
Messages
47
The data that comes out of our corporate database always adds extra spaces to the cells so I wanted a macro that cleans the sheet up for me.

After searching the interwebs, I've managed to find this:
Code:
Sub test()
Dim Cel As Range, Rng As Range

Set Rng = ActiveSheet.UsedRange

For Each Cel In Rng '' Causes Run-Time Error
    If Len(Cel) > 0 Then Cel = Trim(Cel)
Next
End Sub

This works great although it always gives me a Run-time error '1004': Application-defined or object-defined error whenever one of the cells in the worksheet originally contained data like " =blabla" (i.e. a bunch of leading spaces with an equal sign).

I assume that the error is because after the Trim, excel is trying to do the sum (or other function) and this is causing the error.

Is there a way I can still trim my worksheet even with these occasional cells occuring?
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Try this:

Code:
Sub TC()
    With ActiveSheet.UsedRange
        .NumberFormat = "@"
        TrimRange .Cells
    End With
End Sub
 
Sub TrimRange(r As Range)
    Dim rArea       As Range
    Dim sAdr        As String
    For Each rArea In r.Areas
        With rArea
            sAdr = .Address(ReferenceStyle:=Application.ReferenceStyle, _
                            External:=True)
            .Value = Evaluate("if(row(), substitute(" & sAdr & ", char(160), "" ""))")
            .Value = Evaluate("if(row(), trim( " & sAdr & "))")
        End With
    Next rArea
End Sub
 
Upvote 0
Woohooo! That code worked great for cells that contain text!

Unfortunately, my worksheet has both Numerical cells and Text cells and this macro ended up turning all the Numerical cells into Text.

Is there a way I can still keep my numerical cells as numerical?
 
Upvote 0
That would require some different code. I'll posrt something later if noone else does in the meanwhile.

If you don't need the = signs, change the first sub to

Code:
Sub TC()
    With ActiveSheet.UsedRange
        .Replace "=", ""
        TrimRange .Cells
    End With
End Sub

... and start again from scratch.
 
Upvote 0
[SOLVED]: Trim all Cells in a Sheet

Not sure if this is the proper way to flag a thread "solved" or not on here...
 
Upvote 0
There isn't, just wanted to confirm that everything's laying flat.
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,233
Members
452,898
Latest member
Capolavoro009

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