xyz
xyz

Reputation: 2300

Trim Macro gets error

I cant figure out this line For Each c In ws.UsedRange.Cells I keep getting error Method 'worksheet' range failed

Thanks

Edit: updated

Sub trimAll()
Dim ws As Worksheets
Dim c As Range

For Each ws In WorkSheets
    If ws.Name = "XXX" Or ws.Name = "YYY" Then
       For Each c In ws.UsedRange.Cells
           If Not (IsEmpty(c) Or IsError(c)) Then
              c.Value = Trim(c.Value)
           End If
       Next c
    End If
Next ws
End Sub

Upvotes: 0

Views: 71

Answers (1)

barrowc
barrowc

Reputation: 10679

Try: For Each ws in Worksheets because the Sheets collection can contain things which aren't worksheets

Upvotes: 2

Related Questions