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

发布于 2024-12-09 07:14:11 字数 1270 浏览 0 评论 0原文

我正在编写一个创建 powerpoint 幻灯片的 Excel 脚本。 在幻灯片上,我想显示图表的图例,但不显示图表的其余部分。

以下是我的代码摘录:

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”也不起作用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长伴 2024-12-16 07:14:11

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

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

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

如果需要,您可以减小图表区域的大小,使其紧贴在图例周围。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文