+ Reply to Thread
Results 1 to 16 of 16

Get the automatic maximum y-axis bounds value and paste in cell

  1. #1
    Forum Contributor
    Join Date
    05-25-2019
    Location
    Sioux Falls, South Dakota
    MS-Off Ver
    Microsoft 365
    Posts
    178

    Get the automatic maximum y-axis bounds value and paste in cell

    On the attached image, you will see the maximum y-axis value automatically listed as 30.0. What can be done for a worksheet cell (ex. A1) to automatically list this number? I need this number to set my phase line maximum.
    Please help!

    Screenshot 2022-06-09 163647.png

  2. #2
    Valued Forum Contributor
    Join Date
    01-07-2022
    Location
    Europe
    MS-Off Ver
    Office 365
    Posts
    473

    Re: Get the automatic maximum y-axis bounds value and paste in cell

    Two options I'm aware of:
    1) Use a VBA script to read this from the chart and paste into a cell
    2) Calculate manually based on your dataset used by the graph, following the same logic as Excel does. A description of how excel chooses these values is at https://peltiertech.com/how-excel-ca...t-axis-limits/
    <<< If you have valued anyone's contributions in this thread, please click * to thank them for their efforts

  3. #3
    Forum Contributor
    Join Date
    05-25-2019
    Location
    Sioux Falls, South Dakota
    MS-Off Ver
    Microsoft 365
    Posts
    178

    Re: Get the automatic maximum y-axis bounds value and paste in cell

    I'm trying to figure out what the VBA script would be. The closest I got was use the following debug.print function to print the maximum y scale value into the immediate window in code.
    Please Login or Register  to view this content.
    I can get the correct automatic value that the chart generates into the immediate code window when I run this code. But how do I get this value placed, for example, in cell U4 on the worksheet? I need this value in order to resize my phase line.
    Thanks
    Last edited by Dbroek; 06-15-2022 at 05:13 PM.

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,917

    Re: Get the automatic maximum y-axis bounds value and paste in cell

    Use the Range.Value property to write a value to a cell.
    Please Login or Register  to view this content.
    Range.value help file: https://docs.microsoft.com/en-us/off...el.range.value
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Forum Contributor
    Join Date
    05-25-2019
    Location
    Sioux Falls, South Dakota
    MS-Off Ver
    Microsoft 365
    Posts
    178

    Re: Get the automatic maximum y-axis bounds value and paste in cell

    I don't know how to write the code to use this.

  6. #6
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,917

    Re: Get the automatic maximum y-axis bounds value and paste in cell

    Try putting Sub test()..End Sub around it, maybe a Stop statement so it will enter debug mode, then see what happens when you run it:
    Please Login or Register  to view this content.
    paste that into a regular module, then make sure the desired Excel sheet is active, then place the cursor anywhere inside of the procedure and press F5 or Run->Run procedure. Use F8 to step through the procedure, watching what each statement does.

  7. #7
    Valued Forum Contributor
    Join Date
    01-07-2022
    Location
    Europe
    MS-Off Ver
    Office 365
    Posts
    473

    Re: Get the automatic maximum y-axis bounds value and paste in cell

    but to answer your question directly i.e. how to get the value and paste into cell U4, simply use:

    Please Login or Register  to view this content.
    or to make it even more concise....

    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    05-25-2019
    Location
    Sioux Falls, South Dakota
    MS-Off Ver
    Microsoft 365
    Posts
    178

    Re: Get the automatic maximum y-axis bounds value and paste in cell

    Thanks MrShorty, for sticking with this issue.
    I did paste the code into the module using the following format:
    Please Login or Register  to view this content.
    After that, went to the worksheet where the chart is located and ran this code there. it came up with a msgbox indicating a compile error: Variable not defined. "dblmaxscale =" was highlighted.
    So, maybe dblmaxscale is not being recognized as a definable variable?

    I'd like to make this work, if possible, just to get a better understanding of what's we're trying to accomplish.

  9. #9
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,917

    Re: Get the automatic maximum y-axis bounds value and paste in cell

    it came up with a msgbox indicating a compile error: Variable not defined. "dblmaxscale =" was highlighted.
    That suggests you have Option Explicit specified, which usually just means you need to add a DIM statement defining all variables, or, as noted by AskMeAboutExcel, bypass storing the max scale value in a variable.

  10. #10
    Forum Contributor
    Join Date
    05-25-2019
    Location
    Sioux Falls, South Dakota
    MS-Off Ver
    Microsoft 365
    Posts
    178

    Re: Get the automatic maximum y-axis bounds value and paste in cell

    I finally got the dblmaxscale code to work rewritten as this:

    Please Login or Register  to view this content.
    But had to add the ".MaximumScale" to the middle line. I'm guessing this was inadvertently left out?

  11. #11
    Forum Contributor
    Join Date
    05-25-2019
    Location
    Sioux Falls, South Dakota
    MS-Off Ver
    Microsoft 365
    Posts
    178

    Re: Get the automatic maximum y-axis bounds value and paste in cell

    AskMeAboutExcel,
    Both of your codes worked from the start, and performed as well as the dblmaxscale code I rewrote.
    All three, however, failed to place the max Y value into the U4 cell whenever I deselected one of the datasets with the larger max value (for example, 72) and kept selected a dataset containing a smaller max value (for example, 12). It is jammed up, somehow, by the phase line (see dataset series 11 in code below).

    Below is an example of what I had to rewrite to free things up so the codes can do their jobs:

    Please Login or Register  to view this content.
    It's pretty clunky and bangs the chart around a bit. Not exactly sure what to do next to smooth this out a bit more. But, at least, it accomplishes what I wanted to see.
    Thank you both for taking some of your time to help on this problem. I'm so grateful!

  12. #12
    Valued Forum Contributor
    Join Date
    01-07-2022
    Location
    Europe
    MS-Off Ver
    Office 365
    Posts
    473

    Re: Get the automatic maximum y-axis bounds value and paste in cell

    Dbroek - that's strange, although I'm not sure why it's having that behaviour.

    If you do need to filter / unfilter to get the right value, then you should put the U4 = max value logic at the end of your code, not the start though.

    Perhaps you can share a sanitised version of the worksheet and we can try help you get to the bottom of this? Or is your current workaround fit for purpose so no further support needed?

  13. #13
    Forum Contributor
    Join Date
    05-25-2019
    Location
    Sioux Falls, South Dakota
    MS-Off Ver
    Microsoft 365
    Posts
    178

    Re: Get the automatic maximum y-axis bounds value and paste in cell

    Here's the CountTest file.
    I changed to have the macro run when the button in O4 is changed instead of using a SelectionChange macro.

    I suspect that the U4 cell needs to automatically be refreshed whenever the series data is being changed on the chart.
    Attached Files Attached Files

  14. #14
    Valued Forum Contributor
    Join Date
    01-07-2022
    Location
    Europe
    MS-Off Ver
    Office 365
    Posts
    473

    Re: Get the automatic maximum y-axis bounds value and paste in cell

    does the chart only change when you select the dropdown options in Cells E4 and H4?

    If so, you can add the two lines highlighted in blue below to the Worksheet Change Event for 'Sheet 7 (Day1_5)', to run concurrently with updating the chart categories

    Please Login or Register  to view this content.

  15. #15
    Forum Contributor
    Join Date
    05-25-2019
    Location
    Sioux Falls, South Dakota
    MS-Off Ver
    Microsoft 365
    Posts
    178

    Re: Get the automatic maximum y-axis bounds value and paste in cell

    That doesn't work. I still have to hit the form control button to align the phase line properly with the rest of the data sets.

    For example, if you have data1 selected to graph on the chart and then deselect it and have only data2 and data3 selected to be graphed, the phase line will stay stuck at 76. If you then hit the form control button, it will drop down to 57, which is where we want it to be. The phase line occurs on the 5/17/22 date and takes its form from the 1_5 worksheet.

  16. #16
    Forum Contributor
    Join Date
    05-25-2019
    Location
    Sioux Falls, South Dakota
    MS-Off Ver
    Microsoft 365
    Posts
    178

    Re: Get the automatic maximum y-axis bounds value and paste in cell

    Playing around with Record Macro I noticed something interesting that could possibly lead to a solution.

    The following macro:
    Please Login or Register  to view this content.
    runs the getYAxisMax code after deselecting the phase line data set and then reselects the phase line data set while the chart is activated. This allows the Phase Line to accept the new value that the getYAxisMax placed in the U4 cell. I noticed that when I ran the Macro1 code immediately after deselecting or selecting another data set inside the chart, for example...
    Please Login or Register  to view this content.
    the phase line readapts smoothly to the correct value without causing the chart to bounce like it does when hitting the form control button.

    Is it possible to have a code that automatically runs Macro1 or maybe the following
    Please Login or Register  to view this content.
    whenever any of the available ActiveChart.FullSeriesCollection(X).IsFiltered false or true? It will be a dream if that could be done.

+ 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. Auto Adjust of Primary and Secondary Axis Bounds
    By polishfc in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 12-05-2020, 04:54 AM
  2. Replies: 1
    Last Post: 12-03-2020, 01:11 PM
  3. Trying to change the bounds of the x axis of a line graph
    By Stovasaurus in forum Excel Charting & Pivots
    Replies: 4
    Last Post: 06-30-2020, 09:28 PM
  4. Dynamic Axis Bounds - is it possible?
    By rgunter in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 04-30-2019, 03:28 PM
  5. Excel graph- two bounds on X Axis
    By cindy100 in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 01-23-2019, 08:57 AM
  6. Automatic Bounds in a Graph not even when refreshed
    By bencook in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 09-28-2017, 11:29 AM
  7. [SOLVED] VBA for changing the x-axis bounds in a graph
    By lrouquette in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-21-2015, 07:18 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