Can anyone please help me with the code? Im looking to create a macro where I can update the Powerpoint's chart from the data selected/copied in the excel file. Below are the following steps to understand my request well:
New data are available in excel file. I will the select/copy the data
Charts are existed in the powerpoint, but the data are old. Need to update only the data which are copied/selected in the excel file using VBA automations.
Below are my code, pls assist.
Sub update_Chart()
Dim myDocument As Variant
Dim myChart As Variant
Dim Wb As Object
Dim App As Object
Dim clipboard As MSForms.DataObject
Set clipboard = New MSForms.DataObject
Set myChart = ActiveWindow.Selection.ShapeRange(1).Chart
myChart.ChartData.Activate
Set Wb = myChart.ChartData.Workbook
Set App = Wb.Application
clipboard.SetText ActiveSheet.Range("A1")
clipboard.PutInClipboard
Wb.Close (0)
End Sub
Now the above code does not able to update the new data.
Related
I'm sorry but after three days I Give Up... So I'm asking what should be a very simple question but every example I find opens an Excel file or opens a read Only version of the same file, to do something where as in my case the file has already been opened from another macro using the code below, I just can't seem to figure how to adapt it for a file already open??
I'm also trying to figure out how to move excel to the active window from within the code? I would really appreciate any help
Dim oApp As Object
Dim x As Variant
Dim sPath As String
Dim oExcel As Excel.Application
Dim oWB As Workbook
Dim oSheet As String
sPath = "E:\Special Folders\WWWRoot\temp.xlsx"
oSheet = "--Keywording--"
On Error Resume Next
Set oExcel = New Excel.Application
Set oWB = oExcel.Workbooks.Open(sPath)
oExcel.Visible = True
Sheets(oSheet).Select
Range("A1:G1000").Clear
Range("A1").Select
Sheets(oSheet).Cells(1, 1).Select
Sheets(oSheet).PasteSpecial (xlPasteAll)
Range("A1").Select
Sorry, but can't you just turn on the Macro Recorder and get what you want?
Sub TryThis()
Windows("SecondaryWorkbook.xlsb").Activate
Range("A1").Select
Windows("PrimaryWorkbook.xls").Activate
Range("A1").Select
End Sub
I believe you need to be in the same instance of Excel, or this wonr't work. AFAIK, different instances of Excel don't communicate with each other...at all.
I am trying to copy many ppt slides (from the same ppt) onto an existing publisher document. I've been trying to piece a code together to do this but I can't figure it out due to lack of experience.
I preferably want to only copy the shape on each ppt slide and be able to update the publisher document if any changes are made to the ppt. To complicate things is there a way to initially position all the slides in rows and columns when copying them into the publisher document?
This is what I have so far.
Sub PPTSlidesToPublisher()
'Copy/Paste Power Point slides Into an existing Publisher document
'Copy Range from PPT
Set rng = ActivePresentation.Slides.Range
Dim appPub As New Publisher.Application
appPub.Open FileName:="filename.pub", _
ReadOnly:=False, AddToRecentFiles:=False, _
SaveChanges:=pbPromptToSaveChanges
appPub.ActiveWindow.Visible = True
Dim myDocument As Object
Dim myPage As Object
Dim myShape As Object
'Add a page to the Documenet
Dim shpSlide As Shape
Set rng = OLEObject
Set shpSlide = ActiveDocument.Pages(3).Shapes.AddOLEObject _
(Left:=2, Top:=5, ClassName:="Publisher.Application")
shpSlide.OLEFormat.Activate
End Sub
I've created a macro in PowerPoint that opens up an Excel workbook, loops through the sheets in the workbook, creates PowerPoint charts and populates them with the data from the Excel sheets. Just to make it clear, the macro is run from PowerPoint.
I now have the need to make the data ranges(when transferring from Excel to Powerpoint worksheets) and PowerPoint chart data ranges dynamic. E.g. because each Excel worksheet range is not the same and therefore each PowerPoint chart data range is not the same.
Below is my macro:
Sub CreateChartAllWKsv3()
'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
Dim CurSlide As Slide 'new from update
Dim LastRow As Long ' 8/22
Dim LastColumn As Long ' 8/22
' 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:\ExcelWorkbook.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 xlWB.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
' 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("Sheet1")
'Clear contents from PowerPoint worksheet
pptWorkSheet.UsedRange.ClearContents 'Works
'Find Last Row and Column of xlWS
LastRow = xlWS.Cells(1, 1).SpecialCells(xlCellTypeLastCell).Row
LastColumn = xlWS.Cells(1, 1).SpecialCells(xlCellTypeLastCell).Column
' Add the data to the PowerPoint workbook.
xlWS.Range(Cells(1, 1), xlWS.Cells(LastRow, LastColumn)).Copy 'Fails to past any data on the second worksheet
pptWorkSheet.Range("A1").PasteSpecial Paste:=xlPasteValues
' Update PowerPoint workbook chart data reference.
'line below didn't work
pptWorkSheet.ListObjects("Table1").Resize pptWorkSheet.Range("Table1[#All]").Resize(Rows.Count, Columns.Count)
' 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
xlWB.Close
'Option to close the excel application
End Sub
I'm running into 2 issues:
xlWS.Range(Cells(1, 1), xlWS.Cells(LastRow, LastColumn)).Copy and pptWorkSheet.Range("A1").PasteSpecial Paste:=xlPasteValues transfer data to the first PowerPoint worksheet, but fail on the second - nothing is pasted.
pptWorkSheet.ListObjects("Table1").Resize pptWorkSheet.Range("Table1[#All]").Resize(Rows.Count, Columns.Count) fails to resize to PowerPoint chart range on the PowerPoint worksheet. I get a method failed error.
EDIT
My workaround for the first issue is to just transfer a large range that my data will never be larger than using pptWorkSheet.Range("a1:z100").Value = xlWS.Range("a1:z100").Value.
Sorry but I do not have enough reputation to add a comment.
Is there a reason that it cannot use linked data instead?
I currently have powerpoint shows that use excel data and just have it linked to pull through the most recent information when I select update links on open.
Thanks
I want to create a "driver" workbook where someone can update values that will be applied to another workbook. The contents of cell B8 of "macros.xlsm" contains the text string I want to use for the author of "report1.xlsx". I have written the following macro but keep getting a
"Object doesn't support this property or method" error on the last line.
Sub add_properties()
Dim xL As Excel.Application
Set xL = New Excel.Application
Dim mainWB As Excel.Workbook
Dim reportWB As Excel.Workbook
Set mainWB = xL.Workbooks.Open("C:\Users\ga1085\adHoc\macros.xlsm")
Set reportWB = xL.Workbooks.Open("C:\Users\ga1085\adHoc\report1.xlsx")
MsgBox mainWB.Sheets("adHoc").Range("B8").Value
mainWB.Sheets("adHoc").Range("b8").Copy
reportWB.BuiltinDocumentProperties("author").PasteSpecial (xlPasteValues)
End Sub
I am also using "macros.xlsm" to update margins, headers, etc for "report1.xlsx" - will this work on those too?
Try opening your workbooks this way. You'll need to add the Microsoft Excel Object Library to your references.
Dim xL As excel.Application
Set xL = New excel.Application
Dim mainWB As excel.Workbook
Dim reportWB as excel.Workbook
Set mainWB = xL.Workbooks.Open("macros.xlsm")
Set reportWB = xL.Workbooks.Open("report1.xlsx")
I figured out how to get it to work. Macros.xlsm is already open and contains the macro that I want to execute. I didn't have to use PasteSpecial. I think the main difference is that I had to declare a variable "author" then use the variable "author" to update the report workbook. I am new to VBA - and would appreciate any other input or explanations. Thank you for your help.
Sub add_properties()
Dim author
Dim reportWB As Excel.Workbook
Set reportWB = Workbooks.Open("C:\Users\ga1085\adHoc\report1.xlsx")
author = Workbooks("macros.xlsm").Sheets("adHoc").Range("b8").Value
reportWB.BuiltinDocumentProperties("author").Value = author
End Sub
I've been having trouble taking cell values from excel and using them in a word macro (I'm trying to insert string values from sheet cells at various bookmarks in a word doc). Right now I'm just trying to be able to access cell values, but I'm coming up with an error 429 (ActiveX component can't create object). Any help/advice on how to approach pulling values from excel and using them in word would be appreciated.
Dim objExcel As New Excel.Application
Dim exWbs As Excel.Workbooks
Dim exWb As Excel.Workbook
Dim strWbName As String
Dim cisInfo As CIS
Sub PopulateDoc()
Set exWb = New Excel.Workbook
exWb = exWbs.Open("CIS.xlsx")
exWb.Sheets("Property Information").Cells(2, 8).Value = "test"
End Sub
You cannot create a New instance of a WorkBook instead:
Set exWb = objExcel.Workbooks.Open("CIS.xlsx")
(You also need to have added a reference to the Microsoft Excel XX Object Library)