VBA - Excel强制重启

时间:2017-12-05 19:07:17

标签: excel vba excel-vba excel-2007

我的代码存在重大问题,应该重置工作表,而不是关闭整个工作簿并强制重启。这不是问题,因为我添加了从On Error Resume Next开始的最后一段代码,所以只发生了这个问题。

Sub Reset()

    Application.ScreenUpdating = False

    Dim ws As Worksheet

    For Each ws In ThisWorkbook.Worksheets
        If ws.Name = "Set Up" Or ws.Name = "Report" Then
            Else:
                Application.DisplayAlerts = False
                ws.Delete
        End If
    Next

    Worksheets("Report").Cells.ClearContents

    On Error Resume Next
    Application.DisplayAlerts = False
    ThisWorkbook.Charts.Delete
    Application.DisplayAlerts = True
    On Error GoTo 0

End Sub

提前致谢

2 个答案:

答案 0 :(得分:2)

如果您有图表,那么您应该使用以下代码:

Sub DeleteChartSheets()

    Dim ch As Chart

    For Each ch In ThisWorkbook.Charts
        ch.Delete
    Next

End Sub

答案 1 :(得分:1)

这会删除workhseet中的图表,例如:activesheet:

Sub DeleteallCharts() 
    Dim chtObj As ChartObject 
    For Each chtObj In ActiveSheet.ChartObjects
        chtObj.Delete 
    Next 
End Sub

如果要删除工作簿中的所有图表,则必须循环遍历工作表,如下所示:

Sub DeleteallChartsInWorkbook() 
    Dim chtObj As ChartObject 
    Dim WS As Worksheet
    For Each WS in Thisworkbook.Worksheets
        For Each chtObj In WS.ChartObjects
            chtObj.Delete 
        Next chtObj 
    Next WS
End Sub