Excel删除先前创建的表格

时间:2017-05-10 01:45:38

标签: excel vba excel-vba

我有一个宏,我已经设置了多个排序和过滤器,并在每个步骤结束时创建一个新工作表并粘贴过滤后的信息。

当我希望此人能够使用具有一组新数据的同一工作簿时,我希望通过删除所有先前创建的工作表来启动宏。

我尝试了多种样式的代码,这些样式都删除了工作表,但无法将其删除" P High Offstd。"

我已经尝试调整删除的顺序以及删除工作表的各种方法,但它拒绝删除该工作表。

我甚至尝试更改名称,因为它曾经被称为Processing High Offstd。任何帮助都是有益的。

Application.ScreenUpdating = False
For Each Sheet In ActiveWorkbook.Worksheets
If Sheet.Name = "Processing 0% Performance" Then
    Application.DisplayAlerts = False
    Worksheets("Processing 0% Performance").Delete
    Application.DisplayAlerts = True
End If
Next Sheet
For Each Sheet In ActiveWorkbook.Worksheets
If Sheet.Name = "Processing 0 OnStd" Then
    Application.DisplayAlerts = False
    Worksheets("Processing 0 OnStd").Delete
    Application.DisplayAlerts = True
End If
Next Sheet
For Each Sheet In ActiveWorkbook.Worksheets
If Sheet.Name = "P High OffStd" Then
    Application.DisplayAlerts = False
    Worksheets("P High OffStd").Delete
    Application.DisplayAlerts = True
End If
Next Sheet
For Each Sheet In ActiveWorkbook.Worksheets
If Sheet.Name = "Processing Low Performance" Then
    Application.DisplayAlerts = False
    Worksheets("Processing Low Performance").Delete
    Application.DisplayAlerts = True
End If
Next Sheet
With ActiveWorkbook.Worksheets("Processing")
.Range("AA1:AZ" & .Range("AA1").End(xlDown).Row).ClearContents
.Range("C3:C" & .Range("C3").End(xlDown).Row).ClearContents
.Range("E3:E" & .Range("E3").End(xlDown).Row).ClearContents
End With

1 个答案:

答案 0 :(得分:0)

您的代码可以简化为

With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
End With

For Each Sheet In ActiveWorkbook.Worksheets
    If Sheet.Name = "Processing 0% Performance" Or Sheet.Name = "Processing 0 OnStd" _
    Or Sheet.Name = "P High OffStd" Or Sheet.Name = "P High OffStd" Or Sheet.Name = "Processing Low Performance" Then
        Sheet.Delete
Next Sheet

With ActiveWorkbook.Worksheets("Processing")
    .Range("AA1:AZ" & .Range("AA1").End(xlDown).Row).ClearContents
    .Range("C3:C" & .Range("C3").End(xlDown).Row).ClearContents
    .Range("E3:E" & .Range("E3").End(xlDown).Row).ClearContents
End With
With Application
    .DisplayAlerts = True
    .ScreenUpdating = True
End With