Home » excel » Setting maximum and minimum values for x-axis in Excel

Setting maximum and minimum values for x-axis in Excel

Posted by: admin April 23, 2020 Leave a comment


I have a graph that has dates on the x-axis and I’m trying to set maximum and minimum values for this axis using an Excel VBA. I defined MinXAxis and MaxXAxis values in the same sheet and here is my code:

Sub UpdateChartAxes()

   With ActiveSheet.ChartObjects("Chart 1").Chart

      .Axes(xlCategory).MinimumScaleIsAuto = False
      .Axes(xlCategory).MinimumScale = Range("MinXAxis").Value
      .Axes(xlCategory).MaximumScaleIsAuto = False
      .Axes(xlCategory).MaximumScale = Range("MaxXAxis").Value

   End With

End Sub

When I run this code I get error 400 with no explanation about the error.
Any ideas what might be going wrong?

How to&Answers:

Axis.MinimumScale Property

Returns or sets the minimum value on the value axis. Read/write

You should be applying these properties to the value axis xlValue, not the Category axis.

Setting this property will also set MinimumScaleIsAuto = False so you won’t need those lines.

Added If you use a column, bar, line, etc., graph then these have a Value and a Category axis. You can only set the Minimum or Maximum for the Value axis. Even if you swap them around (x – y) there is still only one Value and one Category axis. Edited An exception to this is if dates are used as the Category axis, in which case Excel enables the Scale settings, as it recognises them as values.

If, instead, you use a Scatter graph, then this has both X-Values and Y-Values, and each of these value axes can be given maxima and minima.

By far the easiest way to prove all this is to double click on an axis and see whether the Axis Options allows you to set a minimum and maximum.


At the beginning of the code, add

On Error GoTo ShowErrDescription.

After End With and before End Sub, add the following code

Exit Sub
MsgBox Err.Description

This should at least give you a little more information about the error.