图表可以位于工作表中,也可以单独位于图表工作表中。
对于位于工作表中的图表,可以通过遍历Shape对象判断其Type属性后删除。
也可以通过遍历ChartObject对象将其删除。
一、通过遍历Shape对象判断其Type属性后删除
Sub exceloffice() '作者QQ:1722187970,微信:xycgenius,微信公众号exceloffice Dim oSP As Shape Dim oWK As Worksheet Set oWK = Excel.ActiveSheet With oWK For Each oSP In .Shapes With oSP If .Type = msoChart Then .Delete End If End With Next End With End Sub
二、遍历ChartObject对象将其删除
代码如下:
Sub exceloffice() '作者QQ:1722187970,微信:xycgenius,微信公众号exceloffice Dim oChart As Chart Dim oChartObject As ChartObject Dim oSeries As Series Dim oWK As Worksheet Set oWK = Excel.ActiveSheet With oWK If .ChartObjects.Count > 0 Then '先删除所有图表 .ChartObjects.Delete End If End With End Sub
发表评论