从ChartObjects获取图表标题

时间:2019-04-02 17:38:37

标签: excel vba excel-charts

我希望能够通过标题(或我给它的其他非默认名称)识别图表。在这里,我试图删除它是否具有特定标题:

Sub delchart()

Call create_chart
Dim d

For Each d In Worksheets("sheet1").ChartObjects

    Debug.Print d.Name '<-not a usable name

    If d.ChartTitle = "Scatter Chart" Then '<- errors
        d.Delete
    End If
Next d
End Sub

这是我创建图表的方式:

Sub create_chart()


    Dim sh As Worksheet
    Dim chrt As Chart

    Set sh = ActiveWorkbook.Worksheets("Sheet1")
    Set chrt = sh.Shapes.AddChart.Chart
    With chrt
        'Data?
        .ChartType = xlXYScatter
        .SeriesCollection.NewSeries
        .SeriesCollection(1).Name = "=""Scatter Chart"""
        .SeriesCollection(1).XValues = "=Sheet1!$A$2:$A$11"
        .SeriesCollection(1).Values = "=Sheet1!$B$2:$B$11"

        'Titles
        .HasTitle = True
        .ChartTitle.Characters.Text = "Scatter Chart"
        .Axes(xlCategory, xlPrimary).HasTitle = True
        .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "X values"
        .Axes(xlValue, xlPrimary).HasTitle = True
        .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Y values"
        .Axes(xlCategory).HasMajorGridlines = True

        'Formatting
        .Axes(xlCategory).HasMinorGridlines = False
        .Axes(xlValue).HasMajorGridlines = True
        .Axes(xlValue).HasMinorGridlines = False
        .HasLegend = False
        .Location Where:=xlLocationAsObject, Name:="Sheet1"
        '.Name = "my scatter 1"
    End With

End Sub

如果图表具有确定的标识,该如何删除?图表标题对我来说是最直观的,但似乎无法通过ChartObject访问标题。

1 个答案:

答案 0 :(得分:1)

您可以像这样访问 // while (list($id, $name) = each($attr_array)) { foreach($attr_array as $feKey => $feRow){ // $dom[$key]['attribute'][$name] = $attr_array[$id]; $dom[$key]['attribute'][$feRow] = $attr_array[$feKey]; ... ... } unset($feKey,$feRow); 文本:

ChartTitle
相关问题