使用VBA将图表图例复制到Powerpoint

时间:2011-10-12 10:19:41

标签: excel vba graph powerpoint legend

我处理一个创建powerpoint幻灯片的excel脚本。 在powerpoint幻灯片上,我想显示图表的图例,而不是图表的其余部分。

以下是我的代码的摘录:

With Sheets("data")
    Set bereich = Range(.Cells(Daty + 1, 3), .Cells(Daty + 2 + UBound(SubCategories), Datx + UBound(anzahl, 1)))
    Set dia = .ChartObjects.Add(10, 800, 650, 400)
   .ChartObjects(dia.Name).Activate
   dia.Name = "ideaspersubcatstatus"       
   .Shapes(dia.Name).Left = Range(.Cells(intSubCat + 3, 1), .Cells(intSubCat + 3, 1)).Left
   .Shapes(dia.Name).Top = Range(.Cells(intSubCat + 3, 1), .Cells(intSubCat + 3, 1)).Top
End With
With ActiveChart
    .ChartType = xlBarStacked
    .SetSourceData Source:=bereich, PlotBy:=xlColumns
    .HasLegend = True
    .PlotArea.Interior.ColorIndex = xlNone
    .Axes(xlCategory).TickLabelSpacing = 1
    .ChartArea.Border.LineStyle = 0
    .Axes(xlValue).MajorGridlines.Border.LineStyle = xlDot
End With

我需要将图表的图例复制到powerpoint,或者在将图例复制到Powerpoint之前删除图表中的图例。

    With .Slides(9)
        'copy graph from Excel
        Workbooks("data.xls").Worksheets("data").ChartObjects("ideaspersubcatstatus").Copy
        'paste graph into Powerpoint
        .Shapes.Paste        
    End With

“。PlotArea.Delete”不受支持。 “.ChartObjects(1).Legend.Copy”也没有用。

1 个答案:

答案 0 :(得分:2)

我认为您不能仅复制图例,或删除绘图区域 - 或删除其所有内容(系列)而图例不会消失。

你可以做的是使绘图区域非常小,并将其隐藏在图例后面:

With ActiveChart
    .Axes(xlCategory).Delete
    .Axes(xlValue).Delete
    .PlotArea.ClearFormats
    .Axes(xlValue).MajorGridlines.Delete
    ' Make it small
    With .PlotArea
        .Width = 0
        .Height = 0
    End With
    ' Move the legend on top of it
    With .Legend
        .Left = 1
        .Top = 1
    End With
End With

如果需要,可以缩小图表区域的大小,使其适合图例。