修剪宏获取错误

时间:2014-11-24 01:26:04

标签: excel-vba trim vba excel

我无法弄清楚这一行For Each c In ws.UsedRange.Cells我一直收到错误Method 'worksheet' range failed

由于

修改:已更新

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

1 个答案:

答案 0 :(得分:2)

尝试:For Each ws in Worksheets因为Sheets集合可以包含不是工作表的内容