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
Related
I would like to ask if it is possible to add a non continuous range as a data source to create a sparkline. Without VBA it can be done by creating a named range, provided that data are on the same worksheet and using that named range as a data source.
My named range is called "SPA". I can even record a macro, but when I run the recorded macro on the correct activesheet I get Application-defined or object-defined error.
This is the code that was generated when recording the macro
Range("$B$2").SparklineGroups.Add Type:=xlSparkColumn, SourceData:="SPA"
Here is another code, which I actually use in my macro, where all the ranges are fully qualified
SM.Range("A2").SparklineGroups.Add Type:=xlSparkColumn, SourceData:=SM.Range("SPA")
In both cases I get Application-defined or object-defined error.
I have a custom to rename Sheet1, Sheet2, etc. codenames to own abbreviations, so that I do not need to qualify sheets in every macro.
So I use SM.Range("A2") instead of Dim SM as Worksheet: Set SM = ThisWorkbook.Sheets("SM") so the error does not relate to the ranges not being fully qualified.
When I use add sparkline manually without VBA and I use the named range, the non continuous data source range works.
Thank you very much for any hints.
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
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
This is a simple request. I am trying to clear the contents of a worksheet before copying over a range from another sheet. There are two things I want to make sure of:
1. The sheet I am copying into (Sheet1) has named ranges defined that make absolute references to cells in this sheet. I want to make sure that these definitions are not affected in any way. The contents will, of course, change when I clear the sheet and copy over data from the other sheet.
2. There are some pasted images in Sheet1. These also need to be cleared out.
I tried the following:
Sheets("Sheet1").Cells.Clear
This seems to ensure point #1 above. Seriously, I don't think there is any danger of this being a problem, but just wanted to mention it. However, the images do not go away. Perhaps this is because I use .Cells.Clear? I can't delete the sheet. How should I do this?
This should do it:
Sub KleanUp()
Sheets("Sheet1").Cells.Clear
Dim sh As Shape
For Each sh In Sheets("Sheet1").Shapes
sh.Delete
Next sh
End Sub
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