如何从 excel 更新 powerpoint 图表中的范围
How to update the range in powerpoint chart from excel
我 运行 来自 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")
要更改图表的范围,您必须更改标签和数据的范围。
XValues 是标签的范围,values 是数据的范围
如果 oSh 是 PPTPres.Slides(2).Shapes("Diagram1")
那么这个的一些变体应该可以工作
oSH.Chart.SeriesCollection(1).XValues = "sht2!$A:$A15" ' change the series range for labels
oSH.Chart.SeriesCollection(1).values = "sht2!$B:$B15" ' change the series range for data
mooseman 回答有效,但您需要更新图表 AfterWords。对我来说,LinkFormat.Update 没有拾取任何形状,所以我添加了这个来解决它
With oSH.Chart.ChartData
.Activate
.Workbook.Close
End With
我 运行 来自 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")
要更改图表的范围,您必须更改标签和数据的范围。
XValues 是标签的范围,values 是数据的范围
如果 oSh 是 PPTPres.Slides(2).Shapes("Diagram1")
那么这个的一些变体应该可以工作
oSH.Chart.SeriesCollection(1).XValues = "sht2!$A:$A15" ' change the series range for labels
oSH.Chart.SeriesCollection(1).values = "sht2!$B:$B15" ' change the series range for data
mooseman 回答有效,但您需要更新图表 AfterWords。对我来说,LinkFormat.Update 没有拾取任何形状,所以我添加了这个来解决它
With oSH.Chart.ChartData
.Activate
.Workbook.Close
End With