【问题标题】:How do you change the default data of a chart in PowerPoint with VBA?如何使用 VBA 更改 PowerPoint 中图表的默认数据?
【发布时间】:2021-06-04 09:19:53
【问题描述】:

我想创建一个聚集柱形图并将其添加到 PowerPoint 幻灯片中。我不想在 Excel 中创建该图表,然后使用 PowerPoint.Slide.Shapes.PasteSpecial 方法将其复制过来。我可以毫无问题地创建图表,并在有限的范围内与它进行交互(例如,我可以禁用图例,请参阅代码)。但是,我不知道如何编辑图表所引用的数据。

Private Sub AddGraph()

' Create a new slide
Dim PowerPointApplication As PowerPoint.Application
Set PowerPointApplication = New PowerPoint.Application

Dim Presentation As PowerPoint.Presentation
Set Presentation = PowerPointApplication.Presentations(1)

Dim Slide As PowerPoint.Slide
Set Slide = Presentation.Slides.Add(Presentation.Slides.Count + 1, ppLayoutText)

' Remove the existing shape 2
Slide.Shapes(2).Delete

' Add the chart
Slide.Shapes.AddChart _
    Type:=xlColumnClustered

With Slide.Shapes(2).Chart

    .Legend.Delete

    ' Try to edit some of the default data of the cluster column chart
    ' This causes a Subscript out of Range error 
    WorkBooks("Chart in Microsoft PowerPoint").Cells(2,1) = 2021
    
End With

End Sub

当 PowerPoint 创建图表时,它似乎还创建了一个临时(?)Excel 工作簿来存储数据。我尝试访问该工作簿以编辑默认数据,例如 WorkBooks("Chart in Microsoft PowerPoint").Cells(2,1) = 2021,但这会抛出下标超出范围错误。

有没有办法编辑在 PowerPoint 中创建图表时创建的 Excel 工作簿?我是否在一条没有结果的道路上走得太远了;有没有更简单的方法来完成这项工作(不包括在 Excel 工作簿中创建所有内容,然后将该图表复制到 PowerPoint 演示文稿中)?

【问题讨论】:

    标签: vba charts powerpoint


    【解决方案1】:

    ChartData 是缺少的链接。以下是更改工作表值的典型代码:

    With Slide.Shapes(2).Chart
        With .ChartData
            .Activate
            strText = .Workbook.Worksheets(1).Range("B2").Value
            strText = Replace(strText, "4.3", "5.2")
            .Workbook.Worksheets(1).Range("B2").Value = strText
            .Workbook.Close
        End With
    End With
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-18
      • 1970-01-01
      相关资源
      最近更新 更多