職場上,有些公司特喜歡用PPT作簡報,然後簡報裏又要有一大堆數據圖表,每月又要花很多時間更新數據,萬一有時不小心格式貼錯了,又會讓PPT變的異常肥大,肥大到很難開啟。Bot.學習.人 因為常常要做這樣的事,所以後來決定寫一個簡單的VBA,讓自己的PPT 可以讀取Excel 裏的值,然後貼在PPT的圖表裏。避免不小心造成PPT肥大,以及可以減少自己人工作業貼值的時間。
程式碼如下:
Private Sub CommandButton1_Click()如此,按下CommandButton1, 就會更新PPT後圖表中的數據了。
Dim xlApp As Excel.Application
Dim xlWorkBook As Workbook
Dim xlSheet As Worksheet
Dim iCount, jCount As Integer
Dim myPresentation As PowerPoint.Presentation
Set myPresentation = ActivePresentation
Set xlApp = New Excel.Application
Set xlWorkBook = xlApp.Workbooks.Open("D:\Personal\blogger\ppt_data.xlsx")
Set xlSheet = xlWorkBook.Worksheets("Income")
With myPresentation.Slides("Slide_Income").Shapes("Chart_Body").Chart.ChartData
For iCount = 2 To 2
For jCount = 2 To 13
.Workbook.Sheets("工作表1").Cells(iCount, jCount).Value = xlSheet.Cells(iCount, jCount).Text
Next jCount
Next iCount
.Workbook.Close
End With
xlWorkBook.Close
End Sub