Runtime Error 13 Type Mismatch

dgr

Board Regular
Joined
Apr 24, 2005
Messages
176
Hello,
I'm using Excel 2013. I was running the macro below to trim all excess spaces out of cells. The macro was running fine until today. Now, I get the Runtime Error 13 Type Mismatch. I'm not sure why. Is there a more reliable code you could recommend? Thanks.
Code:
Sub TrimXcessSpaces()
 'On Error Resume Next
'I could un-comment the line above to "solve" the problem but I'm not sure if that would be a good idea.
 
    Dim cl As Variant
    
    With ActiveSheet.UsedRange

    For Each cl In ActiveSheet.UsedRange
        If Len(cl) > Len(WorksheetFunction.Trim(cl)) Then 'this part is yellow
            cl.Value = WorksheetFunction.Trim(cl)
        End If
    Next cl
End With
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Perhaps

Code:
Sub TrimXcessSpaces()
 'On Error Resume Next
'I could un-comment the line above to "solve" the problem but I'm not sure if that would be a good idea.
 
Dim cl As Range


For Each cl In ActiveSheet.UsedRange
    If Not IsError(cl) Then
        If Len(cl) > Len(WorksheetFunction.Trim(cl)) Then 'this part is yellow
            cl.Value = WorksheetFunction.Trim(cl)
        End If
    End If
Next cl
End Sub
 
Upvote 0
Yes, this was much more reliable. Thanks once again VoG. I appreciate your help. Have a good day.
 
Upvote 0

Forum statistics

Threads
1,214,861
Messages
6,121,971
Members
449,059
Latest member
oculus

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