これは私が使用するアプローチです:
- [チャートの挿入] を使用して、最初に PPT でチャートを設定します。
- 次に、VBA から、グラフごとに Excel ソース ファイルからデータを収集し、そのデータを
array
変数に格納します。
- これらの変数を使用して、チャートの系列データを更新します (または、パワーポイント チャートの埋め込みワークシートを更新します
.ChartData
)。
リンク/埋め込みに OLEObjects を使用するなどの方法は他にもありますが、率直に言って、それらを使用するのは面倒であり、ファイルが共有ドライブにある場合、ファイルが移動または名前変更された場合などに問題が発生する可能性があります。
これが、上で説明した一般的なフレームワークです。
これにはかなりの量の変更が必要になります。たとえば、これは 1 つのスライドの 1 つのグラフに対してのみ構成されており、Excel のデータがどのように配置されているかわかりません。そのため、方法を示すダミー コードを挿入しました。私は Excel からいくつかの値を取得します。明らかに、すべてのチャートで動作するのに十分動的になるように、かなりの量のコードでそれを微調整する必要があります (データが適切に整理されていれば、これは十分に簡単に実行できます。 Excel VBAの使い方を知ってください)。
Option Explicit
Option Base 1
Sub GetChartDataFromXLS()
Dim wbFileName As String '## full filename & path of the Excel file.'
Dim oXL As Object
Dim xlWB As Object
Dim xlWS As Object
Dim cl As Object
Dim c As Long
Dim shp As Shape
Dim cht As Chart
Dim srs As Series
Dim x As Long
Dim sArray() As Variant '## temporary array for each series, will be stored in chtData array.'
Dim chtData() As Variant '## I would use this array to store several arrays from the Excel file.'
Dim s As Long
wbFileName = "C:\users\david_zemens\desktop\dummy chart data.xlsx"
Set oXL = CreateObject("Excel.Application")
oXL.Visible = True
Set xlWB = oXL.Workbooks.Open(wbFileName)
'## iterate over the shapes in the slide.'
For Each shp In ActivePresentation.Windows(1).Selection.SlideRange(1).Shapes
'## check to see if this shape is a chart.'
If shp.HasChart Then
'## set the chart variable.'
Set cht = shp.Chart
'## clear out any existing series data in the chart'
For s = cht.SeriesCollection.Count To 1 Step -1
Set srs = cht.SeriesCollection(s)
srs.Delete
Next
'##Your code to get the chtData will go in this block:'
'##
Set xlWS = xlWB.Sheets(1) ' ##Modify to get the correct sheet where the data for this chart resides'
'## It will probably be something like this, which '
' iterates over some columns and collects data in to a series'
' of arrays, stored within chtData array '
For x = 1 To 3 'However Many Series you need to add:'
'Assuming data series begins in column A, etc...'
c = 1
For Each cl In xlWS.Range("A1:A10").Offset(0, x - 1)
ReDim Preserve sArray(c)
sArray(c) = cl.Value
c = c + 1
Next
'ReDim Preserve the chtData array
ReDim Preserve chtData(x)
chtData(x) = sArray
Next x
'## End collection of the chart data.
'## Expose the data sheet but minimize it to preserve updating
cht.ChartData.Activate
cht.ChartData.Workbook.Application.WindowState = -4140
'## Now, take that data and insert it to the chart
If LBound(chtData) >= 1 Then
For s = LBound(chtData) To UBound(chtData)
'## Add a new series to the chart
Set srs = cht.SeriesCollection.NewSeries
srs.Values = chtData(s) '## Modify this line to point at the appropriate array from chtData'
'manipulate the other series properties here '
'srs.Name = "whatever the series name" '
'srs.XValues = "whatever the series value" '
'# etc...
'# etc...
Next 'Next series...
End If
'## Close the chartdata sheet.
cht.ChartData.Workbook.Close
End If
Next
oXL.ActiveWorkbook.Close
oXL.Quit
On Error Resume Next
Set oXL = Nothing
Set xlWB = Nothing
Set xlWS = Nothing
On Error GoTo 0
End Sub
このメソッドは、チャートのデータ シートに書き込みません。率直に言って、マクロ主導のダッシュボードを作成している場合、データ シートが必要な理由はないはずですが、何らかの理由でデータ シートが必要な場合は、グラフのシリーズの作成方法を変更できます。