cellMatrix.net
Spreadsheet Modeling and Related Topics

About

Formulas, Charts, and Models Created with Microsoft Excel.
More . . .

Statistics

  • Total Entries - 102
  • Current Viewers - 9

Categories

Recent Comments

Syndicate

Validate

Automatically Set Min and Max Scale for Y-Axis

From Microsoft - Knowledge Base Article 213644:

In Microsoft Excel, you can set the Minimum and Maximum properties of the y-axis scale to any value. The following macro sets the minimum and maximum values for the y-axis scale in a chart to the maximum and minimum values of the data used to create the chart.

Sub SetScaleToMinAndMaxValues()
    Dim ValuesArray(), SeriesValues As Variant
    Dim Ctr As Integer, TotCtr As Integer
    With ActiveChart
        For Each X In .SeriesCollection
            SeriesValues = X.Values
            ReDim Preserve ValuesArray(1 To TotCtr + UBound(SeriesValues))
            For Ctr = 1 To UBound(SeriesValues)
                ValuesArray(Ctr + TotCtr) = SeriesValues(Ctr)
            Next
            TotCtr = TotCtr + UBound(SeriesValues)
        Next
        .Axes(xlValue).MinimumScaleIsAuto = True
        .Axes(xlValue).MaximumScaleIsAuto = True
        .Axes(xlValue).MinimumScale = Application.Min(ValuesArray)
        .Axes(xlValue).MaximumScale = Application.Max(ValuesArray)
    End With
End Sub

It you need to use "=NA()" as a source value, the macro above fails. However, you can modify the macro to allow the array to accept only numeric values. To do so, modify the For-Next procedure as follows:

For Ctr = 1 To UBound(SeriesValues)
    If IsNumeric(SeriesValues(Ctr)) Then
        ValuesArray(Ctr + TotCtr) = SeriesValues(Ctr)
    End If
Next



Comment Form

Name: (Required)
E-Mail Address: (Optional)
Location: (Optional)
Web Site Address: (Optional)

Remember my personal information?
Notify me of follow-up comments?

Before submitting your comment, please enter the phrase you see below: