如何从Excel更新PowerPoint图表中的范围

时间:2019-02-15 10:41:02

标签: excel vba powerpoint powerpoint-vba

我从一个Excel工作簿运行了VBA脚本,该工作簿还包含所有数据。如果添加新的数据点,脚本会自动更新excel中的图表。但是,当我在Powerpoint中放置相同的图表时,添加新数据点后它不再更新。

我尝试使用图表的各种链接版本。其中一个可以完成工作,但是那只是powerpoint中的一个图像,运行VBA脚本时,我需要更新实际的图表。

在下面的代码中,我尝试直接访问Powerpoint图表并仅更新其范围。我已经尝试过各种版本,但是无法正常工作。它会打开Powerpoint并识别形状,但不能更改范围。

Dim PPTApp As Object
Dim PPTPres As Object
Dim ppSlide As PowerPoint.Slide

Set PPTApp = CreateObject("PowerPoint.Application")
PPTApp.Visible = True

Set PPTPres = PPTApp.Presentations.Open("file:///C:\Users\user.name\Desktop\pptest111.pptx")
PPTPres.Windows(1).Activate

PPTPres.Slides(2).Shapes("Diagram1").Chart.ChartData.Sheets("sht2").Range ("A5:A15")

1 个答案:

答案 0 :(得分:0)

要更改图表的范围,必须更改标签和数据的范围。

XValues是标签的范围,而value是数据的范围

如果oSh是PPTPres.Slides(2).Shapes(“ Diagram1”)

然后应该可以使用它的某些变体

  oSH.Chart.SeriesCollection(1).XValues = "sht2!$A$5:$A15"  ' change the series range for labels
  oSH.Chart.SeriesCollection(1).values = "sht2!$B$5:$B15"   ' change the series range for data