VBA宏帮助 - 从Excel 2016清除表格和单元格格式

时间:2018-01-31 23:00:12

标签: excel-vba vba excel

我正在尝试清除整个工作簿中的所有表格格式和单元格格式。这段代码让我大部分都在那里,但我不想清除单元格内容。

我尝试通过删除Selection.ClearContents字符串来修改此行为。但是,这使得代码无法正常运行。

非常感谢任何帮助。

谢谢!

Sub ClearAll()

    Set wbook = ActiveWorkbook

    For Each sht In wbook.Worksheets
       sht.Activate
       sht.Cells.Select
       Selection.ClearContents
       Selection.ClearFormats ' edit: clear formats too
       sht.Cells(1, 1).Select ' edit: select the first cell to cancel selection of the whole sheet
    Next sht


End Sub

1 个答案:

答案 0 :(得分:0)

不确定你的意思是非功能性的。无论如何,你不需要选择或激活任何东西。

Sub ClearAll()

Dim sht As Worksheet, wbook As Workbook, t As ListObject

Set wbook = ActiveWorkbook

For Each sht In wbook.Worksheets
    For Each t In sht.ListObjects
        t.Unlist
    Next t
    sht.Cells.ClearFormats
Next sht

End Sub