Link Excel Chart Axis Scale to Values in Cells

Excel offers two ways to scale chart axes. You can let Excel scale the axes automatically; when the charted values change, Excel updates the scales the way it thinks they fit best. Or you can manually adjust the axis scales; when the charted values change, you must manually readjust the scales. Wouldn’t it be great to be able to link the axis scale parameters to values or, even better, formulas in the worksheet? This page shows how to use VBA to accomplish this.

If you want a ready-to-use solution, try Tushar Mehta’s AutoChart Manager add-in, available as a free download at

There are a few pieces you need to make this technique work. You need a chart, a set of values for the scaling parameters, and some VBA code to change the axis scales. The code can be either linked to a button, or run from a Worksheet_Change event procedure.

The Chart

The actual mechanics of creating this chart are incidental to the discussion, but we’ll use the following simple data and chart (named “Chart 1″, the default name of the first chart created in a worksheet).

Simple data and scatter chart in Excel

Axis Scale Parameters in the Worksheet

You need a place to put the axis scale parameters. In this example, the range B14:C16 is used to hold primary X and Y axis scale parameters for the embedded chart object named “Chart 1″. This example can be expanded to include secondary axes, or to change other charts as well.

Simple data and scatter chart with range containing axis scale parameters

The cells B14:C16 can contain static values which the user can manually change, or they can contain formulas with your favorite axis scaling algorithms. See how to set up axis-scaling formulas in Calculate Nice Axis Scales in Your Excel Worksheet.

Change Chart Axes with VBA

The parts of Excel’s charting object model needed here are the .MinimumScale.MaximumScale, and .MajorUnit properties of the Axis object (the .MinorUnit property could also be controlled, but I usually do not show minor tick marks). These properties can be set equal to constant values, to VBA variables, or to worksheet range or named range values, as illustrated in this code sample:

    With ActiveChart.Axes(xlValue, xlPrimary)
      .MaximumScale = 6
        ' Constant value
      .MinimumScale = dYmin
        ' VBA variable
      .MajorUnit = ActiveSheet.Range("A1").Value
        ' Worksheet range value
    End With

If you have a Line, Column, or Area chart with a category-type X axis, you can’t use the properties shown above. The maximum and minimum values of a category axis cannot be changed, and you can only adjust .TickLabelSpacing and.TickMarkSpacing. If the X axis is a time-scale axis, you can adjust .MaximumScale.MinimumScale, and .MajorUnit. You should turn on the macro recorder and format an axis manually to make sure you use correct syntax in your procedure. Any chart’s Y axis is a value axis, and this code will work as is.

VBA Procedure to Rescale Chart Axes

Press Alt+F11 to open the VB Editor. In the Project Explorer window, find the workbook to which you want to add code.

Project Explorer window of the VB Editor

Double click on the module to open it. If there is no module, right click anywhere in the workbook’s project tree, choose Insert > Module. Or use Insert menu > Module. Up will pop a blank code module.

Empty code module

If your module does not say Option Explicit at the top, type it in manually. Then go to Tools > Options, and in the Editor tab check the Require Variable Declaration checkbox. This will place Option Explicit at the top of every new module, saving innumerable problems caused by typos. While in the Options dialog, uncheck “Auto Syntax Check”. This will save innumerable warnings about errors you already know about because the editor turns the font of the offending code red.

Tools menu > Options in the VB Editor

You can use a simple procedure that changes the axes on demand. The following changes the scales of the active chart’s axes using the values in B14:C16. Select the chart, then run the code.

Sub ScaleAxes()
  With ActiveChart.Axes(xlCategory, xlPrimary)
    .MaximumScale = ActiveSheet.Range("B14").Value
    .MinimumScale = ActiveSheet.Range("B15").Value
    .MajorUnit = ActiveSheet.Range("B16").Value
  End With
  With ActiveChart.Axes(xlValue, xlPrimary)
    .MaximumScale = ActiveSheet.Range("C14").Value
    .MinimumScale = ActiveSheet.Range("C15").Value
    .MajorUnit = ActiveSheet.Range("C16").Value
  End With
