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.
Related
Please read this fully and understand that this program was working fine until I changed the way I was hiding the workbook.
I have a program that worked great while I was using Application.Visible = False and only showing the user form. I came to realize that this would hide all Excel windows and not just the one I was using. This is going to be distributed throughout the department and hiding all Excel windows was unacceptable.
I started using ActiveWindow.Visible = False, but I am now getting Error 91 anytime I search a worksheet for a value (Cells.Find).
Modifying the worksheet is not an option and the value for which I'm searching can move around the sheet depending on what has been added or removed.
Cells.Find worked out great for this reason. I need to either find another way to search the page, or find another way to hide the worksheet. Please help
When the window is not visible, the Cells reference is not qualified to a worksheet object (unless qualified, Cells refers to ActiveSheet.Cells and there is no ActiveSheet), so you can do like:
Sheets("sheetname").Cells.Find ' modifying "sheetname" as needed
This may also fail (with the same error), or it could also yield incorrect results if there are other open workbooks, so it's best to qualify to a workbook fully, e.g.:
Workbooks("workbookname").Sheets("sheetname").Cells.Find(...
It is still a good idea to test the result of Find before performing additional method/property calls against an object which could be Nothing, as per this answer:
Find command giving error: "Run-time Error '91': Object variable or With block variable not set"
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 have made multiple excel files trying to solve this problem. I have gotten to this point and am still getting an error for this function:
Sub graph1()
ActiveChart.Name = IChart
MsgBox "done"
End Sub
This is in a macro enabled excel file with numerical-only data on Sheet 1 in the range A1 to F754 and a (manually made) Smooth Scatter Graph on Sheet 2.
Trying to run this code (with the chart selected to make ActiveChart effective) I am getting the error:
Run Time Error '7':Out of Memory
Please help, I need to be able to name the chart so I can make .Axis formatting changes, re-size the chart, and some other things with a VBA macro.
How about assigning the variables something like the below, and making the changes as described:
Function test()
Dim ic As Chart
' Somehow assign the chart to a variable
Set ic = ActiveChart
' Change the name as desired
ic.Parent.Name = IChart
' Change the axes as desired
ic.Axes(xlCategory).CrossesAt = -350
End Function
Don't mark this as answer since Demetri answered it correctly in the comments.
He is correct that you should use:
ActiveChart.Parent.Name = "ChartName"
Or you can use a variable that contains your name.
Why use Parent property? The reason is ActiveChart points to Chart property of the ChartObject and you can only set the Name property on ChartObject.
So basically,
Dim Co As ChartObject
Set Co = ActiveChart.Parent
Debug.Print Co.Name 'retrieve chart object name
Co.Name = "Chart Name" 'assign name
I hope this clear things out.
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"
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"...