2016-08-22 21 views
0

我在PowerPoint中創建了一個打開Excel工作簿的宏,循環顯示工作簿中的工作表,創建PowerPoint圖表並使用Excel工作表中的數據填充它們。爲了清楚起見,宏從PowerPoint運行。如何將數據從Excel傳輸到PowerPoint工作表並更新PowerPoint圖表範圍?

我現在需要製作數據範圍(從Excel轉換爲Powerpoint工作表時)和PowerPoint圖表數據範圍動態。例如。因爲每個Excel工作表範圍都不相同,因此每個PowerPoint圖表數據範圍都不相同。

下面是我的宏:

Sub CreateChartAllWKsv3() 

    '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 
     Dim CurSlide As Slide 'new from update 
     Dim LastRow As Long ' 8/22 
     Dim LastColumn As Long ' 8/22 

    ' 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:\ExcelWorkbook.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 xlWB.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 
        ' 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("Sheet1") 
        'Clear contents from PowerPoint worksheet 
          pptWorkSheet.UsedRange.ClearContents 'Works 
        'Find Last Row and Column of xlWS 
          LastRow = xlWS.Cells(1, 1).SpecialCells(xlCellTypeLastCell).Row 
          LastColumn = xlWS.Cells(1, 1).SpecialCells(xlCellTypeLastCell).Column 
        ' Add the data to the PowerPoint workbook. 
          xlWS.Range(Cells(1, 1), xlWS.Cells(LastRow, LastColumn)).Copy 'Fails to past any data on the second worksheet 
          pptWorkSheet.Range("A1").PasteSpecial Paste:=xlPasteValues 
        ' Update PowerPoint workbook chart data reference. 
          'line below didn't work        
          pptWorkSheet.ListObjects("Table1").Resize pptWorkSheet.Range("Table1[#All]").Resize(Rows.Count, Columns.Count) 

        ' 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 
      xlWB.Close 
      'Option to close the excel application 
    End Sub 

我運行到2個問題:

  1. xlWS.Range(Cells(1, 1), xlWS.Cells(LastRow, LastColumn)).CopypptWorkSheet.Range("A1").PasteSpecial Paste:=xlPasteValues將數據傳送到第一PowerPoint中的工作表,但未能在第二 - 沒有粘貼。
  2. pptWorkSheet.ListObjects("Table1").Resize pptWorkSheet.Range("Table1[#All]").Resize(Rows.Count, Columns.Count)無法調整到PowerPoint工作表上的PowerPoint圖表範圍。我收到method failed錯誤。

編輯 我對第一個問題的解決方法是剛剛轉會大範圍,我的數據會不會比使用pptWorkSheet.Range("a1:z100").Value = xlWS.Range("a1:z100").Value大。

回答

0

對不起,我沒有足夠的知名度來添加評論。

是否有一個原因,它不能使用鏈接的數據呢?

我目前有powerpoint顯示使用excel數據,只是讓它鏈接到拉動最新的信息,當我選擇打開更新鏈接。

謝謝

+0

不幸的是,圖表必須使用PowerPoint工作表等在PowerPoint中創建。 – RTrain3k

相關問題