End Sub

You can type all this into the code module, or you can copy it and paste it in.

Code module with sample code

Select the chart and run the code. You can run the code by pressing Alt+F8 to open the Macros dialog, selecting the procedure in the list of macros, and clicking Run.

Macros dialog with ScaleAxes procedure

Or you could assign the code to a button in the worksheet.

Here is the chart after running the code.

Simple data and scatter chart after rescaling axes

Worksheet_Change Event Procedure to Rescale Chart Axes

A more elegant approach is to change the relevant axis when one of the cells within B14:C16 changes. We can use the Worksheet_Change event to handle this.

For an introductory description of events in Microsoft Excel, check out the Events page on Chip Pearson’s web site (

The Worksheet_Change event procedure fires whenever a cell in the worksheet is changed. To open the code module for a worksheet, right click on a worksheet tab and select View Code from the pop up menu. Or double click on the worksheet object in the Project Explorer window. The code module for the worksheet is opened. Now that we’ve set Require Variable Declaration, note that Option Explicit has appeared automatically atop the module.

Empty worksheet code module

You can write the entire procedure yourself, but it’s easier and more reliable to let the VB Editor start it for you. Click on the left hand dropdown at the top of this module, and select Worksheet from the list of objects.

Worksheet code module - Objects dropdown

This places a stub for the Workbook_SelectionChange event in the module. Ignore this for now.

Click on the right hand dropdown at the top of this module, and select Change from the list of events.

Worksheet code module - Events dropdown

You now have a couple event procedure stubs.

Worksheet code module with event procedure stubs

Delete the Worksheet_SelectionChange stub, which we will not be needing, and type or paste the Worksheet_Change code into the Worksheet_Change stub.

Worksheet code module with Worksheet_Change event procedure

The code is given below, so you can copy it. When the event fires, it starts the procedure, passing in Target, which is the range that has changed. The procedure uses Select Case to determine which cell was changed, then changes the appropriate scale parameter of the appropriate axis.

Private Sub Worksheet_Change(ByVal Target As Range)
  With ActiveSheet.ChartObjects("Chart 1").Chart
    Select Case Target.Address
      Case "$B$14"
        .Axes(xlCategory).MaximumScale = Target.Value
      Case "$B$15"
        .Axes(xlCategory).MinimumScale = Target.Value
      Case "$B$16"
        .Axes(xlCategory).MajorUnit = Target.Value
      Case "$C$14"
        .Axes(xlValue).MaximumScale = Target.Value
      Case "$C$15"
        .Axes(xlValue).MinimumScale = Target.Value
      Case "$C$16"
        .Axes(xlValue).MajorUnit = Target.Value
    End Select
  End With
End Sub

Peltier Tech Chart Utility


  1. And they all lived happily ever after. Until someone inserted another row/column in the workbook above/to the left of B14. Or until someone thought “I really don’t want those trigger parameters in the range B14:C16 so I’m going to move them to E20:F22.”

    At which case you decide it’s safest to name those input ranges, and make a slight tweak to the code

    Private Sub Worksheet_Change(ByVal Target As Range)
    With ActiveSheet.ChartObjects("Chart 1").Chart
    Select Case Target.Address
    Case Is = [x_Max].Address
    .Axes(xlCategory).MaximumScale = Target.Value
    Case Is = [x_Min].Address
    .Axes(xlCategory).MinimumScale = Target.Value
    Case Is = [x_Tick].Address
    .Axes(xlCategory).MajorUnit = Target.Value
    Case Is = [y_Max].Address
    .Axes(xlValue).MaximumScale = Target.Value
    Case Is = [y_Min].Address
    .Axes(xlValue).MinimumScale = Target.Value
    Case Is = [y_Tick].Address
    .Axes(xlValue).MajorUnit = Target.Value
    End Select
    End With
    End Sub

    And they all lived happily ever after. Until someone renamed the chart. But let’s save that for the sequel ;-)

  2. Forgot to say….great post. It’s fun to change those parameters and watch things adjust. Any chance of a download file, to encourage the folks at home to play along?

  3. Nice post as always.
    I however, in order to minimize vba routines, use automatic scaling by normalizing the values to numbers between 0 and 1. You can then manipulate the axis to show the values you need. and even set tick lines by using error bars.
    I have attached a link to a workbook showing my solution.
    Maybe an idea for a post after some tweaking. it actually is made by using information from Jon’s blog

  4. I have been trying to build a table for a rating system in excel.
    The following are the points to be awarded based on productivity and quality parameters for various processes

    Is there a way I can use vlookup or index/match to populate values in the table below based on the point scheme above ?

  5. Hi to all,

    This is a great post. Im not quite experienced with VBA codes and was very easy to adapt to my needs. however I got a question:

    I replaced the numbers on the cells with a formula to adjust the max and min values, but when the formula changes the new value, it does not update on the chart, unless I double click-enter on each of the cells.

    Is there any way to come around this?
    many thanks again,


  6. Arthur –

    Private Sub Worksheet_Change(ByVal Target As Range)

    fires when a cell is changed, but not when one is recalculated. Instead you need to use

    Private Sub Worksheet_Calculate()
  7. Great!, thanks for the heads up Jon!
    best regards,

  8. Thank you Jon for a simple and timely fix to a problem I was facing. However, I’m now trying to embed the charts into a Word document, and no matter how I try to paste them into the document – linked by either the Excel or the Word themes – the charts refuse to recognize the reconfigured Y axis. The chart in the Word doc pastes in the fixed values from the Excel chart that I copied and pasted, and won’t readjust as the data (and Y axis ranges) change when I update the Excel chart. Any suggestions? Thanks again for great tips!

  9. Hi Jon,

    Thanks for the explainations. Just like Arthur would I like to run the script when there is a new calculation. Here is what I wrote:

    Option Explicit

    Private Sub Worksheet_Calculate(ByVal Target As Range)
    With ActiveSheet.ChartObjects(“Chart 1″).Chart
    Select Case Target.Address

    Case “$G$2″
    .Axes(xlValue).MinimumScale = Target.Value

    End Select
    End With
    End Sub

    However, I get the following error message and the script doesn’t seem to work properly:
    Compile Error: Procedure declaration does not match description of event or procedure having the same name

    Any idea where the mistake could be?

    Thanks and best regards,

  10. Worksheet_Change has an argument Target which indicates which range has changed; Worksheet_Calculate has no such argument, since the whole sheet is recalculated. You’ll need something like this:

    Private Sub Worksheet_Calculate()
      Dim cht As Chart
      Dim wks As Worksheet
      Set wks = ActiveSheet
      Set cht = wks.ChartObjects("Chart 1").Chart
      If wks.Range("$B$14").Value <> cht.Axes(xlCategory).MaximumScale Then
        cht.Axes(xlCategory).MaximumScale = wks.Range("$B$14").Value
      End If
      If wks.Range("$B$15").Value <> cht.Axes(xlCategory).MinimumScale Then
        cht.Axes(xlCategory).MinimumScale = wks.Range("$B$15").Value
      End If
      If wks.Range("$B$16").Value <> cht.Axes(xlCategory).MajorUnit Then
        cht.Axes(xlCategory).MajorUnit = wks.Range("$B$16").Value
      End If
      If wks.Range("$C$14").Value <> cht.Axes(xlValue).MaximumScale Then
        cht.Axes(xlValue).MaximumScale = wks.Range("$C$14").Value
      End If
      If wks.Range("$C$15").Value <> cht.Axes(xlValue).MinimumScale Then
        cht.Axes(xlValue).MinimumScale = wks.Range("$C$15").Value
      End If
      If wks.Range("$C$16").Value <> cht.Axes(xlValue).MajorUnit Then
        cht.Axes(xlValue).MajorUnit = wks.Range("$C$16").Value
      End If
    End Sub
  11. Are any other changes required to use Private Sub Worksheet_Calculate()? I am running into errors with a simple substitution. Thanks.

  12. Nevermind. The above code works. Thanks.

  13. Thanks a lot, that was a great help!

  14. Okay, I have to ask.. How do you know what the charts name is? I doubleclicked on the chart, then clicked on Properties and it doesn’t tell me.

  15. Rob –

    Select the chart, and look in the Name Box (above cell A1).

  16. I can get the codes to work for one chart, but what if I have multiple charts on the same sheet, with different ranges for axes? Chart 1 would have y-axis values in C26:C28, while Chart 2 would have the values in D26:D28. Ideally they would both run when under worksheet_change or worksheet_calculate.

  17. John, Thanks for the posting. I have been looking for this type of solution for a while. Thanks for helping us out.

  18. Hi Jon,,

    What if I want to add a secondary vertical axis on my chart? How do I modify the Worksheet_Calculate() code to achieve that? Thx

  19. Jonathan –

    The same way I’ve used B14:C16 to hold parameters for the primary X and Y axis scales, you could use D14:E16 for the secondary axis scales, and extend the code to update these as well.

  20. Hi,
    I’m a VBA novice. This code:

    Sub ChangeAxisScales()
    Dim objCht As ChartObject
    For Each objCht In ActiveSheet.ChartObjects
    With objCht.Chart
    ‘ Value (Y) Axis
    With .Axes(xlValue)
    .MaximumScale = Sheets(“Inputs”).Range(“D29″).Value
    .MinimumScale = Sheets(“Inputs”).Range(“D30″).Value
    .MajorUnit = Sheets(“Inputs”).Range(“D31″).Value
    End With
    With .Axes(xlCategory)
    .MaximumScale = Sheets(“Inputs”).Range(“C29″).Value
    .MinimumScale = Sheets(“Inputs”).Range(“C30″).Value
    .MajorUnit = Sheets(“Inputs”).Range(“C31″).Value
    End With
    End With
    Next objCht
    End Sub

    Works fairly well except for 1 main thing and 1 minor thing! The main thing is that I want the worksheet where the data is entered (the Chart 1 is on a different worksheet) to automatically update the chart when a different value is entered (instead of running the macro). The minor thing is that when the x-axis maximum is say 5, the axis goes to up 5 but the last number shown along the axis is 4. How can I change what I have now? I’ve tried various things I’ve seen online but to no avail.



  21. J –

    The Worksheet_Calculate or _Change event code has to be on the code module attached to the sheet where the data is located, but the code has to reference the changing chart by the name of the sheet that contains the chart.

  22. This is fantastic. I am using your second example of how to update the axis when new data is entered into the cell. I am having trouble replicating the function to accommodate multiple graphs on a page. I am a vba rookie, so I’m sure its simple, but I have yet to figure it out. Can you show how this would be done for multiple graphs?


  23. Hi,

    I would like to know how can I record a macro so I could insert a button in order to refresh the limits instead of running the macro pressing alt+f8 and ‘run’.


  24. Dave –

    Try this:

    Private Sub Worksheet_Change(ByVal Target As Range)
      Dim cht As ChartObject
      For Each cht In ActiveSheet.ChartObjects
        With cht.Chart
          Select Case Target.Address
            Case "$B$14"
              .Axes(xlCategory).MaximumScale = Target.Value
            Case "$B$15"
              .Axes(xlCategory).MinimumScale = Target.Value
            Case "$B$16"
              .Axes(xlCategory).MajorUnit = Target.Value
            Case "$C$14"
              .Axes(xlValue).MaximumScale = Target.Value
            Case "$C$15"
              .Axes(xlValue).MinimumScale = Target.Value
            Case "$C$16"
              .Axes(xlValue).MajorUnit = Target.Value
          End Select
        End With
    End Sub
  25. Bernardo –

    See my tutorial, How to Assign a Macro to a Button or Shape.

  26. I got it Jon. Works perfectly. Thank you so much for posting this. It makes such a huge improvement on this sheet I am working on. Much appreciated.

  27. Hi,

    I need to trigger this code based on changes happening from two drop down boxes (drop down boxes are form controls in excel). My axis values are dependent on selection from two drop down boxes and for some reason is not triggering this code when i select from the drop downs.

    Can this be done. Thanks for all your help

  28. Aakash –

    Even though the dropdown changes what appears in a cell, it doesn’t trigger a change, since the cell is linked to the dropdown. It’s like a formula that changes its output based on changing inputs does not trigger a change, since the underlying formula is unchanged.

    You can make a simple formula that links to the linked cell of the dropdown. When the dropdown changes, this cell will change, and the Calculate event will fire. Use this code to capture the event:

    Private Sub Worksheet_Calculate()
      ' code here to change axis scale
    End Sub
  29. Thanks for the reply. I tried using the code with Calculate instead of Change but I am thrown a compile error stating “procedure declaration does not match description of event or procedure having the same name”

    To further explain
    I have two worksheets – Charts and Data
    In Charts worksheet drop downs give an option to choose a commodity. Based on the choice, Data worksheet calculates the data. This calculated data is shown in a chart which is shown in the chart worksheet.

    My calculations for axis is also on the chart worksheet . And the axis is calculated using max and min functions linked to data worksheet.

    When i change options in Chart worksheet using the dropdown, the axis do no adjustment accordingly.

    Thanks for all your help

  30. Hi Jon,

    Thanks for this very informative post. I have a few questions that I was hoping you could help me with:

    What if we only wanted to customize the y-axis? i.e. the x-axis will just be dates so there’s no need for me to change those and I would prefer that Excel automatically graphs them as they are doing now. Is there a way to set x-axis values to “auto” or, better yet, ignore the x-axis altogether?

    Also, say I have 20 graphs on one page and would like to set it up so that I would have a code that would basically have separate y-axis input options for each graph (i saw in an earlier response you addressed multiple graphs but I think that was with the same set of y inputs). How would I go about doing that? And is there a way to have certain y-axis values set to “auto” and others set to the actual input options? (Again, similar to what I was looking for above).

    I’d really appreciate any help you can offer!


  31. Hi Jon,

    Your module Worksheet_Calculate really worked like a charm. Thanks. I used it in my dashboard to set scales.

    Now I’m facing some problems to set horizontal scale automaticaly scale for a scatter chart like this one:

    I know it has to do with chart type but I’m struggling to set min and max scale using calculated argument.

    Any Idea? thanks a lot

  32. The horizontal axis is the .Axes(xlCategory) axis of an XY Scatter plot, and this is included in the example code. I can’t tell what the axis settings are before changing them, since all that appear in your screenshot are zero and ­±100%.

  33. Hi Jon,

    Thanks for this, it’s great.

    I am having the exact same problem as Aakash – when I use calculate() to make the graphs update automatically (the max/min cells are based on a formula) I get the following error:
    Run-time error ‘424’:
    Object required

    Then when I debug, the following is highlighted yellow:
    Select Case Target.Address

    Please could you help me on this as I feel like I’m so close!!!

    Thanks a million!

  34. Calculate() doesn’t have a Target range.

Subscribe without commenting

Write a Comment

I welcome comments from my readers. If you have an opinion on this post, if you have a question, or if there is anything to add, I want to hear from you. Whether you agree or disagree, please join the discussion. If you want to include an image in your comment, post it on your own site or on one of the many free image sharing sites, and include a link in your comment. I'll download your image and insert the necessary html to display the image inline.

Subscribe without commenting

Peltier Tech Chart Utility


Create Excel dashboards quickly with Plug-N-Play reports.