VBA code to remove leading and trailing spaces from cells in table now failing

BlondieC

New Member
Joined
Feb 9, 2016
Messages
41
Hi, this code was working and now is failing and I'm not sure why. It was set to trim all leading and training spaces from all cells in MasterTbl. The line it fails on is in red in the below code. The following error message is returned: Run-time error '13': Type mismatch.

Code:
Sub RemoveAllSpaces()     'Macro Purpose: To trim all excess spaces out of cells.  This
     'eliminates issues where users have cleared the cell with a space,
     'and elimates all extra spaces at the beginning or end of a string
     
    Application.ScreenUpdating = False
     
    Dim cl As Variant
     
     'Loop through cells removing excess spaces
      For Each cl In ActiveSheet.Range("MasterTbl")
[COLOR=#ff0000][B]        If Len(cl) > Len(WorksheetFunction.Trim(cl)) Then[/B][/COLOR]
            cl.Value = WorksheetFunction.Trim(cl)
        End If
    Next cl
        
    Application.ScreenUpdating = True
    
End Sub


MasterTbl data from columns A:K with D as a hidden column:
This data at this point needs a lot of cleanup done but as step one I want leading and trailing spaces removed.

[TABLE="width: 1296"]
<tbody>[TR]
[TD]Old Box#[/TD]
[TD]Civic#[/TD]
[TD]Unit[/TD]
[TD]Street Name[/TD]
[TD]Street Type[/TD]
[TD]Direction[/TD]
[TD]Last Name[/TD]
[TD]First Name[/TD]
[TD]Convenience Box[/TD]
[TD]Comments[/TD]
[/TR]
[TR]
[TD="align: right"]1660[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD="colspan: 2"]CENTRAL LAMBTON FAMILY HEALTH TEAM[/TD]
[TD="align: right"]269[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]1247[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]OPTIMIST CLUB[/TD]
[TD][/TD]
[TD="align: right"]156[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]2001[/TD]
[TD]MOVED[/TD]
[TD][/TD]
[TD]CHECK[/TD]
[TD][/TD]
[TD][/TD]
[TD]ZAVITZ[/TD]
[TD]DONALD[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]518[/TD]
[TD]CHECK[/TD]
[TD][/TD]
[TD]ADDRESS/CONFIRM[/TD]
[TD]UNIT[/TD]
[TD][/TD]
[TD]BROOKS[/TD]
[TD]GERRY[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]843[/TD]
[TD="align: right"]408[/TD]
[TD][/TD]
[TD]ALBANY[/TD]
[TD]ST[/TD]
[TD][/TD]
[TD]HARTLEY[/TD]
[TD]LAWRENCE[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]1004[/TD]
[TD="align: right"]410[/TD]
[TD][/TD]
[TD]ALBANY[/TD]
[TD]ST[/TD]
[TD][/TD]
[TD]621 TECHNOLOGIES IN.[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]929[/TD]
[TD="align: right"]413[/TD]
[TD]C[/TD]
[TD]ALBANY[/TD]
[TD]ST[/TD]
[TD][/TD]
[TD]SHAKERS LOUNGE[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]1090[/TD]
[TD="align: right"]430[/TD]
[TD][/TD]
[TD]ALBANY[/TD]
[TD]ST[/TD]
[TD][/TD]
[TD]HOBEN DENTISTRY[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]2094[/TD]
[TD="align: right"]436[/TD]
[TD][/TD]
[TD]ALBANY[/TD]
[TD]ST[/TD]
[TD][/TD]
[TD]BUDS PIZZA[/TD]
[TD]LORNE DOUGLAS[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]1451[/TD]
[TD="align: right"]437[/TD]
[TD][/TD]
[TD]ALBANY[/TD]
[TD]ST[/TD]
[TD][/TD]
[TD]DOUGLAS[/TD]
[TD]LORNE[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]13[/TD]
[TD="align: right"]432[/TD]
[TD="align: right"]1[/TD]
[TD]ALBANY[/TD]
[TD]ST.[/TD]
[TD][/TD]
[TD]SYKES[/TD]
[TD]STEVE[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]310[/TD]
[TD="align: right"]413[/TD]
[TD]A[/TD]
[TD]ALBANY[/TD]
[TD][/TD]
[TD][/TD]
[TD]OIL RIG RESTAURANT[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]1209[/TD]
[TD="align: right"]422[/TD]
[TD][/TD]
[TD]ALBANY[/TD]
[TD][/TD]
[TD][/TD]
[TD]PETROLIA INSTANT PRINT[/TD]
[TD]RANDY DRYDAK[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]


Thank you!
 
hiker95 this worked like a charm! Thank you so much! And the additional comments and conversation help me understand how it works. This is one more thing to help the end-users of this workbook I'm creating for them. Much appreciated. :biggrin:

BlondieC,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
I'm reading through other posts right now before I post my next question if I don't find something similar to what I'm trying to do now so I'll be here for a while. :biggrin:

BlondieC,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0

Forum statistics

Threads
1,224,532
Messages
6,179,388
Members
452,908
Latest member
MTDelphis

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