VBA Paste Special on Existing Chart - excel

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

Related

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

How to control excel chart source range through VBA

I want to be able to control and update the source (range) of an excel chart through VBA. The chart is ultimately going to be presented in a powerpoint presentation and the whole process shall update automatically.
I have tried using tables which doesn't give me the level of control I want. For example if i delete one datapoint the chart doesn't "shrink" accordingly.
ActiveChart.SetSourceData (Sheets("Sheet2").Range("E5:E9"))
The current script doesn't run. But I just want to "set" a source range for a particular chart, preferably accessed by name. And if new datapoints are added I just run the script again and the new range will be the top cell in the column down to .End(xlDown)
Thanks guys! I manged to find a solution that works for me and made a function of it, if anyone else has the same problem. Nothing fancy but you can set a range like "A2:A14" and the chart will update with the new range. The function is called by sheet name, chart name and range like below:
Call changecharts("Sheet2", "Chart1", "A2:A11")
Public Function changecharts(sheeet As String,chartname As String, rnge As String)
Worksheets(sheeet).ChartObjects(chartname).Activate
With ActivateChart
ActiveChart.SetSourceData Source:=ActiveWorkbook.Sheets(sheeet).Range(rnge)
End With
End Function

How to disable the user to delete a chart in vba?

i made a chart on my excel sheet, and i want the user not to be able to delete it...
how can i do it?
I already tried this options from here:
https://peltiertech.com/Excel/ChartsHowTo/ChartProtection.html
The code is like this:
'Draw a chart
Dim rng As range
Dim cht As ChartObject
'Your data range for the chart
Set rng = Worksheets("Sheet1").range("$A$2:$A$100")
'Activate chart
Worksheets("Sheet1").ChartObjects(1).Activate
'Populate chart with data
ActiveChart.SetSourceData Source:=rng
'Determine type
ActiveChart.SeriesCollection.NewSeries.ChartType = xlLineMarkers
'Delete the 5th series
If ActiveChart.SeriesCollection.Count = 5 Then
ActiveChart.SeriesCollection(5).Delete
End If
I need something like this:
ActiveChart.preventDeleting
Now, the protection I had in mind was to prevent the user, from actively deleting the chart....a simple select and remove, or button press delete, or something...
In simple terms, block any attempt to delete the chart...
But, i dont know if this is relevant, but the table can change its range, and the chart should be still able to get updated...
Please let me know, if you need something else...
Also, it does not nessesary need to be a vba code, it can also be a manual solution, I am using MS excel 2007
Thanks...
You can protect the chart by protecting the sheet. If you want the user to be able to edit some of the cells, simply right click on the cells that you want to be able to be edited and select Format Cells -> Protection -> Locked and make sure this is unchecked.
Then protect the worksheet via Review -> Protect Sheet on the menu bar. This will prevent the deletion of the chart and allow users to continue editing the cell content

Adding X-Y plots to an existing chart in VBA

I’d like to write visual basic code that adds a “new series” to an existing plot. I’ve already managed to write the code that will select that data I want to add. So to complete the operation I did it in excel, recording the following macro:
‘desired data already selected a column of x values and one of y values to be plotted
Selection.Copy
Sheets("XVSER").Select
ActiveSheet.ChartObjects("Chart 3").Activate
ActiveChart.Paste
However, what this recorded macro does not show is I wanted to add the data as a “new series”, even though this option was explicitly selected while recording the macro. So when I run the macro it does not do the same thing I did will recording it, and adds the data to an existing series instead of creating a new series for it.
Surely there must be a way of specifying in VBA that I’d like a new series. I just don’t know how, or where to find documentation, and what I found already on stackoverflow, was difficult for me to understand, and seemed to require a different approach, which I’d rather avoid, since I’ve already developed the code to successfully select the data I want, and to select the chart I want to paste it in.
When recording MACROS with charts, not allways the result comes out the same as when recorded it. You will need to familiarize yourself with some of the ChartObject properties.
Try the code below:
Option Explicit
Sub AddSelectionasNewSeries()
Dim ChtRng As Range
Dim ChtObj As ChartObject
Dim Ser As Series
' set the selection as range >> However, try avoid using slection
Set ChtRng = Selection
' set the chart object
Set ChtObj = Sheets("XVSER").ChartObjects("Chart 3")
' add a new series with the Selection
ChtObj.Chart.SeriesCollection.Add ChtRng
End Sub
You have to use SeriesCollection.Paste, which has parameters to specify whether to add points or series and so forth. The Macro Recorder doesn't know this.
Replace
ActiveChart.Paste
with
ActiveChart.SeriesCollection.Paste Rowcol:=xlColumns, Serieslabels:=False, _
CategoryLabels:=True, NewSeries:=True

Named range reverts to normal range in chart

I have encountered strange problem. I have made named range SomeName
=OFFSET(Source!$B$29:$C$29,1,0, COUNTIF(Source!$B$30:$B$55, "<>x"),2)
And then I have tried to used in chart as a chart data range:
=Source!SomeName
As far, it worked fine. But when I switched to the sheet Source and went back I have realised that the data range went back to the normal range =Source!$B$30:$C$33. I guess I could fix it with macro (like setting named range to chart everytime when somebody activate the sheet) but there must be some other way.
Anyway know how to make it without VBA?
You cannot set whole chart source data to be named dynamic range. It will always transfer to hardcoded values. what you can do and will work, is to create named range for each series in chart, than add it manually (for more info take a look at this site). this way your chart will be somewhat dynamic. But the best way to get what you want is by using a macro. Something similar to this code...
Sub Test()
Dim CHARTDATA As Range
Set CHARTDATA = Range(or your dynamic range)
ActiveSheet.ChartObjects("Chart 1").Chart.SetSourceData Source:=CHARTDATA
End Sub

Resources