+ Reply to Thread
Results 1 to 9 of 9

Primary and secondary Y axis should cut X axis at same value

Hybrid View

  1. #1
    Registered User
    Join Date
    10-31-2010
    Location
    London
    MS-Off Ver
    Excel 2007, Excel 2010
    Posts
    56

    Primary and secondary Y axis should cut X axis at same value

    Hi All,

    I have a chart in excel 2010 where the primary Y axis on the left shows returns in % (plotted as a column chart) and the secondary Y axis on the right shows the cumulative return also in % (plotted as a line chart).

    Now the problem I am having is that the secondary Y axis instead of cutting the X-axis at 0 (just like the primary Y axis does) cuts it at some random value. Now to remedy this I set the option 'Horizontal axis crosses at' to 0.0 but this does not seem to work and the secondary Y axis still does not cut the X-axis at 0.

    Any other ideas how this problem can be remedied?

    Thanks
    Last edited by goels; 05-24-2011 at 12:23 PM.

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

    Re: Primary and secondary Y axis should cut X axis at same value

    http://peltiertech.com/Excel/Charts/AlignXon2Ys.html
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    10-31-2010
    Location
    London
    MS-Off Ver
    Excel 2007, Excel 2010
    Posts
    56

    Re: Primary and secondary Y axis should cut X axis at same value

    Hi Andy,

    Thanks. I incorporated the code in my sheet and can see option 4 ie input 4 to AlignY function works best for me. As I am not that conversant with VBA can I ask how this code can be modified so that whenever my spreadsheet is opened for the first time option 4 runs for a particular chart (I have several on a worksheet but just want the code to apply to one of them) automatically without having to manually run the macro?

    Thanks

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

    Re: Primary and secondary Y axis should cut X axis at same value

    You can use the Thisworkbook workbook_open event to run code on opening.

  5. #5
    Registered User
    Join Date
    10-31-2010
    Location
    London
    MS-Off Ver
    Excel 2007, Excel 2010
    Posts
    56

    Re: Primary and secondary Y axis should cut X axis at same value

    Thanks. I have managed to trim the code down to the below:


    ' The Main Event
        Sub AlignY()
    Dim Y1min As Double
    Dim Y1max As Double
    Dim Y2min As Double
    Dim Y2max As Double
    
    With ActiveChart
    With .Axes(2, 1)
    Y1min = .MinimumScale
    Y1max = .MaximumScale
    .MinimumScaleIsAuto = False
    .MaximumScaleIsAuto = False
    End With
    With .Axes(2, 2)
    Y2min = .MinimumScale
    Y2max = .MaximumScale
    .MinimumScaleIsAuto = False
    .MaximumScaleIsAuto = False
    End With
    With .Axes(2, 2)
    .MinimumScale = Y1min * Y2max / Y1max 
    End With
    End With
        End Sub
    The problem I am facing now is how to tell this code that always run for my particular chart. Reading a bit on the internet what I was trying is this code which should activate my chart called Chart5 (I got this name by clicking on the chart and then looking in the name box on top left) but when I run this code it gives all sorts of errors. Am I missing something here?

    Sub activate1()
        ActiveSheet.ChartObjects("Chart5").activate
    End Sub
    Thanks

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

    Re: Primary and secondary Y axis should cut X axis at same value

    ' The Main Event
        Sub AlignY(Cht as chart)
    Dim Y1min As Double
    Dim Y1max As Double
    Dim Y2min As Double
    Dim Y2max As Double
    
    With Cht
    With .Axes(2, 1)
    Y1min = .MinimumScale
    Y1max = .MaximumScale
    .MinimumScaleIsAuto = False
    .MaximumScaleIsAuto = False
    End With
    With .Axes(2, 2)
    Y2min = .MinimumScale
    Y2max = .MaximumScale
    .MinimumScaleIsAuto = False
    .MaximumScaleIsAuto = False
    End With
    With .Axes(2, 2)
    .MinimumScale = Y1min * Y2max / Y1max 
    End With
    End With
    End Sub
    Sub activate1()
        AlignY thisworkbook.worksheets("xx").ChartObjects("Chart5").chart
    End Sub
    Just alter worksheet name from xx to whatever is relavent to your file.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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