I have multiple slides with multiple charts having sample data. I want to update these charts with actual data which is present in excel files located in the same folder. Can I do this via VBA inside ppt and how?
I read this answer edit chart data in powerpoint but this does not specify where I can mention the excel file and select the specific data.
Please help!
On the question you linked to, the top answer provided the following code:
With ActivePresentation.Slides(sl).Shapes(sh).Chart.ChartData
.Activate
.Workbook.Sheets(1).Range("A1").Value = "test_data"
.Workbook.Close
End With
One of the comments on the answer pointed out that once you Activate the ChartData, you're basically dealing with Excel. You can also launch an actual instance of Excel inside the Powerpoint VBA, then just open your workbooks, extract the data from them and put it into your ChartData objects like you would do if you were working in VBA in excel.
In order to launch an instance of Excel in powerpoint VBA try the following:
Dim xlApp As Excel.Application
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.Workbooks.Open "C:\lol\Book1.xlsx", True, False
'once Book1 is open you can extract data from Book1 and move it to
'the appropriate place in .ChartData.Workbook
Set xlApp = Nothing
(code above sourced from this answer)
Related
I want to print Excel sheet (Single sheet) which is stored in a path of local computer using a click of button on MS Access form. For some business reasons I need to provide flexibility to show the print setup dialog box which we normally get if we were printing Excel sheet from outside, so that user can select the printer, select what part of sheet to print, copies to print etc. I currently have code as shown below:-
Private Sub cmdPrint_Click()
'wks is already set to Excel sheet which needs to be printed
wks.PrintOut
End Sub
I am trying to also use something like as shown below , but not sure how to connect the Printout method with xlDialogPrint
ExcelApp.Dialogs(xlDialogPrint).Show
I am struggling with what code would do that in MS Access VBA.
The follow code will open Excel then open print interface to allow user to make choices for printing whatever sheet has focus.
Sub PrintSheet()
Dim xl As Excel.Application, wb As Excel.Workbook
Set xl = CreateObject("Excel.Application")
Set wb = xl.Workbooks.Open("C:\filepath\filename.xlsx")
xl.Visible = True
xl.CommandBars.ExecuteMso ("PrintPreviewAndPrint")
End Sub
This may be simplistic to most of you. I just started using VBA to create and update excel workbooks. I found some code on the internet to open and update an existing workbook and worksheet. Like I said, I am brand new at this. Does this code even make sense? I just need to know how to open an existing workbook and all the examples I have found aren't working in our environment.Thanks for any help I can get
Dim wbSource, xlApp, srcWorksheet
'initialize
Set xlApp = CreateObject("Excel.Application")
'open source and target files
Set wbSource = lApp.Workbooks.Open("X:\GCIXCycleCompare_test_auto.xlsx")
set srcWorksheet = wbSource.Worksheets("NewCycle")
srcWorksheet.sheets("NewCycle").Activate
srcWorksheet.Rows("1:1").Delete
If you are in Excel VBA, this isn't quite what you want. This code was written for an external app (say written in VB6) to open Excel remotely and then do stuff to that copy of Excel. If you are already in Excel/VBA you obviously don't need to do that.
In VBA, the equivalent code would be something like this:
Public Sub MyCode()
Dim wb as Workbook
Dim ws as Worksheet
Set wb = Application.Workbooks.Open("X:\GCIXCycleCompare_test_auto.xlsx")
Set ws = wb.Worksheets("NewCycle")
ws.Rows(1).Delete
End Sub
If you run this code (by click F5 from inside VBA ... or by run macro in Excel) it should open up the test file off the X: drive, and then delete the first row of it.
So after a bit of digging I got some help to find a code to export a chart from Excel to Powerpoint, problem is it is only selecting an object.
Sub ertert()
With New PowerPoint.Application
With .Presentations.Add
With .Slides.Add(1, 12)
ActiveSheet.ChartObjects(1).CopyPicture xlPrinter, xlPicture
.Shapes.Paste
.Shapes(1).Select
.Application.ActiveWindow.Selection.ShapeRange.Align msoAlignMiddles, True
.Application.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
End With
End With
End With
End Sub
the object it's selecting is a chart/vlookup image result inside of a larger chart (the object itself is named) so basically it's only getting a small part of the item itself.
Question is where I went wrong in having it only select an item? Also I would like to know how to modify this code to add to an existing Powerpoint (assuming the existing one is already open).
What exactly are you trying to do?
If you are just looking to display a chart that updates based on the content of your excel workbook you can simply copy the chart across and it creates a link between the two. When both your presentation and workbook are open it will update the chart automatically with no need for VBA.
I have added an Excel sheet into a PowerPoint 2010 document using the Insert tab --> Object --> Microsoft Excel 97-2003 Worksheet (Create New) option. I want to reference some of the cells in the Excel sheet in another slide of my PowerPoint. Is there a way to do this?
The purpose is I have a client who insists on a PPT report, except I need to use Excel to create the information required. Rather than constantly having two documents open and transferring the info from the Excel sheet to the PPT slide, I wanted to consolidate into one document, thus the Excel sheet added into the PPT file.
I'm not an expert at VBA by any means, but I know enough to muddle my way through if I need to use VBA to accomplish this.
I've uploaded pictures of an example (I hope). On slide 1, I have three cells filled in using the inserted Excel sheet. Slide 2 is where I need to reference those cells (text boxes with text in red). The information in those cells will change week from week and I need the text boxes in slide 2 to update with it. Any help would be appreciated.
Use the Selection Pane in PowerPoint to identify the embedded object's name, and reference it, then use the OLEFormat.Object to get a handle on the Workbook object, and from there you're just working with an instance of Excel.Workbook class, so all your familiar Excel properties & methods should be available:
Option Explicit
'Requires reference to Excel
Sub populate_text_box()
Dim obj As Object
Dim tb As Shape 'TextBox
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Set obj = ActivePresentation.Slides(1).Shapes("Object 3")
Set wb = obj.OLEFormat.Object
Set ws = wb.Sheets(1)
Set tb = ActivePresentation.Slides(2).Shapes("TextBox 1") 'Modify slide/shape name as needed
tb.TextFrame2.TextRange.Text = ws.Range("B2").Value
Set tb = ActivePresentation.Slides(2).SHapes("TextBox 2") 'Modify slide/shape name as needed
tb.TextFrame2.TextRange.Text = ws.Range("D2").Value
Set tb = ActivePresentation.Slides(2).SHapes("TextBox 3") 'Modify slide/shape name as needed
tb.TextFrame2.TextRange.Text = ws.Range("F2").Value
End Sub
I have a program that filters data and then outputs it to a tabg delimited file. I then use Excel Interop to open this file in Excel because directly outputting to a worksheet takes too long. So currently I am using this code:
AD.DF.WriteToFile(vbTab)
Dim WB As Workbook = ExcelApp.Workbooks.Open(AD.DF.DatafileInfo.WriteToFileLocation)
ExcelApp.Visible = True
The first line takes the filtered data and outputs to a tab delimited file. The second opens that same file in a new workbook in Excel. And obviously the third makes Excel visible. Here is my problem though: right now when this code is run there are two open workbooks. I already have an active workbook and I would just like to open this file to that workbook.
If this is possible, how do I do it?
Thank you.
Look at the GetObject function.
Dim MyXL As Object
MyXL = GetObject(, "Excel.Application")
should get you a reference to the currently running instance of Excel.
In the code I created an object that is an Excel Workbook. I then set the created workbook as the ExcelApp.ActiveWorkbook. Then I was able to open the file without another workbook being created.