删除空图

时间:2019-01-18 13:13:37

标签: vba

我正在使用此代码删除空白图表,但显示错误

  

未定义对象错误424

Sub HideEmptyCharts()
Dim wksCharts As Worksheet
Dim objCO As ChartObject
    ' Set up a variable for the worksheet containing the charts
    Set wksCharts = ThisWorkbook.Sheets("Sheet2")

    ' Loop through every embedded chart object on the worksheet
    For Each objCO In wksCharts.ChartObjects

        ' Make each one visible
        objCO.Delete = True

        ' If the chart is empty make it not visible
        If IsChartEmpty(objCO.Chart) Then objCO.Delete = False

    Next objCO

End Sub

Private Function IsChartEmpty(chtAnalyse As Chart) As Boolean

Dim i As Integer
Dim j As Integer
Dim objSeries As Series

    ' Loop through all series of data within the chart
    For i = 1 To chtAnalyse.SeriesCollection.Count

        Set objSeries = chtAnalyse.SeriesCollection(i)

        ' Loop through each value of the series
        For j = 1 To UBound(objSeries.Values)

            ' If we have a non-zero value then the chart is not deemed to be empty
            If objSeries.Values(j) <> 0 Then
                ' Set return value and quit function
                IsChartEmpty = False
                Exit Function
            End If

        Next j

    Next i

    IsChartEmpty = True

End Function

0 个答案:

没有答案