Dynamic source data for powerpoint chart - excel

The data on the chart on the powerpoint slide is dependent on the data encased by the blue lines. The data is on a worksheet that pops out when i click edit data after right clicking on the chart.
I am trying to write a vba code to set the source of the data to include all of the data but to no avail as of now. My code is as follows:
Melon.Chart.SetSourceData _
Source:=Melon.Chart.ChartData.Workbook.Sheets(1).Range("B3:C" & (28 + Weekno))
Melon is the name of the chart on the powerpoint slide. Weekno actually stands for the week number of the 2017 which is a variable depending on the current week. The above code keeps returning a Run-time error '13': Type mismatch error and does not set the source data to the intended range.
Anyone has any idea? All help will be appreciated! Thank you!

With Melon.Chart.ChartData
.Activate
.Workbook.Sheets(1).ListObjects("Table1").Resize Range("$A$1:$C$29")
End With
This Works!!!! Im so happy

Related

Powerpoint Chart Data Update From Excel Instance - Data disappears when Edit Data

I am editing a PPT template with data from Excel using the following code:
Set shapeObject = oPPT.ActivePresentation.Slides(slideIndex).Shapes("Chart 9")
With shapeObject.Chart.ChartData
.Workbook.Worksheets(1).Range("B2").Value = Sheets(slideSheetName).Range("D3").Text
.Workbook.Worksheets(1).Range("B3").Value = Sheets(slideSheetName).Range("D4").Text
.Workbook.Worksheets(1).Range("B4").Value = Sheets(slideSheetName).Range("D5").Text
.Workbook.Close
End With
When I click to Edit Data on the chart in PPT I see the data as it was copied over from the above code. But some users see no data in the Edit Data sheet but the chart itself still has data in it.
Has anyone ever seen that before? What is the fix?
.Workbook.Worksheets(1).
This reference doesn't seem sound.
I'd try something like this:
Workbooks('NameofWB').Sheets('Sheet1')
So the code is explicit as to what workbook is referenced.

PasteSpecial Excel Table into Slide Placeholder PowerPoint VBA

I'm sorry to be posting another PasteSpecial-question, but I haven't found something that relates precisely to what I'm trying to do.
I have some VBA code:
For Each Workbook In Workbooks
For Each Sheet In Workbook.Sheets
running_pp_app.ActivePresentation.Slides.Add running_pp_app.ActivePresentation.Slides.Count + 1, ppLayoutTitleOnly
Sheet.Range("a1:b2").Copy
running_pp_app.ActivePresentation.Slides(running_pp_app.ActivePresentation.Slides.Count).Shapes.PasteSpecial DataType:=ppPasteDefault, link:=msoCTrue
Next Sheet
Next Workbook
When I paste the Excel-range into a slide, I ultimately want to have the pasted range/shape occupying a placeholder in the slide-layout. I don't want it to be just some additional shape on the slide.
Ultimately, my goal is to be able to easily control all of the pasted ranges/shapes via master-layouts (via the UI, after my VBA runs). I don't know how to paste the linked OLE object into the slide so that it occupies a placeholder-position--so that it is the "content" in the "Title and Content" master-layout, for example.
How can I do this?
All help is appreciated. Thanks.
My first thought was that this doesn't appear to be possible. This works to paste into a placeholder:
Sub PasteIntoPlaceholder()
ActivePresentation.Slides(1).Shapes(2).Select
ActiveWindow.View.PasteSpecial DataType:=ppPasteDefault
End Sub
But as soon as you add the link parameter, it pastes as a separate shape, not in the placeholder.
This mostly corresponds to analogous actions in the user interface. A straight Paste onto a slide with a selected placeholder will insert the chart as expected, but a Paste Special will not.
Revision:
Following your suggestion, this mostly does the job. The placeholder shrinks to fit the pasted Excel object instead of the Excel object expanding to fit the placeholder, but hey, it works:
Sub PasteIntoPlaceholder()
With ActiveWindow
.View.PasteSpecial DataType:=ppPasteDefault, Link:=msoCTrue
.Selection.Cut
ActivePresentation.Slides(1).Shapes(2).Select
.View.Paste
End With
End Sub

Error while creating error bars in Excel with VBA

