Resetting Chart Axis fails - excel

Sorry for such a simple question, I can't figure this out. I have searched several forums for ideas but none of them solved my issue.
All I want is to have the graph axis reset upon the end of some code.
Sheet1.ChartObjects("Chart 13").Chart.Axes(xlValue).MinimumScaleIsAuto = True
This is what I used along with several variations and I get the same error.
Method 'MinimumScaleIsAuto' of object 'Axis' failed.
OR Subscript out of Range
I can assure you that it is Chart 13, and on Sheet1.
Interestingly, if I record a macro doing what I want, then run the macro, I get the same error...

Related

Runtime Error 1004: Unable to get the PivotTables property of the worksheet class. My recorded macro doesnt run?

This is driving my nut, i spent the last hour trying to find a fix to these with no success.
I have a few PivotCharts in a worksheet and i need to refresh them manually.
So i tried to record a macro doing a left click and a refresh, here is the result of the macro :
ActiveChart.PlotArea.Select
ActiveChart.ChartArea.Select
ActiveSheet.PivotTables("PivotChartTable5").PivotCache.refresh
ok, when i tried to run the the macro, i have the following error :
And if i get rid of the first 2 lines of the macro, select one chart and run the macro (the last line only) i have the following error :
I need help.
I tried to replace the activeSheet with the current sheet (Sheets.("mysheet")), doesnt help :(
One thing, if i click on the chart, i dont find the PivotChartTable5 name anywhere, my charts are called something like Chart 12 or whatever the number.
Thanks

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)

"On Error GoTo" ignored when searching for second horizontal axis

NOTE: the code below actually works! See my own answer on the cause of the issue I was having. I decided to leave the code here since I have seen other questions regarding how to delete second horizontal axis.
THE INITIAL QUESTION/ISSUE: I'm formating a series of spreadsheets that have multiple charts on them. When applying the chosen chart style (I'm using style 209 - with a code line "ActiveChart.ChartStyle = 209), some charts come back with two horizontal axis, which I don't want (some charts have two vertical axis - but that is OK). Instead of going chart by chart and manually deleting each secondary horizontal axis, I included a piece of code on my formating subroutine. The code works most of the time. But sometimes I get the error below:
VB error image
The impression I have is that the "On Error GoTo" is being "ignored". I.e. the error above happens when I get to "ActiveChart.SetElement..." line, as the chart has no secondary axis. This means that the line where I asked for the secondary axis' maximum scale (i.e., "chart_axis = ...") must have returned an error (as the secondary axis doesn't exist). Which means that the "On Error GoTo" was ignored (or didn't jump to "No_second_axis").
Here is the part of the code I'm using that deletes the second axis (if it exists):
Dim s_name As String
Dim chart_axis As Variant
For Each Shape In ActiveSheet.Shapes
s_name = Shape.Name
If Shape.Type = msoChart Then
ActiveSheet.ChartObjects(s_name).Activate
On Error GoTo No_second_axis
'Try to get the "scale" of secondary horizontal axis - error if non-existent
chart_axis = ActiveChart.Axes(xlValue, xlSecondary).MaximumScale
'If it reached this line, there was no error when getting the second horizontal axis scale (i.e., the second horizontal axis exists. No I can delete it!
With ActiveChart
ActiveChart.SetElement (msoElementSecondaryCategoryAxisNone)
End With
No_second_axis:
End If
Next Shape
End Sub
Am I missing something? Why does the "On Error Go To" appears to work sometimes but doesn't work for some charts? I can't find a "pattern" in terms of which type of chart it works with vs. a type of chart it doesn't work with.
After an extra couple of hours struggling with this issue, I decided to inspect each chart before running the sub. Using "Go To" on Excel (shortcut = F5) and selecting "objects", I could cycle through each object (by pressing TAB). What I realized is that I had some "hidden" charts (i.e., very old charts that were somehow minimized/collapsed into a obscure place in the spreadsheet). These charts had no "series" on them. For some reason, the "chart_axis = ..." part of the code above can read a secondary horizontal axis scale, but the axis really doesn't exist (as I get an error when trying to delete such axis). Maybe this is a bug on Excel or there is a logical explanation for it - but this is beyond what I need for now. I decided to answer my own question in case this example helps someone else.
The error you are getting is a generic Office error that suggests something went wrong under the hood.
To try and avoid this error I would try the following:
Use Application.ScreenUpdating=false in the beginning of the function (and then Application.ScreenUpdating=true in the end). This helped me avoid rendering problems in the when updating a large number of objects/shapes in an Office document.
OR
Use Application.ScreenUpdating=false in the beginning of every iteration of your Shape object loop and then use Application.ScreenUpdating=true after updating it.
Hopefully you will be able to spot which of the charts creates a problem and under which circumstances (or state).

subscript out of range error when using sheets function

I got a "Subscript out of range error" while running a code that I wrote.
The faulty code line was:
value = Sheets("name_of_sheet").Range("C2").value
The strange thing for me is that this exact code worked daily for a year now without any problem.
Changing "Sheets" to "Worksheets" solved the problem, so I am sure that the sheet name is OK.
Can someone explain this please?
Thanks.

Cannot delete a chart series using VBA in Excel 2010

I have a VBA macro written in Excel 2003 which has worked perfectly well in Excel 2003 and 2007. However, it falls over in Excel 2010 and I cannot tell why.
The macro updates the series in a chart after the user has added or removed data. The first step (after data validation) is to delete all but the first series in the chart.
'clear all chart series except for series 1
Do While theChart.SeriesCollection.Count > 1
theChart.SeriesCollection(2).Delete
Loop
Execution halts in the very first iteration of the loop, with the error dialog "Method 'delete' of object 'series' failed."
I thought perhaps the object model had changed in Excel 2010, so I used the macro recorder to record the action of deleting a series:
ActiveSheet.ChartObjects("Plant Feed").Activate
ActiveChart.SeriesCollection(3).Select
Selection.Delete
Running the recorded macro (with Series 3 re-instated obviously) stops on the second line with "Method 'select' of object 'series' failed."
I then added an object variable and some msgbox lines to try to debug the problem:
Dim theSeries As Series
ActiveSheet.ChartObjects("Plant Feed").Activate
MsgBox (ActiveChart.SeriesCollection(3).Name)
Set theSeries = ActiveChart.SeriesCollection(3)
MsgBox (theSeries.Name)
theSeries.Delete
The object variable sets correctly and the message boxes give the right output, but it still falls over on theSeries.delete.
Finally, I repeated all of the above using a brand new chart created in Excel 2010, just in case it was a legacy problem from 2003, but I got the same error.
I am tearing my hair out. I've searched on line for solutions to no avail, which is what led me to this Stack Overflow site. I would appreciate any help that anyone can offer.
Regards,
Darren
Have you tried it this way instead?
For x = theChart.SeriesCollection.Count To 2 Step -1
theChart.SeriesCollection(x).Delete
Next x
Delete items from the beginning of the collection, there will always be a first element, until you deleted them all.
Do While theChart.SeriesCollection.Count > 1
theChart.SeriesCollection(1).Delete
'theChart.SeriesCollection(2).Delete
Loop

Resources