2016-08-21 7 views
0

Excelファイルを開き、PowerPointグラフを作成し、Excelワークシートのデータでチャートワークシートを作成するマクロを作成/コンパイルしました。VBAを使用してPowerPointスライドを動的に参照する方法

私はExcelファイルのワークシートをループするマクロを変更しようとしています。各ワークシートに

  1. は、PowerPointのスライドを作成し、ワークシートからのデータをPowerPointのチャートを移入
  2. をグラフ化Excelファイル

現在、マクロを実行すると、最初のPowerPointグラフとスライドが正しく作成されます。 2番目のスライドは、Excelファイルの2番目のワークシートに対して作成されますが、PowerPointのグラフは正しく作成されません。マクロをテストしているワークブックには2つのワークシートがあります。

新しいPowerPointスライドをそれぞれ動的に参照する正しい方法は何ですか?今のところ、私が使用してきた:私は、デバッガに行くとき

Set pptWorkSheet = pptWorkBook.Worksheets(ActivePresentation.Slides.Count) 'sorta works-changed 8/19 

をそれがActivePresentation.Slides.Count = 2はので、私はその第二PowerPointのグラフにデータを転送していない理由として確認していないと言います。私はあなたがに実行している問題は、どのようにパワーポイントだと思う

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 
+0

PowerPointからこのコードを実行していますか? –

+0

はい@ShaiRado、私はPowerPointで実行しています。 – RTrain3k

答えて

2

と:

私も正しくここにExcelファイルのワークシートを参照されないことがあります。以下は

pptWorkSheet.Range("a2:b5").Value = xlWB.ActiveSheet.Range("a2:b5").Value 

がいっぱいマクロですExcelのスライド番号とワークシート番号を格納します。 「スライドID」、「スライドインデックス」、「スライド番号」など、スライドには少なくとも3つの異なる属性があります。あなたがそれらを参照しようとするとき、彼らはすべて異なっていて、物事を苦痛にさせます。あなたは今、それへの参照を持っているスライドを作成右

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 
+0

ご協力いただきありがとうございます。 **実行時エラーが発生しています:Wiew(未知のメンバ):整数が範囲外です。 0は有効範囲1〜0 **ではありません。ここ、 'ActiveWindow.View.GotoSlide ActivePresentation.Slides.Count'です。 – RTrain3k

+0

「ActiveWindow.View.GotoSlide ActivePresentation.Slides.Count'をブロックすると**実行時エラー9:下付き文字が範囲外です**と結果が私の投稿と同じです。最初のスライドは正しく作成されますが、2番目のスライドは作成されません。私はそれが重要なのかどうか分からない。 – RTrain3k

+0

遅く返事を申し訳ありません。このエラーの最も一般的な理由は、Slides.Countに0を返すようなスライドはまだありません。その周りにIF文を囲むことができます。 'If ActilvePresentation.Slides.Count <> 0 Then ActiveWindow.View.GotoSlideアクティブなPresentation.Slides.Count End If' – excelledsoftware

関連する問題