+ Reply to Thread
Results 1 to 12 of 12

Help with setting chart axes and ranges

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Help with setting chart axes and ranges

    Hi,

    I am trying to set the chart axes values for the chart in my sheet. I am using the below but get a run time error 'Object doesn't support this property or method' on the line highlighted below:

    Sub InputScaleAxes()
    With ActiveSheet.Charts("Chart1").Axes(xlCategory, xlPrimary)
    .MinimumScale = ActiveSheet.Range("O40").Value
    .MaximumScale = ActiveSheet.Range("039").Value
    .MajorUnit = ActiveSheet.Range("O38").Value
    End With
    With ActiveSheet.Charts("Chart1").Axes(xlValue, xlPrimary)
    .MinimumScale = ActiveSheet.Range("P40").Value
    .MaximumScale = ActiveSheet.Range("P39").Value
    .MajorUnit = ActiveSheet.Range("P38").Value
    End With
    End Sub
    Can anyone assist me with this problem?

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,480

    Re: Help with setting chart axes and ranges

    The is not a chart collection within the activesheet object.

    There is for a workbook object. These are the chart sheet type.

    If you have chart objects then use the chartobjects collection of the worksheet
    With ActiveSheet.ChartObjects("Chart1").Chart.Axes(xlCategory, xlPrimary)
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: Help with setting chart axes and ranges

    I tried the following:

    With MyChtObj.Chart.Axes(xlValue, xlPrimary)
            .MinimumScale = "0"
            .MaximumScale = ws.Range("D5").Value
        End With
        
    
        With MyChtObj.Chart.Axes(xlCategory, xlPrimary)
             .MinimumScale = "0"
             .MaximumScale = ws.Range("D7").Value
        End With
    The first part works, i.e. the values are set on the Y axis, but errors out on the highlighted line above?

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,480

    Re: Help with setting chart axes and ranges

    What chart type do you have?

  5. #5
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: Help with setting chart axes and ranges

    this is my chart in the attached (with code).
    Attached Files Attached Files

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,480

    Re: Help with setting chart axes and ranges

    It fails because the default horizontal axis on an area chart is a categorical one and not numeric.
    If you really meant for the horizontal axis to be numeric you will need to trick it by setting the axis to be Date type rather than text. Then the code would work.

    Probably better to use numeric values rather than text.
    .Axes(xlValue, xlPrimary).MinimumScale = 0  ' and not "0"

  7. #7
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: Help with setting chart axes and ranges

    thanks Andy! Changing the horizontal axis to date type has done the trick

  8. #8
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: Help with setting chart axes and ranges

    I'm afraid this still does not work and I get an error 'Method MinimumScale of object Axis failed' on the highlighted line below?

    Sub graph()
    
    'set up graph
    Dim MyChtObj As ChartObject
    Set MyChtObj = ActiveSheet.ChartObjects("Chart 1")
    
    
    
        With MyChtObj.Chart
        .Axes(xlValue, xlPrimary).MinimumScale = "0"
        .Axes(xlValue, xlPrimary).MaximumScale = ActiveSheet.Range("L2").Value
        End With
        
        With MyChtObj.Chart
        .Axes(xlCategory, xlPrimary).MinimumScale = "0"
        .Axes(xlCategory, xlPrimary).MaximumScale = ActiveSheet.Range("I32").Value
        End With
    End Sub

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

    Re: Help with setting chart axes and ranges

    In many kinds of chart you can't change the minimum and maximum axis scale

    what is your actual intention of this code?
    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.

  10. #10
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: Help with setting chart axes and ranges

    the intention is to set the min and max scale for each axis of the graph.

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

    Re: Help with setting chart axes and ranges

    as you are using an AREA chart type
    to change the X-AXIS i believe you need to change the actual data series rather than just the scale

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

    Re: Help with setting chart axes and ranges

    here's my crack at doing that

    i suppose you don't need select case
    its in there if you want to extrapolate if/when you have varied chart types

    probably needs some error traps as well but ...anyways you get the idea
    Attached Files Attached Files

+ 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. [SOLVED] Ignoring chart types with no axes for finding axes title
    By gregersdk in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-23-2017, 06:41 AM
  2. Replies: 2
    Last Post: 12-07-2016, 05:20 PM
  3. [SOLVED] Axes setting to display mm:ss
    By Blake 7 in forum Excel Charting & Pivots
    Replies: 6
    Last Post: 11-14-2014, 12:22 PM
  4. chart generation for many data sets, setting ranges, need help finishing code
    By ESCOengineer in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-29-2013, 09:15 AM
  5. Changing the setting of X,Y axes
    By Prue in forum Excel General
    Replies: 0
    Last Post: 10-25-2012, 05:18 AM
  6. Excel 2007 Setting chart axes in VBA
    By JFistere in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-25-2011, 03:47 AM
  7. Merging code for hiding columns and setting chart axes values
    By tuckjoseph in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-23-2010, 01:19 PM

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