Excel中可以用如下语句动态修改图表的数据源:
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SetSourceData Source:=Range("J3:O8")
请问在PPT中的图表,用VBA怎么来动态修改数据源?
我做了一个例子,可以获取图表后台的Sheet对象,但图表的对象无法获取。
- VB code
Sub CommandButton1_Click() With ActivePresentation.Slides(1) Dim i As Integer, j As Integer Dim strObjectName '获取嵌入对象的名称 For i = 1 To .Shapes.Count If .Shapes(i).Type = msoEmbeddedOLEObject Then strObjectName = .Shapes(i).Name End If Next i Dim objShapes As Object ''嵌入的对象 Dim objChart As Object ''图表对象 Dim objSheet As Object ''Sheet对象 Set objShapes = .Shapes(strObjectName).OLEFormat.Object objShapes.Worksheets("Sheet1").Cells(1, 7) = "Test" End WithEnd Sub
------解决方案--------------------------------------------------------
不用图表后台的Sheet对象,直接改Series的值:
ActivePresentation.Slides(2).Shapes(1).Chart.ChartType = xl3DPie
ActivePresentation.Slides(2).Shapes(1).Chart.SeriesCollection(1).Values = Array(99, 2)
------解决方案--------------------------------------------------------
解决了记得分享下。