首页 > 解决方案 > 使用 VBA 编辑图例

问题描述

我将图表(我在 excel 中准备的带有图例的图表)从 excel 复制到 .ppt(下面的代码)。如何更改/编辑 legend.Top 和 legend.size?

我的代码不起作用...

Sub pptfromexcel()
    Dim pptapp As PowerPoint.Application
    Dim pptppt As PowerPoint.Presentation
    Dim pptsld As PowerPoint.Slide
    Dim shp As Object
    Set chart1 = ActiveSheet.ChartObjects("Chart 1")
    'Dane do wykresów
    Set d5 = Sheets("Wykresy").Range("Q32:S40")
    Set d6 = Sheets("Wykresy").Range("Q47:S51")

    Set v1PK = Sheets("Wykresy").Range("G7:G7")
    Set v1PM = Sheets("Wykresy").Range("G8:G8")

    Set pptapp = New PowerPoint.Application
    Set pptppt = pptapp.Presentations.Open("C:\Users\Desktop\ppt.pptx")
    pptapp.Visible = True
    pptapp.Activate

    Set pptsld2 = pptppt.Slides(2)


    chart1.Copy
    Set chart1a = pptsld2.Shapes.PasteSpecial

    With chart1a
        .Height = 132
        .Width = 157
        .Left = 26.1
        .Top = 120
        .haslegend=true
        .legend.size = 12
        .legend.top = 150
    End With
End Sub

标签: excelvbachartslegend

解决方案


您设置的前四个属性是所有形状的通用属性。图例属性特定于图表。必须发生的第一件事是将其粘贴为 Excel 对象。如果是这种情况,您的形状上有一个 Chart 属性,您可以这样做:

With chart1a
    .Height = 132
    .Width = 157
    .Left = 26.1
    .Top = 120
    .Chart.HasLegend = True
    .Chart.Legend.Size = 12
    .Chart.Legend.Top = 150
End With

推荐阅读