VBA遍历所有工作表并清除单元格内容

时间:2019-02-28 15:46:49

标签: excel vba loops

我有下面的代码,并且不断出现运行时错误438,有人可以帮助我调试吗?谢谢!

Sub my sub ()

Dim wb As Workbook
Set wb = Workbooks.Open(link to file)

Dim ws As Worksheet

For Each ws In wb
    ws.Cells.ClearContents
Next

End Sub

1 个答案:

答案 0 :(得分:-1)

尝试:

Option Explicit

Sub Test()

Dim ws As Worksheet
'Loop all sheet in the workbook
For Each ws In ThisWorkbook.Worksheets
    'Clear the used range of the sheet
    ws.UsedRange.Clear 'or .ClearContents
Next ws

End Sub