+ Reply to Thread
Results 1 to 13 of 13

Secondary y-axis to auto scale not working: throwing Method 'Axes' of object '_Chart' fail

  1. #1
    Registered User
    Join Date
    01-18-2019
    Location
    Zurich
    MS-Off Ver
    2013
    Posts
    8

    Secondary y-axis to auto scale not working: throwing Method 'Axes' of object '_Chart' fail

    Hi,
    I have a about 12 charts in my worksheet. About 6 charts (Clustered column charts) of them have primary & secondary axis.
    I have a macro which align the primary & secondary axes to zero. So far good.
    The input of my chart changes and I want to be able to reset the axes value to auto and have a macro for that.
    The code works fine for the primary y-axis, but I run into the Method 'Axes' of object '_Chart' failed for the secondary y-axis (highlighted in red and underline in the code below).
    How can this be fixed?

    Any ideas about how this can be fixed would be very much appreciated.

    Please Login or Register  to view this content.

    Thanks
    Last edited by ChennuS; 01-18-2019 at 10:04 AM.

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2507 (Windows 11 Home 24H2 64-bit)
    Posts
    91,861

    Re: Secondary y-axis to auto scale not working: throwing Method 'Axes' of object '_Chart'

    Welcome to the forum!

    Please see our rules regarding code tags, which I have added for you this time.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  3. #3
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,525

    Re: Secondary y-axis to auto scale not working: throwing Method 'Axes' of object '_Chart'

    Your code worked fine for me -- as long as my test chart had a secondary axis system. If my test chart had only a primary axis system, then the code gave me that same error. You indicate that only 6 of your 12 charts have secondary axis systems. I would guess that, as your For Each..Next loop is going through each chart object, the error is triggered when it comes to a chart that has only a primary axis system.

    Debugging suggestions:

    1) Add a Stop statement (or breakpoint) before it enters the loop, so you can enter debug mode.
    2) Add watches or debug.print statements in order to evaluate whether a given ochrtobj has secondary axis system (perhaps ochrtobj.axes.count?)
    (If you are unfamiliar with these debugging tools: http://www.cpearson.com/excel/DebuggingVBA.aspx )

    My guess is that the final fix will be to figure out some way -- based on what you know about all 12 chart objects -- to identify which charts have secondary axis systems and conditionally execute that block of code only when the secondary axis system exists.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  4. #4
    Registered User
    Join Date
    01-18-2019
    Location
    Zurich
    MS-Off Ver
    2013
    Posts
    8

    Re: Secondary y-axis to auto scale not working: throwing Method 'Axes' of object '_Chart'

    MrShorty,
    thank you for the suggestion. You are right not all my charts in the worksheet have secondary access.
    Since the other macro I alluded (the one which was selected & I wanted edited) to modified the "active chart" with secondary axis, I didn't expect the loop to go thorough all the charts in the worksheet.
    I will see if I can do the conditional execute the code to a specific chart!

  5. #5
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,525

    Re: Secondary y-axis to auto scale not working: throwing Method 'Axes' of object '_Chart'

    I didn't expect the loop to go thorough all the charts in the worksheet.
    This is what the For Each...Next loop does. If you don't want the code to loop through all of the charts in the worksheet, then you can drop the For Each...Next loop

  6. #6
    Registered User
    Join Date
    01-18-2019
    Location
    Zurich
    MS-Off Ver
    2013
    Posts
    8

    Re: Secondary y-axis to auto scale not working: throwing Method 'Axes' of object '_Chart'

    Hi,
    I didn't phrase myself precisely.
    The macro where I align primary and secondary axis can be run by clicking a chart and this modifications are done for only the "active chart".
    I wanted the macro to reset the axis also to work similarly i.e. reset the axes values to auto for only the chose chart.

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Secondary y-axis to auto scale not working: throwing Method 'Axes' of object '_Chart'

    As stated, remove the loop and just use the activechart:

    Please Login or Register  to view this content.
    Everyone who confuses correlation and causation ends up dead.

  8. #8
    Registered User
    Join Date
    01-18-2019
    Location
    Zurich
    MS-Off Ver
    2013
    Posts
    8

    Re: Secondary y-axis to auto scale not working: throwing Method 'Axes' of object '_Chart'

    Thanks for the advise. I tried this option and get the error "Object variable or With block not set"

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Secondary y-axis to auto scale not working: throwing Method 'Axes' of object '_Chart'

    On which line? I assume you did select a chart first?

  10. #10
    Registered User
    Join Date
    01-18-2019
    Location
    Zurich
    MS-Off Ver
    2013
    Posts
    8

    Re: Secondary y-axis to auto scale not working: throwing Method 'Axes' of object '_Chart'

    Yes, I did select a chart first:
    It is stopping at the below bold, underlined line
    Please Login or Register  to view this content.

  11. #11
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Secondary y-axis to auto scale not working: throwing Method 'Axes' of object '_Chart'

    How are you running the code? From the macros dialog, a button, or somewhere else?

  12. #12
    Registered User
    Join Date
    01-18-2019
    Location
    Zurich
    MS-Off Ver
    2013
    Posts
    8

    Re: Secondary y-axis to auto scale not working: throwing Method 'Axes' of object '_Chart'

    I had set up a button to launch the right macro.
    Thanks for the hint..... I ran the macro from the macros dialog and code ran through.
    When I run the code via a the button I get the error.
    How come?

  13. #13
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Secondary y-axis to auto scale not working: throwing Method 'Axes' of object '_Chart'

    Are you using an ActiveX button, or a Form button? You want the latter ideally as an activex one takes the focus when you click it by default so the chart is no longer active. If you have a real need to use activex, then set the button's takefocusonclick property to false.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 06-15-2017, 05:34 AM
  2. Create multiple pivot charts [Error: Method 'SetSourceData' of object '_chart' failed]
    By DianCharloValentine in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-26-2014, 07:51 AM
  3. Secondary axis not picking up amended data for axis scale
    By AileenR in forum Excel Charting & Pivots
    Replies: 8
    Last Post: 10-02-2013, 06:17 AM
  4. method 'location' of object '_Chart' failed.
    By Hendel in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 10-29-2008, 01:03 PM
  5. Method 'SeriesCollection' of object '_Chart'failed
    By Dennis Mak in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-08-2006, 02:55 AM
  6. Run-time error '1004'; Method 'Export' of object'_Chart' failed
    By Father Guido in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-25-2005, 10:35 AM
  7. Run-time error 1004: Method 'Axes' of object '_chart' failed
    By Brian Morrison in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-20-2005, 11:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1