I want to insert error bars into a clustered column chart in excel using macros. Whenever I do, an error is thrown.
I have a much larger program that takes and sorts data into a couple column charts, and I want to add error bars to them. In an attempt to troubleshoot, I stripped the program way back to just the part that adds the error bars in a separate sheet (code below). I made a simple column chart, selected it and tried to run the program, but it threw an Application Defined error.
Then I tried recording a macro, and that also failed (with a "Method SetElement of Object _Chart Failed" error).
I've Googled around a bit, and found next to nothing (only examples of how to use my first method, close to exactly the same as mine), but also I am relatively new to VBA and might just not know the correct words to google.
Here is my code:
'The code that I had written
Sub AddErrorBars()
'Adds error bars (Standard deviation) to selected chart
ActiveChart.SeriesCollection(1).ErrorBar Direction:=xlY, _
Type:=xlErrorBarTypeStDev, Include:=xlBoth
End Sub
'The code generated by recoring a macro
Sub AddErrorBarsV2()
'Adds error bars (Standard deviation) to selected chart
ActiveChart.SetElement (msoElementLineHiLoLine)
End Sub
One odd thing, the Include enum in first sub (xlBoth) was listed as xlErrorBarIncludeBoth in the MO documentation website, but the listed value is 1, which is the value of xlBoth according to the debugger, xlErrorBarIncludeBoth is empty. Using xlNone doesn't spit out an error, but also doesn't generate error bars. Ive also tried changing the Direction, but that doesnt seem to help.
Is there something I am doing wrong, or perhaps is it the way my data is formatted? Does anyone else seem to have the same issue?
If you have the chart selected, then ActiveChart.SetElement (msoElementErrorBarStandardError) will work fine. However, you should call the chart by name, like so:
ThisWorkbook.Worksheets("Sheet1").ChartObjects("Chart 1").Chart.SetElement (msoElementErrorBarStandardError)

VBA Paste Special on Existing Chart

I would like to use VBA to paste a range of data into an existing chart, using the options shown in the following screenshot:
If I record a macro whilst doing so manually, the code only states ActiveChart.Paste. Thus, when I re-run this code the series is pasted regularly without the 'Series Name In First Row' deactivated. How can I code this correctly? I haven't found much in the way of help in my research so far.
Well, I tried a simple code and it works.
Suppose you have a data like below and you made a simple Clustered Column Chart:
Now you want to paste special Data2 as in your screen shot.
VBA code which works at my end is below:
Sub test()
Dim ch As Chart: Set ch = Sheet1.ChartObjects(1).Chart
Range("A1:A4,C1:C4").Copy '~~> you need to include the x axis labels when copying
ch.SeriesCollection.Paste RowCol:=xlColumns, SeriesLabels:=False, _
CategoryLabels:=True, Replace:=False, NewSeries:=True
End Sub
Result:
I don't know what chart you're working on or how you want it constructed.
Above just shows how to execute Copy and Paste Special of data from a Range to an Existing Chart. HTH

Excel VBA - pasted object into Powerpoint loses selection for manipulation

Just to let you all know, I'm a beginner to VBA and I'm using Excel and PowerPoint 2013.
The aim: I'm writing code in Excel VBA to paste a chart from Excel into PowerPoint. Then when it's pasted, move it to a set place within the slide.
The code I used: The framework of the code I started with is in http://peltiertech.com/Excel/XL_PPT.html#chartppt, the part called 'Paste the Active Excel Chart into the Active PowerPoint Slide (Early Binding)'.
The main difference is around the 'Paste' part, this is instead a 'PasteSpecial' and my code is as follows:
.
Charty.CopyPicture
ppApp.ActivePresentation.Slides(SlideNumber).Shapes.PasteSpecial (ppPasteEnhancedMetafile)
' Setting the correct position on the slide
ppApp.ActiveWindow.Selection.ShapeRange.Top = ChartTop
ppApp.ActiveWindow.Selection.ShapeRange.Left = ChartLeft
(Note. ChartTop and ChartLeft have been defined earlier)
The issues: I'm aware that after the PasteSpecial it doesn't have a select statement. However, (1) When I run the above code I get a runtime error '-2147188160' saying the Selection.ShapeRange is an invalid request (in the 'ChartTop' line), ie. it isn't selected so it can't be moved. (2) When I add a select statement to the 'Paste.Special' line I get an 'Object required' error on the new 'PasteSpecial' line, ie. it doesn't recognise it as a paste select statement.
The odd part: When I run the same macro on other people's computers using the exact same Excel and PowerPoint files it works (without the select statement in the 'PasteSpecial' line). I've looked at 'Tools' => 'References', and both of our computer have the same settings.
The questions: Is there a setting that I have overlooked on my computer which prevents the code from working? Is there a way after I've pasted it to reselect it before it is moved (I should tell you that the macro is to paste in many graphs into ppt simultaneously)? How come for other people at my work it works on their computers without the 'select' part of the PasteSpecial statement? How come when I use the Shapes.PasteSpecial (ppPasteEnhancedMetafile).Select statement, it doesn't recognise it as an object?
I hope that's clear.
Thanks in advance.
Dim oSh as Shape
Set oSh = ppApp.ActivePresentation.Slides(SlideNumber).Shapes.PasteSpecial (ppPasteEnhancedMetafile)(1)
Now you don't need to select anything (which is best avoided whenever possible). Just manipulate the pasted shape, oSh, directly.
The curious syntax (the (1) at the end) is because Shapes.Paste or .PasteSpecial returns a shaperange, not a shape; the (1) causes it to assign the first shape in the range to the oSh variable. Since there's only one shape in the range, that suits us just fine.

Resources