简体   繁体   中英

Pasting excel data into a powerpoint chart data sheet

I hope smeone can help me on this. I want to take dta from excel and paste into a power point chart, just like I do manually but with code. I want to automate the creation of a standard presentation that Icreate often for different populations of customers. (I am crunching the data in SAS and exporitn it from SAS into an excel file that has the same tabs and formatting each time)

I was able to write the attached macro which is just a first attempt with dummy date to see how it would work, and I copied liberally from other web sources. It is working until then end. It open spower point, cretres a page with my template and adds a chart of the right type. It opens the data sheet for that chart. The issue is that I am not able to paste the data.

I believe I am not referencing properly the sorce excel sheet so VBA is getting cnofused (as it should). Can someone exlain how I do that.

In the end I likely will modify this to open an existing chart and paste new data, as Irather do the formatting once that do all of that in code (I also alreay have the base presentation ready to go). But I wil need to be able to paste the data.

There are numerous examples on this site and elsewhere of how to copy a chart from Excel to PPT, but I do not want to do that, I want the PPT to store the data so it can be modified if needed after creation as needed. I however have not found one that does what I want to accomplish.

If it matters I am using office 2010.

Thanks in advance to anyone who can help me.

Mario

Sub test()

Set ObjPPT = CreateObject("PowerPoint.Application")
Set ObjPresentation = ObjPPT.Presentations.Add
ObjPresentation.ApplyTemplate ("C:\Documents and Settings\ewnym5s\My Documents\Tools\Clean PPT Page.potx")
Set ObjSlide = ObjPresentation.Slides.Add(1, 12)
Set mychart = ObjSlide.Shapes.AddChart(xlBarClustered, 200, 200, 500, 200).Chart

Set wb = mychart.ChartData.Workbook
Set ws = wb.Worksheets(1)
ws.ListObjects("Table1").Resize ws.Range("A1:D6")


Sheets("Sheet1").Select
Range("A1:D6").Select
Selection.Copy

'The macro works perfectly up to here. The data is on Excel Sheet1 A1:d6 and it needs to be pasted to the same range on Sheet1 of the PPT data sheet.

'I can see the data is selected and copied if I switch windows while stepping through the code. Just the paste part is not working.

ws.Range("A1").Paste

Thank you both for posting this as it was something I was having a hard time finding myself! Here is a cleaned up version of the code with changes made so that you can drag and drop this and be ready to go (for example declaring the variables and tweaks to get the code working properly)

Sub test()

Dim ObjPPT As PowerPoint.Application
Dim ObjPresentation As PowerPoint.Presentation
Dim ObjSlide As PowerPoint.Slide
Dim mychart As PowerPoint.Chart
Dim wb As Workbook
Dim ws As Worksheet
Dim MainWorkbook As String

Set ObjPPT = CreateObject("PowerPoint.Application")
Set ObjPresentation = ObjPPT.Presentations.Add
ObjPresentation.ApplyTemplate ("C:\Documents and Settings\ewnym5s\My Documents\Tools\Clean PPT Page.potx")
Set ObjSlide = ObjPresentation.Slides.Add(1, 12)
Set mychart = ObjSlide.Shapes.AddChart(xlBarClustered, 200, 200, 500, 200).Chart

Set wb = mychart.ChartData.Workbook
Set ws = wb.Worksheets(1)
ws.ListObjects("Table1").Resize ws.Range("A1:D6")

MainWorkbook = ThisWorkbook.Name
Windows(MainWorkbook).Activate
Sheets("Sheet1").Select
Range("A1:D6").Select
Selection.Copy
ws.Range("A1:D6").Value = Range("A1:D6").Value

End Sub

You can use the .value parameter to set values between different ranges. This will only set the values in the cells though, none of the formatting will be kept. This shouldn't matter if it's only data for a chart. You can always apply formatting by VBA afterwards anyway. Setting the value is also a lot more efficient than copying and pasting in Excel VBA.

Since the source and destination are both excel ranges the following will work:

ws.Range("A1:D6").Value = Range("A1:D6").Value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM