首页 文章

如何使用VBA动态引用PowerPoint幻灯片

提问于
浏览
0

我编写/编译了一个宏,用于打开Excel文件,创建PowerPoint图表并使用Excel文件中工作表中的数据填充图表工作表 .

我正在尝试更改宏以循环访问Excel文件的工作表,并且:

  • 为每个工作表创建PowerPoint幻灯片和图表

  • 使用Excel文件中工作表中的数据填充PowerPoint图表

目前,当我运行宏时,第一个PowerPoint图表和幻灯片正确创建 . 为Excel文件的第二个工作表创建了第二张幻灯片,但未正确创建PowerPoint图表 . 我正在测试宏的工作簿有两个工作表 .

动态引用每个新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

1 回答

  • 2

    我认为您遇到的问题是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
    

相关问题