我有一个生成大量工作表和图表的宏。还有各种子程序运行,因此生成的每个工作表/图表的名称和数量永远不会相同。什么是常量是我的HOME工作表,它是用户的UI,我希望它不受影响。
我在这里发现了一个类似的问题,即除了你正在使用的工作表(即HOME)之外的所有工作表。这是我到目前为止所拥有的。
Sub ZRESET()
Dim ws As Worksheet, wb As Workbook
Set wb = ActiveWorkbook
Sheets("HOME").Select
Application.DisplayAlerts = False
For Each ws In wb.Worksheets
If ws.Name <> "HOME" Then
ws.Delete
End If
If Chart.Name = "" Then
Charts.Delete
End If
Next
Application.DisplayAlerts = True
Range("B5:E5,B9:E9,B13:E13,B14:E14").ClearContents
Range("A1").Select
End Sub
工作表删除很好,我挂的是图表。我尝试了各种尝试删除图表,有时它们工作(即在FOR循环和IF语句之外放置Charts.Delete)。但这需要我在工作簿中实际拥有一个图表。有时用户可以开发工作表而不用图表。
有任何建议继续我的目标是删除SHEETS 和/或 CHARTS,同时保持我的HOME表原封不动?
答案 0 :(得分:1)
Option Explicit
Sub GetRid()
Dim ASheet As Worksheet
Dim AChart As Chart
Application.DisplayAlerts = False
Application.ScreenUpdating = False
'** first scan for and delete all non HOME worksheets ***
For Each ASheet In ActiveWorkbook.Worksheets
If UCase(ASheet.Name) <> "HOME" Then
ASheet.Delete
End If
Next
'** Now scan and delete any ChartSheets ****
For Each AChart In ActiveWorkbook.Charts
AChart.Delete
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
答案 1 :(得分:0)
Option Explicit
Sub AllSheetsAndcharts()
Dim AChart As ChartObject
Dim ASheet As Worksheet
Application.DisplayAlerts = False
For Each ASheet In ActiveWorkbook.Worksheets
If UCase(ASheet.Name) <> "HOME" Then
For Each AChart In ASheet.ChartObjects
AChart.Delete
Next
ASheet.Delete
End If
Next
Application.DisplayAlerts = False
End Sub