Closed Thread
Results 1 to 17 of 17

Changing scale axis

Hybrid View

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

    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

  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.

    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

    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

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