Closed Thread
Results 1 to 17 of 17

Changing scale axis

  1. #1
    Registered User
    Join Date
    05-22-2008
    Posts
    22

    Changing scale axis

    Hi all. I would like two change the axis scale of a chart by writing the values into a cell. How could I manage with that? Should I use VBA? I don't have any experience on that but I could try with your help.

    Thank you.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481
    VBA is the only way as you can not directly link scale values to cells.

    Something like this will do it for the Value axis
    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    05-22-2008
    Posts
    22
    Thx Andy. I'm trying to use this code for the file I sent you a couple of days ago. I haven't used before this VBA and I'm trying to find out the way it works. I do the following steps:

    1) Open the visual basic editor.
    2) Insert new module
    3) Copy and paste this code there:

    With ActiveChart.Axes(xlValue)
    .MinimumScale = Range("A1")
    .MaximumScale = Range("A2")
    End With
    4) write in the corresponding values so, A1=1000 A2=2000

    is that all? Does the chart change automatically??any other modifications?

    I would appreciate also if you could tell me where to find a link with some information about VBA for beginners.

    Thx

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481
    There is a little more to it than that.

    You would need to wrap the code in a procedure.
    Then select the chart and run the macro

    Please Login or Register  to view this content.

    Or you could add code to the worksheet change event so the code is run when the cells containing the information are altered.
    See attached.

    Included in this is a change to the procedure so the chart to be altered is passed as an argument rather than relying on the active chart object
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    05-22-2008
    Posts
    22
    Is there any change when having the chart in different sheet?

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481
    Yes. The routine would need to know which sheet to check.

    So I have added the min and max values to the argument list.

    Please Login or Register  to view this content.
    And the sheet level code can pass the relevant information

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    05-22-2008
    Posts
    22
    Hi Andy. Thx for your wise suggestions. I managed to use all the code you sent including the part for running the code when changing the cells.

    I'm not able to use it when the chart is in different datasheet though. I think the problem is in defining the variables MinValue & and Maxvalue as inputs you suggested in the rutine you sent. All the cells are in sheet1 and the chart is within the same book as chart sheet.

    Please Login or Register  to view this content.
    How should I do that? Is there any modification in here?

    Please Login or Register  to view this content.
    Thx

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481
    The AxisChange routine would be in a standard code module and is cabable of handling the first chartobject on any sheet.

    But the trick to getting it to work on multiple sheets is to add the Worksheet_Change code to each sheet that has a chart and cell information.

    See revised example.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    05-22-2008
    Posts
    22
    Ok, it's a bit more clear!! but still i get an error about the chart objects property when trying to use the values from the datasheet to the chart. what could it be? I attached and example to clarify my question.

    Thx
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    05-22-2008
    Posts
    22
    Hi Andy. I've tried everything which is in my hands but i'm not able to find how to find the solution when having the data and the chart in diferent data sheet.

    I've got in the main module the following code:

    Please Login or Register  to view this content.
    Attached to the "data" datasheet this one:

    Please Login or Register  to view this content.
    And attached to the "chart" datasheet the same code as for the datasheet. I also tried without code in the "chart" datasheet and i made several changes concerning the name of variables (i.e. Sheets("Sheet1").Range("1")) and so on but still I get errors when running it....
    Any hint.

    Thank you very much.

  11. #11
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481
    I can not seem to donwload your example.
    Can you email it to me direct? andy AT andypoe DOT info

  12. #12
    Registered User
    Join Date
    05-22-2008
    Posts
    22
    Sure! I sent it to andy AT andypope DOT info

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

    The revised code from your example.
    Change cells A1 or A2 on Sheet1 and Chart Sheet Chart1 will be updated.

    Please Login or Register  to view this content.

    And for my revised workbook example this is Sheet1's Change event code

    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    05-22-2008
    Posts
    22
    Ok!!! It works perfectly.

    I took me several hours to try and find the solution and I couldn't....I'm getting familiar with this VBA and is getting interesting but still too much to learn about it.

    THX a lot once again.

  15. #15
    Registered User
    Join Date
    07-03-2010
    Location
    Kemaman, Malaysia
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Changing scale axis

    i have tried your method and its work very well.
    just one question, what if i want it to autoscale again?
    is there any vb or macro that i can use???

  16. #16
    Registered User
    Join Date
    07-03-2010
    Location
    Kemaman, Malaysia
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Changing scale axis

    \1

    As you can see in the picture above...
    I've manage to make a graph of combination of scroll bar and drop down list.
    But then i'm having problem with some graph which got some offset data.
    So, i took your method to change the scale. Then another problem occurred as some graph didn't need to set the y-scale manually. So, i was hoping that i can do some vb @ macro that can change the Y-scale back to auto.

  17. #17
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Changing scale axis

    @nawzi7, welcome to the baord however please note:

    Per Forum Rules do not post a question in the thread of another member -- start your own thread.
    If you feel another thread is particularly relevant to your own question provide a link to the other thread within your own.

Closed 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