Excelファイルを開き、PowerPointグラフを作成し、Excelワークシートのデータでチャートワークシートを作成するマクロを作成/コンパイルしました。VBAを使用してPowerPointスライドを動的に参照する方法
私はExcelファイルのワークシートをループするマクロを変更しようとしています。各ワークシートに
- は、PowerPointのスライドを作成し、ワークシートからのデータをPowerPointのチャートを移入
- をグラフ化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
PowerPointからこのコードを実行していますか? –
はい@ShaiRado、私はPowerPointで実行しています。 – RTrain3k