+ Reply to Thread
Results 1 to 10 of 10

Adding Axis Labels to a Chart

  1. #1
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Adding Axis Labels to a Chart

    I want to produce the chart shown in the picture below. I am not able to add the caption for the primary axis. As I step through the code, it deletes the old axis. When I step through the Add new axis: this line
    Please Login or Register  to view this content.
    does nothing, so this line
    Please Login or Register  to view this content.
    fails with an "Object Required" error.

    If I comment the line out, everything else works fine.

    Full code
    Please Login or Register  to view this content.
    Attached Images Attached Images
    Attached Files Attached Files
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  2. #2
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Adding Axis Labels to a Chart

    add this line in
    Please Login or Register  to view this content.
    right before
    Please Login or Register  to view this content.
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  3. #3
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: Adding Axis Labels to a Chart

    Thanks for the help. I experimented with that, but had ti in the wrong part of the code.

    Any idea why you have to do this for the primary axis but not the secondary or category axis and why the macro recorder doesn't suggest it? I'll accept, "It's just one of those Excel things." as an answer.

  4. #4
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Adding Axis Labels to a Chart

    for some reason
    ActiveChart.SetElement (msoElementPrimaryValueAxisTitleAdjacentToAxis)
    this does not automatically make it visible (or just doesnt do anything...cant tell)

    or
    ActiveChart.SetElement (msoElementPrimaryValueAxisTitleNone)
    makes
    .Axes(xlValue).HasTitle = false


    macro recorder...often leaves out important tidbits while adding numerous other things
    i would just add this to another thing you cant extract from recorder

  5. #5
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Adding Axis Labels to a Chart

    It looks like a bug with that constant. msoElementPrimaryValueAxisTitleNone and msoElementPrimaryValueAxisTitleAdjacentToAxis actually have the same value (306). You could add
    Please Login or Register  to view this content.
    to the code

    Of course, it shouldn't be necessary to activate the chart for this code.
    Last edited by xlnitwit; 04-25-2018 at 04:28 AM.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  6. #6
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: Adding Axis Labels to a Chart

    I dislike working with active anything - I'll tell Excel where it should be and what it should be doing. However, when it comes to charts, it seems that you have to activate it somewhere in the process or at least set the chart object.

  7. #7
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Adding Axis Labels to a Chart

    Quote Originally Posted by dflak View Post
    or at least set the chart object.
    The same as any other object. Using sh.ChartObjects(ChartName).Chart would be preferable here, in my opinion. There are a few niggles with charts that occasionally require selecting it to work around but none appear to be present in this code.

  8. #8
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: Adding Axis Labels to a Chart

    Xlintwit - I'm going to revise the code based on your comments and then post it as a finished product. Thanks for the tips.

  9. #9
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: Adding Axis Labels to a Chart

    And here it is ...
    Attached Files Attached Files
    Last edited by dflak; 05-03-2018 at 08:26 AM.

  10. #10
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Adding Axis Labels to a Chart

    Invisible ink?

+ 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. Adding Horizontal Axis Labels to Chart without Selecting the chart
    By Jonathan78 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-31-2017, 11:32 AM
  2. [SOLVED] Adding Horizontal Axis Labels to Chart without Selecting the chart
    By Jonathan78 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-26-2017, 04:02 PM
  3. Changing chart type & second axis on chart with 2-level axis labels
    By trolle in forum Excel Charting & Pivots
    Replies: 5
    Last Post: 04-15-2016, 09:52 AM
  4. Replies: 3
    Last Post: 04-30-2014, 05:57 AM
  5. [SOLVED] X-Axis labels disappear when adding data table to chart
    By phacexcel in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 08-22-2012, 02:37 PM
  6. Replies: 2
    Last Post: 05-16-2010, 11:23 PM
  7. [SOLVED] how to put x axis values in the chart instead of x axis labels
    By Sinclair in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 06-21-2005, 06: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