PPT图表怎么用VBA控制修改数据源
发布网友
发布时间:2022-04-23 03:33
我来回答
共1个回答
热心网友
时间:2023-10-13 10:46
以下是通过VBA修改PPT图表数据源源代码,原理就是获取图表后台Link的Excel对象,再操作该对象。
Visual Basic code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Sub PPT_ChangeChart()
With ActivePresentation.Slides(1)
Dim oChart As Chart
Dim oChartData As ChartData
Dim gWorkBook As Excel.Workbook
Dim gWorkSheet As Excel.Worksheet
''Chart对象
Set oChart = ActivePresentation.Slides(1).Shapes(1).Chart
Set oChartData = oChart.ChartData
oChartData.Activate
Set gWorkBook = oChartData.Workbook
Set gWorkSheet = gWorkBook.Worksheets("Sheet1")
gWorkSheet.Cells(2, 1).Value = "Proct A"
gWorkSheet.Cells(3, 1).Value = "Proct B"
gWorkSheet.Cells(4, 1).Value = "Proct C"
gWorkSheet.Cells(5, 1).Value = "Proct D"
gWorkSheet.Cells(6, 1).Value = "Proct E"
gWorkSheet.ListObjects("Table1").Resize gWorkSheet.Range("A1:D6") '设置图标数据源区域
gWorkBook.Application.Quit
oChart.Refresh
Set gWorkSheet = Nothing
Set gWorkBook = Nothing
Set oChartData = Nothing
Set oChart = Nothing
End With
End Sub