Closed Thread
Results 1 to 17 of 17

Aligning X & Y Axis at Zero

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-08-2016
    Location
    Canada
    MS-Off Ver
    MS365 Version 2401
    Posts
    130

    Aligning X & Y Axis at Zero

    Hello All,

    This might have been asked before and I am sorry if I couldn't find it.

    I have an excel chart with two different values. One on the primary axis and another on a secondary. The problem I am having is to have the secondary axis align at zero as the primary, as right now it looks like my lease count is a negative at first glance if you are not aware of the secondary axis.

    I have seen recommendations of programming for this but since I don't know anything about programming, I am wondering if there is an easy way to do this.

    I am attaching the data for the chart with the Max and Min values on the primary axis as bars and the Leases on the secondary axis as a line.

    Thanks.
    Attached Files Attached Files

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

    Re: Aligning X & Y Axis at Zero

    Easiest is probably manually. I formatted the secondary axis to have a minimum of -40 and a maximum of 50 (to coincide with the primary axis's limits of -0.4 and 0.5). If you are unsure how to bring up the format axis dialog: https://support.office.com/en-us/art...3-f9ce41df0021

    You will need to do this everytime the primary axis changes, but it is easy.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Re: Aligning X & Y Axis at Zero

    you want zeroes of both axis on the same level?

    nevermind

  4. #4
    Forum Contributor
    Join Date
    09-08-2016
    Location
    Canada
    MS-Off Ver
    MS365 Version 2401
    Posts
    130

    Re: Aligning X & Y Axis at Zero

    Hi Sandy666,

    Yes, I do and I am aware that there is a macro that you can use for this, however I am totally ignorant of macros.

    Playing around with values as MrShorty suggested, but since I have multiple values, would be nice to figure out a way to do this instead of manually.

    Thanks.

  5. #5
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Re: Aligning X & Y Axis at Zero

    first what i did is:

    2nd-axis.jpg

    but i really don't know it is what you want

  6. #6
    Forum Expert sourabhg98's Avatar
    Join Date
    10-22-2014
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2007, 2013
    Posts
    1,899

    Re: Aligning X & Y Axis at Zero

    For this you will need to change the minimum value of the secondary axis in a way that horizontal axis crosses secondary axis at 0.

    You can use this simple macro which adjusts the minimum value of secondary axis according to the ratio of primary axis. (It is assumed primary axis crosses the x axis at 0)

    Sub alignme()
    With ActiveSheet.ChartObjects("Chart 2").Chart
    x1 = .Axes(xlValue).MinimumScale
    y1 = .Axes(xlValue).MaximumScale
    r1 = x1 / y1
    y2 = .Axes(xlValue, xlSecondary).MaximumScale
    x2 = y2 * r1
    .Axes(xlValue, xlSecondary).MinimumScale = x2
    End With
    End Sub

    Check attached. You can just press that button to align.

    X & Y.xlsm
    Happy to Help

    How to upload excel workbooks at this forum - http://www.excelforum.com/the-water-...his-forum.html

    "I don't get things easily, so please be precise and elaborate"

    If someone's post has helped you, thank by clicking on "Add Reputation" below the post.
    If your query is resolved please mark the thread as "Solved" from the "Thread Tools" above.

    Sourabh

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

    Re: Aligning X & Y Axis at Zero

    Excel does not have a built in "automatic" way to relate the secondary axis values to the primary axis. If you do not like manually changing the axis limits, I can make two further suggestions.

    1) Use a charting application other than Excel. I have used other charting applications (temporary trial basis only) which had the option to format the secondary axis as a function of the primary axis.

    2) You can use a "dummy series and format it to look like an axis" kind of approach. Something like this: https://peltiertech.com/secondary-ax...tional-scales/

    3) You can use VBA to read the primary axis limits and set the secondary axis limits to something reasonable. You did not seem to like the idea of trying to use VBA, so I will not pursue this further.

    Assuming that you really want to use Excel for this, approach 2 is probably the next one to look at.

  8. #8
    Forum Contributor
    Join Date
    09-08-2016
    Location
    Canada
    MS-Off Ver
    MS365 Version 2401
    Posts
    130

    Re: Aligning X & Y Axis at Zero

    Sourabhg98, The macro works perfectly on the example sheet, however since I am not at all familiar with macros and how to use them, would you be kind enough to walk me through the procees of setting it up in my current workbook?

    Thanks.

  9. #9
    Forum Contributor
    Join Date
    09-08-2016
    Location
    Canada
    MS-Off Ver
    MS365 Version 2401
    Posts
    130

    Re: Aligning X & Y Axis at Zero

    Thanks MrShorty, I appreciate your suggestions.

  10. #10
    Forum Expert sourabhg98's Avatar
    Join Date
    10-22-2014
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2007, 2013
    Posts
    1,899

    Re: Aligning X & Y Axis at Zero

    Sure, firstly copy the code from Post #6. Now open your workbook, press Alt+F11, a new window will pop up. Now go to Insert >> Module >> Paste this code in the white area.

    Now, insert a shape on the sheet where you have the chart. Right Click on the shape >> Assign macro >> and select "alignme".

    Now just press the button to align the axis.

    Also, you have to change the "Chart 2" in the code to whatever the name of your chart is.

  11. #11
    Forum Contributor
    Join Date
    09-08-2016
    Location
    Canada
    MS-Off Ver
    MS365 Version 2401
    Posts
    130

    Re: Aligning X & Y Axis at Zero

    Great! Thank you very much.

    Works perfectly!

  12. #12
    Forum Expert sourabhg98's Avatar
    Join Date
    10-22-2014
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2007, 2013
    Posts
    1,899

    Re: Aligning X & Y Axis at Zero

    Glad to know we could help!

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

  13. #13
    Registered User
    Join Date
    02-26-2008
    Posts
    21

    Re: Aligning X & Y Axis at Zero

    I have been using the VBA code for a couple of graphs and it works great. However, I applied the code to a new (but similar) graph and the result
    is that the primary and secondary axis do not share a common zero.
    My primary axis max-min range from +8 to -4 and my secondary min to max is -1.5 to -.25 (all points are less than zero for the secondary data
    - could this be the issue)? I attached a picture of the resultant Axis format - this is what the chart and settings look like after the macro is ran.
    The green line is applicable to the secondary (right) axis. Axis.PNG

    Thoughts?

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

    Re: Aligning X & Y Axis at Zero

    @MCII: This forum strongly discourages posting your question in another person's thread -- even if it is similar. They strongly prefer that you start your own thread. You can (I think) provide a link to this thread, if you feel it will help to explain your question.

  15. #15
    Registered User
    Join Date
    06-01-2011
    Location
    Eugene, Oregon
    MS-Off Ver
    Excel 2007
    Posts
    1

    Re: Aligning X & Y Axis at Zero

    I used this macro and it works wonderfully. THANK YOU THANK YOU THANK YOU sourabhg98!!

  16. #16
    Registered User
    Join Date
    06-21-2023
    Location
    spain
    MS-Off Ver
    365
    Posts
    1

    Re: Aligning X & Y Axis at Zero

    Hi guys,

    the solution with VBA code is perfect, but is possible to change the VBA code in order that the axis always display the value "0"?

    Thank for your help.

    Chema.

  17. #17
    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. 2504 (Windows 11 Home 24H2 64-bit)
    Posts
    90,941

    Re: Aligning X & Y Axis at Zero

    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original.

    Please see Forum Rule #4 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread
    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.

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 5
    Last Post: 01-07-2017, 03:17 PM
  2. What is the VBA code for aligning x axis text 270 deg
    By DDCC in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-28-2014, 03:35 AM
  3. Replies: 14
    Last Post: 08-01-2013, 06:34 AM
  4. [SOLVED] Aligning category Axis in a stacked column chart
    By phedax in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 06-01-2013, 12:59 PM
  5. Excel chart: Aligning secondary axis with primary
    By Harlort in forum Excel General
    Replies: 3
    Last Post: 04-04-2011, 10:26 AM
  6. Aligning horizontal 0 axis, chart with two axis
    By Harlort in forum Excel Charting & Pivots
    Replies: 10
    Last Post: 02-10-2011, 08:54 AM
  7. [SOLVED] Aligning decimal numers to the centre of the cell and aligning dec
    By Ramesh Babu in forum Excel General
    Replies: 1
    Last Post: 07-01-2006, 05:40 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