Charts("Vendor").ChartTitle.Text = "Test"
Is throwing subscript error. The chart does exist and is named Vendor.
Any ideas?
Chart is a child object of a ChartObject (which like #DerekCheng alluded to, are contained within a worksheet); therefore, you'll need to get the Chart directly from there. Try this instead.
Worksheets("YourSheetName").ChartObjects("Vendor").Chart.ChartTitle.Text = "Test"
Related
Hi i'm facing a subscript out of range error when I tried to copy a chart from excel sheet to word document. I checked the worksheet name it seems to be fine. I tried changing to ThisWorkbook.Sheets(wsname) but i received an application define type error. How do I go about resolving this issue.
With ThisWorkbook.Worksheets(wsName)
.ChartObjects(1).Activate
ActiveChart.ChartArea.Copy
End With
The name of the worksheet I'm trying to reference
I have finally figured out what's the problem.
Since I'm referencing a chartsheet and not a worksheet, I have to use .Charts instead of .Worksheets. In addition since it is a chartsheet, the chartsheet itself is a chart object, hence .ChartObjects(1) is not required, only .ChartArea.Copy
Correct Code:
With ThisWorkbook.Charts(wsName)
.ChartArea.Copy
End With
How can I select data range for a chart from different sheet using VBA? Suppose that data sheet name is data_sheet, chart sheet name is chart_sheet, and my data range is A1:A20. How can I do this in excel? I checked THIS but didn't work for different sheets. Otherwise, I checked THIS but returned this error: Subscript out of range:
With Worksheets("chart_sheet")
ActiveChart.SetSourceData Source:=Worksheets("data_sheet").Range("A1:A20")
End With
Assuming "chart_sheet" is the name of your Chart and "data_sheet" is the name of your Worksheet, I think you want to do the following:
Charts("chart_sheet").SetSourceData Source:=Worksheets("data_sheet").Range("A1:A20")
Your With block was not doing anything useful - the purpose of a With block is to allow you to just type . as a shortcut for something like Worksheets("data_sheet")..
So something like:
With Sheets("chart_sheet")
.SetSourceData Source:=Worksheets("data_sheet").Range("A1:A20")
End With
would work, because the .SetSourceData is an abbreviation of Sheets("chart_sheet").SetSourceData. (Notice also that the Sheets collection contains both Worksheets and Charts objects, so Charts("chart_sheet") and Sheets("chart_sheet") both point to the same thing.)
ActiveChart refers to the currently active chart, just as ActiveSheet returns to the currently sheet. If you don't have a chart active when that piece of code executes, you will get an error.
So the following piece of code would also probably have worked for you:
Sheets("chart_sheet").Activate
ActiveChart.SetSourceData Source:=Worksheets("data_sheet").Range("A1:A20")
as chart_sheet is probably not a worksheet, did you try this ?
with sheets("chart_sheet")
I want to manipulate an existing chart to filter data series and change title by clicking command buttons on a worksheet, but I'm getting runtime error 438 when trying to write code for some operations.
First, a line
Worksheets("Report").ChartObjects("ChartVisitors").Visible=True (or False)
works just fine, but when I try:
Worksheets("Report").ChartObjects("ChartVisitors").ChartTitle.Text = "Test" or
Worksheets("Report").ChartObjects("ChartVisitors").FullSeriesCollection(25).IsFiltered = True (False), I get runtime error 438. Those two are pretty much the only properties I want to modify, I'm guessing my syntax is wrong? MSDN only specifies that the chart should have .HasTitle property set to True, which it does.
A ChartObject functions as a container for a Chart object. Think of it as the window that the chart is embedded in. You need:
Worksheets("Report").ChartObjects("ChartVisitors").Chart.ChartTitle.Text = "Test"
Rather than
Worksheets("Report").ChartObjects("ChartVisitors").ChartTitle.Text = "Test"
etc.
I have a chart in Excel ("Chart 13"), I also have a named range called "nationals".
I'd like to replace one of the chart's series' Y Values with the named range (the chart has three different series, I'm planning to repeat a similar process for all three).
This is what I have been trying with no success:
ActiveWorkbook.Sheets("My_Sheet").ChartObjects("Chart 13").Activate
ActiveChart.SeriesCollection(1).Values = Range("nationals")
I get the following error:
"Run-time error '91': Object variable or With block variable not set"
I feel like I'm close but can't get to the finish line for the life of me! I appreciate any help.
EDIT
This is a line chart (with markers) - sorry that wasn't clear before. Simple version of same situation:
You need to add object to the range like so:
Dim MyChart
Set MyChart = ActiveWorkbook.Sheets("My_Sheet").ChartObjects("Chart 13").Chart
MyChart.SeriesCollection(1).Values = ActiveWorkbook.Sheets("My_Sheet").Range("nationals")
Also, as you note, the ChartObjects().Chart object has the SeriesCollection.
I recorded the following macro :
Sheets("Rejets Techniques TGC").Select
ActiveSheet.ChartObjects("Graphique 1").Activate
ActiveChart.Axes(xlCategory).Select
ActiveChart.SeriesCollection(1).Values = "='Données'!$EU$68:$IJ$68"
ActiveChart.SeriesCollection(1).XValues = "='Données'!$EU$1:$IJ$1"
However when I try to lauch it I get this error (translated from french):
Execution error '-2147024809 (80070057)'
There is no element with this name
How can this be? if there was no graph named this way I wouldn't have been able
to record it.
(yes I'm running it from the good sheet)
Thanks.
Here's what it comes down to: Your chart is not an object on the sheet, it is the sheet.
So while you use ActiveSheet.ChartObjects("Graphique 1").Activate to start your code, there are no ChartObjects found in your sheet, because the sheet is the Chart. So here's how you get around it:
Dim CO As Variant
Set CO = ActiveSheet
CO.Axes(xlCategory).Select
CO.SeriesCollection(1).Values = "='Données'!$ET$68:$IJ$68"
CO.SeriesCollection(1).XValues = "='Données'!$ET$1:$IJ$1"
And this should work just fine. I noticed that when I looked at the chart tab, I couldn't get into any cells. This is not abnormal, but it is not the most common way (that I see) to create the chart. To verify, I added a watch on the ActiveSheet and saw that it was indeed a chart (of type Object/Graph2) with all the normal chart methods available to it.
From there, I just plugged in your code, converting to the CO variable (but yours should still work using ActiveSheet across the board), and ran with no errors.
As a side note, using ActiveSheet is not always effective, and it is generally better to explicitly call the sheet, i.e. Set CO = ThisWorkbook.Sheets("Rejets Techniques TGC")
1 - Check if the active sheet is the one that contaisn the chart. Or use the sheet name in code to run it from any sheet.
2 - Check if the good sheet contains the chart with exact "Graphique 1" name. Maybe there's an underline, like "Graphique_1", or no space "Graphique1"...