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.

Old Box#Civic#UnitStreet NameStreet TypeDirectionLast NameFirst NameConvenience BoxComments
1660CENTRAL LAMBTON FAMILY HEALTH TEAM269
1247OPTIMIST CLUB156
2001MOVEDCHECKZAVITZDONALD
518CHECKADDRESS/CONFIRMUNITBROOKSGERRY
843408ALBANYSTHARTLEYLAWRENCE
1004410ALBANYST621 TECHNOLOGIES IN.
929413CALBANYSTSHAKERS LOUNGE
1090430ALBANYSTHOBEN DENTISTRY
2094436ALBANYSTBUDS PIZZALORNE DOUGLAS
1451437ALBANYSTDOUGLASLORNE
134321ALBANYST.SYKESSTEVE
310413AALBANYOIL RIG RESTAURANT
1209422ALBANYPETROLIA INSTANT PRINTRANDY DRYDAK

<tbody>
</tbody>


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

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
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,215,869
Messages
6,127,414
Members
449,382
Latest member
DonnaRisso

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