Excel 如何使用VBA动态引用PowerPoint幻灯片
我编写/编译了一个宏,该宏打开Excel文件,创建PowerPoint图表,并使用Excel文件中工作表中的数据填充图表工作表 我试图更改宏,使其在Excel文件的工作表中循环,并:Excel 如何使用VBA动态引用PowerPoint幻灯片,excel,powerpoint,vba,Excel,Powerpoint,Vba,我编写/编译了一个宏,该宏打开Excel文件,创建PowerPoint图表,并使用Excel文件中工作表中的数据填充图表工作表 我试图更改宏,使其在Excel文件的工作表中循环,并: 为每个工作表创建PowerPoint幻灯片和图表 使用Excel文件中工作表中的数据填充PowerPoint图表 目前,当我运行宏时,第一张PowerPoint图表和幻灯片已正确创建。第二张幻灯片是为Excel文件的第二张工作表创建的,但PowerPoint图表创建不正确。我正在测试宏的工作簿有两个工作表 动态引用
Set pptWorkSheet = pptWorkBook.Worksheets(ActivePresentation.Slides.Count) 'sorta works-changed 8/19
当我转到调试器时,调试器显示ActivePresentation.Slides.Count=2
,因此我不确定为什么它不将数据传输到第二个PowerPoint图表
我也可能没有在此处正确引用Excel文件工作表:
pptWorkSheet.Range("a2:b5").Value = xlWB.ActiveSheet.Range("a2:b5").Value
以下是完整的宏:
Sub CreateChartAllWKs()
'Create variables
Dim myChart As Chart
Dim pptChartData As ChartData
Dim pptWorkBook As Excel.Workbook
Dim pptWorkSheet As Excel.Worksheet
Dim xlApp As Excel.Application
Dim xlWB As Workbook
Dim xlWS As Worksheet
' Create new excel instance and open relevant workbook
Set xlApp = New Excel.Application
xlApp.Visible = True 'Make Excel visable
Set xlWB = xlApp.Workbooks.Open("C:\filepath\ExcelData.xlsm", True, False) 'Open relevant workbook
'Loop through each worksheet in xlWB and transfer data to new pptWorkBook and
'create new PowerPoint chart
For Each xlWS In ActiveWorkbook.Worksheets
'Add a new slide where we will create the PowerPoint worksheet and chart
ActivePresentation.Slides.Add ActivePresentation.Slides.Count + 1, ppLayoutText
ActiveWindow.View.GotoSlide ActivePresentation.Slides.Count
Set activeSlide = ActivePresentation.Slides(ActivePresentation.Slides.Count)
' Create the chart and set a reference to the chart data.
Set myChart = activeSlide.Shapes.AddChart.Chart 'changed 8/19
Set pptChartData = myChart.ChartData
' Set the PowerPoint Workbook and Worksheet references.
Set pptWorkBook = pptChartData.Workbook
Set pptWorkSheet = pptWorkBook.Worksheets(ActivePresentation.Slides.Count) 'sorta works-changed 8/19
' Add the data to the PowerPoint workbook.
pptWorkSheet.ListObjects("Table1").Resize pptWorkSheet.Range("A1:B5")
pptWorkSheet.Range("Table1[[#Headers],[Series 1]]").Value = "Items"
pptWorkSheet.Range("a2:b5").Value = xlWB.ActiveSheet.Range("a2:b5").Value 'transfer data from ExcelWB to pptWorkSheet (i.e. the PowerPoint workbook)
' Apply styles to the chart.
With myChart
.ChartStyle = 4
.ApplyLayout 4
.ClearToMatchStyle
End With
' Add the axis title.
With myChart.Axes(xlValue)
.HasTitle = True
.AxisTitle.Text = "Units"
End With
'Apply data labels
myChart.ApplyDataLabels
Next xlWS
' Clean up the references.
Set pptWorkSheet = Nothing
' pptWorkBook.Application.Quit
Set pptWorkBook = Nothing
Set pptChartData = Nothing
Set myChart = Nothing
'Clean up Excel references.
Set xlApp = Nothing
'Option to close excel workbook
'ExcelWB.Close
End Sub
我认为您遇到的问题是PowerPoint和Excel如何存储幻灯片编号和工作表编号。PowerPoint至少有3种不同的幻灯片属性,包括“幻灯片ID”、“幻灯片索引”和“幻灯片编号”。它们都是不同的,当你试图引用它们时,它们会让你感到痛苦。我喜欢做的是在创建幻灯片时,将幻灯片的参考设置为正确:
Set CurSlide = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count + 1, ppLayoutText)
这样,当您创建幻灯片时,您就有了对它的引用
此外,我发现使用数字作为工作表参考也可能导致问题,因为如果您参考第5张工作表,它可能根本不是第5张工作表。您必须在Excel的VBA编辑器中查看哪些工作表获得了哪些引用。但是,如果您能够引用工作表名称,如“Sheet1”、“Sheet2”、“OtherWorksheet”等,您可以使事情变得更简单。如果您制作一个名为“5”的工作表,然后使用调用该工作表,则可以更全面地了解这一点
Set ws = ActiveWorkBook.WorkSheets(5)
这是行不通的。你需要使用
Set ws = ActiveWorkBook.Worksheets("5")
希望这是有道理的。这一部分不是必需的,但如果您遇到问题,它会使调试变得更容易。我建议这样做的方式不在下面的代码中,因为我没有您的工作簿
Set PPtWorkSheet = pptWorkBook.Worksheets("Sheet" & CurSlide.SlideIndex)
我重新编写了几行您的代码,并且我能够让它工作。但是,我没有您的工作簿副本,因此我不能100%确定这是否可行。如果您仍然无法从幻灯片索引引用工作表,请考虑更改工作簿上的工作表名称。
修改后的代码如下,如果您有任何问题,请告诉我
Sub CreateChartAllWKs()
'Create variables
Dim myChart As Chart
Dim pptChartData As ChartData
Dim pptWorkBook As Excel.Workbook
Dim pptWorkSheet As Excel.Worksheet
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet
Dim CurSlide As Slide 'new from update
' Create new excel instance and open relevant workbook
Set xlApp = New Excel.Application
xlApp.Visible = True 'Make Excel visable
Set xlWB = xlApp.Workbooks.Open("C:\filepath\ExcelData.xlsm", True, False) 'Open relevant workbook
'Loop through each worksheet in xlWB and transfer data to new pptWorkBook and
'create new PowerPoint chart
For Each xlWS In ActiveWorkbook.Worksheets
'Add a new slide where we will create the PowerPoint worksheet and chart
'Set CurSlide = ActivePresentation.Slides.Add ActivePresentation.Slides.Count + 1, ppLayoutText
ActiveWindow.View.GotoSlide ActivePresentation.Slides.Count
'This is my recommendation
Set CurSlide = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count + 1, ppLayoutText)
' Create the chart and set a reference to the chart data.
Set myChart = CurSlide.Shapes.AddChart.Chart 'changed 8/19
Set pptChartData = myChart.ChartData
' Set the PowerPoint Workbook and Worksheet references.
Set pptWorkBook = pptChartData.Workbook
Set pptWorkSheet = pptWorkBook.Worksheets(CurSlide.SlideIndex) 'From Update
' Add the data to the PowerPoint workbook.
pptWorkSheet.ListObjects("Table1").Resize pptWorkSheet.Range("A1:B5")
pptWorkSheet.Range("Table1[[#Headers],[Series 1]]").Value = "Items"
pptWorkSheet.Range("a2:b5").Value = xlWB.ActiveSheet.Range("a2:b5").Value 'transfer data from ExcelWB to pptWorkSheet (i.e. the PowerPoint workbook)
' Apply styles to the chart.
With myChart
.ChartStyle = 4
.ApplyLayout 4
.ClearToMatchStyle
End With
' Add the axis title.
With myChart.Axes(xlValue)
.HasTitle = True
.AxisTitle.Text = "Units"
End With
'Apply data labels
myChart.ApplyDataLabels
Next xlWS
' Clean up the references.
Set pptWorkSheet = Nothing
' pptWorkBook.Application.Quit
Set pptWorkBook = Nothing
Set pptChartData = Nothing
Set myChart = Nothing
'Clean up Excel references.
Set xlApp = Nothing
'Option to close excel workbook
'ExcelWB.Close
End Sub
我认为您遇到的问题是PowerPoint和Excel如何存储幻灯片编号和工作表编号。PowerPoint至少有3种不同的幻灯片属性,包括“幻灯片ID”、“幻灯片索引”和“幻灯片编号”。它们都是不同的,当你试图引用它们时,它们会让你感到痛苦。我喜欢做的是在创建幻灯片时,将幻灯片的参考设置为正确:
Set CurSlide = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count + 1, ppLayoutText)
这样,当您创建幻灯片时,您就有了对它的引用
此外,我发现使用数字作为工作表参考也可能导致问题,因为如果您参考第5张工作表,它可能根本不是第5张工作表。您必须在Excel的VBA编辑器中查看哪些工作表获得了哪些引用。但是,如果您能够引用工作表名称,如“Sheet1”、“Sheet2”、“OtherWorksheet”等,您可以使事情变得更简单。如果您制作一个名为“5”的工作表,然后使用调用该工作表,则可以更全面地了解这一点
Set ws = ActiveWorkBook.WorkSheets(5)
这是行不通的。你需要使用
Set ws = ActiveWorkBook.Worksheets("5")
希望这是有道理的。这一部分不是必需的,但如果您遇到问题,它会使调试变得更容易。我建议这样做的方式不在下面的代码中,因为我没有您的工作簿
Set PPtWorkSheet = pptWorkBook.Worksheets("Sheet" & CurSlide.SlideIndex)
我重新编写了几行您的代码,并且我能够让它工作。但是,我没有您的工作簿副本,因此我不能100%确定这是否可行。如果您仍然无法从幻灯片索引引用工作表,请考虑更改工作簿上的工作表名称。
修改后的代码如下,如果您有任何问题,请告诉我
Sub CreateChartAllWKs()
'Create variables
Dim myChart As Chart
Dim pptChartData As ChartData
Dim pptWorkBook As Excel.Workbook
Dim pptWorkSheet As Excel.Worksheet
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet
Dim CurSlide As Slide 'new from update
' Create new excel instance and open relevant workbook
Set xlApp = New Excel.Application
xlApp.Visible = True 'Make Excel visable
Set xlWB = xlApp.Workbooks.Open("C:\filepath\ExcelData.xlsm", True, False) 'Open relevant workbook
'Loop through each worksheet in xlWB and transfer data to new pptWorkBook and
'create new PowerPoint chart
For Each xlWS In ActiveWorkbook.Worksheets
'Add a new slide where we will create the PowerPoint worksheet and chart
'Set CurSlide = ActivePresentation.Slides.Add ActivePresentation.Slides.Count + 1, ppLayoutText
ActiveWindow.View.GotoSlide ActivePresentation.Slides.Count
'This is my recommendation
Set CurSlide = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count + 1, ppLayoutText)
' Create the chart and set a reference to the chart data.
Set myChart = CurSlide.Shapes.AddChart.Chart 'changed 8/19
Set pptChartData = myChart.ChartData
' Set the PowerPoint Workbook and Worksheet references.
Set pptWorkBook = pptChartData.Workbook
Set pptWorkSheet = pptWorkBook.Worksheets(CurSlide.SlideIndex) 'From Update
' Add the data to the PowerPoint workbook.
pptWorkSheet.ListObjects("Table1").Resize pptWorkSheet.Range("A1:B5")
pptWorkSheet.Range("Table1[[#Headers],[Series 1]]").Value = "Items"
pptWorkSheet.Range("a2:b5").Value = xlWB.ActiveSheet.Range("a2:b5").Value 'transfer data from ExcelWB to pptWorkSheet (i.e. the PowerPoint workbook)
' Apply styles to the chart.
With myChart
.ChartStyle = 4
.ApplyLayout 4
.ClearToMatchStyle
End With
' Add the axis title.
With myChart.Axes(xlValue)
.HasTitle = True
.AxisTitle.Text = "Units"
End With
'Apply data labels
myChart.ApplyDataLabels
Next xlWS
' Clean up the references.
Set pptWorkSheet = Nothing
' pptWorkBook.Application.Quit
Set pptWorkBook = Nothing
Set pptChartData = Nothing
Set myChart = Nothing
'Clean up Excel references.
Set xlApp = Nothing
'Option to close excel workbook
'ExcelWB.Close
End Sub
是否从PowerPoint运行此代码?是@ShaiRado,我通过PowerPoint运行。是否从PowerPoint运行此代码?是@ShaiRado,我通过PowerPoint运行。谢谢您的帮助。我收到一个运行时错误:WIW(未知成员):整数超出范围。0不在1到0的有效范围内。这里,
ActiveWindow.View.GotoSlide-ActivePresentation.Slides.Count
。当我阻止ActiveWindow.View.GotoSlide-ActivePresentation.Slides.Count时,我得到一个运行时错误9:下标超出范围,结果与我在帖子中指出的相同。第一张幻灯片创建正确,但第二张幻灯片创建不正确。我不知道这是否重要。我的PowerPoint工作簿总是以没有幻灯片的开头。很抱歉回复太晚。错误最可能的原因是您没有任何幻灯片,导致它在幻灯片上返回0。Count
您可以在其周围附上IF
语句If actilvedpresentation.Slides.Count 0然后ActiveWindow.View.GotoSlide Active Presentation.Slides.Count结束If
I将xlWB.ActiveSheet.Range
更改为xlWS.Range
,一切都像魔咒一样工作。谢谢你的帮助。我确实采纳了你的建议。谢谢你的帮助。我收到一个运行时错误:WIW(未知成员):整数超出范围。0不在1到0的有效范围内。这里,ActiveWindow.View.GotoSlide-ActivePresentation.Slides.Count
。当我阻止ActiveWindow.View.GotoSlide-ActivePresentation.Slides.Count时,我会得到一个运行时错误