Copy Legend of Chart to Powerpoint with VBA
I work on an excel script that creates powerpoint slides. On a powerpoint slide, I want to show the legend of a chart, but not the rest of the chart.
Here is an excerpt of my code:
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
I either need to copy the legend of the graph to powerpoint, or remove everything but the legend from the chart before I copy it to 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" is not supported. ".ChartObjects(1).Legend.Copy" did not work, either.
I don't think you can copy only the legend, or delete the plot area -- or delete all of its contents (series) without the legend disappearing.
What you can do is make the plot area really small, and hide it behind the legend:
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
If you want, you can then reduce the size of your Chart area so that it fits snuggly around the legend.
精彩评论