Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  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
    2003 & 2007 & 2010
    Posts
    10,944
    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
    Code:
        With ActiveChart.Axes(xlValue)
            .MinimumScale = Range("A1")
            .MaximumScale = Range("A2")
        End With
    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
    2003 & 2007 & 2010
    Posts
    10,944
    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

    Code:
    Sub ChangeAxis()
    
        With ActiveChart.Axes(xlValue)
            .MinimumScale = Range("A1")
            .MaximumScale = Range("A2")
        End With
    
    End sub

    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
    Cheers
    Andy
    www.andypope.info

  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
    2003 & 2007 & 2010
    Posts
    10,944
    Yes. The routine would need to know which sheet to check.

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

    Code:
    Sub AxisChange(Cht As Chart, MinValue, MaxValue)
        With Cht.Axes(xlValue)
            .MinimumScale = MinValue
            .MaximumScale = MaxValue
        End With
    End Sub
    And the sheet level code can pass the relevant information

    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        If Not Intersect(Range("A1:A2"), Target) Is Nothing Then
            AxisChange ActiveSheet.ChartObjects(1).Chart, Range("A1").Value, Range("A2").Value
        End If
        
    End Sub
    Cheers
    Andy
    www.andypope.info

  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.

    Code:
    Sub AxisChange(Cht As Chart, MinValue, MaxValue)
        With Cht.Axes(xlValue)
            .MinimumScale = MinValue
            .MaximumScale = MaxValue
        End With
    End Sub
    How should I do that? Is there any modification in here?

    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        If Not Intersect(Range("A1:A2"), Target) Is Nothing Then
            AxisChange ActiveSheet.ChartObjects(1).Chart, Range("A1").Value, Range("A2").Value
        End If
        
    End Sub
    Thx

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    10,944
    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
    Cheers
    Andy
    www.andypope.info

  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:

    Code:
    Sub AxisChange(Cht As Chart, MinValue, MaxValue)
        With Cht.Axes(xlValue)
            .MinimumScale = MinValue
            .MaximumScale = MaxValue
        End With
    End Sub
    Attached to the "data" datasheet this one:

    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        If Not Intersect(Range("A1:A2"), Target) Is Nothing Then
            AxisChange ActiveSheet.ChartObjects(1).Chart, Range("A1").Value, Range("A2").Value
        End If
        
    End Sub
    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
    2003 & 2007 & 2010
    Posts
    10,944
    I can not seem to donwload your example.
    Can you email it to me direct? andy AT andypoe DOT info
    Cheers
    Andy
    www.andypope.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
    2003 & 2007 & 2010
    Posts
    10,944
    Thanks!

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

    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        If Not Intersect(Range("A1:A2"), Target) Is Nothing Then
            AxisChange Chart1, Range("A1").Value, Range("A2").Value
        End If
        
    End Sub

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

    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        If Not Intersect(Range("A1:A2"), Target) Is Nothing Then
            AxisChange Worksheets("Sheet3").ChartObjects(1).Chart, Range("A1").Value, Range("A2").Value
        End If
        
    End Sub
    Cheers
    Andy
    www.andypope.info

  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???

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.2.0