[Excel VBA] How to Batch Update the X-Axis of Multiple Charts: Automate Manual Time Axis Adjustments

目次

Overview

Many people use multiple charts in Excel to visualize data and show changes over time. However, if the X-axis represents a “time axis,” you often need to change the period or adjust the intervals. Doing this manually for every single chart is very time-consuming.

In this article, I will introduce a method using Excel VBA to batch update the X-axis settings (minimum value, maximum value, and major unit) for all charts on a sheet at once.

Desired Specification

The goal is to automatically change the X-axis based on values entered in specific cells:

  • Cell A1: X-axis Minimum Value
  • Cell A2: X-axis Maximum Value
  • Cell A3: Major Unit (Interval)
  • Action: Clicking a “Run Button” (created with a Shape) updates the X-axis of all charts on the current sheet.

Implementation Code (VBA)

Paste the following code into a standard module and assign the macro to a shape.

Sub UpdateAxisMajorUnit()
    Dim chrt As ChartObject
    Dim minValue As Double
    Dim maxValue As Double
    Dim majorUnit As Double

    With ActiveSheet
        ' Read values from A1 to A3
        minValue = .Range("A1").Value
        maxValue = .Range("A2").Value
        majorUnit = .Range("A3").Value

        ' Update X-axis for all charts in the sheet
        For Each chrt In .ChartObjects
            With chrt.Chart.Axes(xlCategory, xlPrimary)
                .MinimumScale = minValue
                .MaximumScale = maxValue
                .MajorUnit = majorUnit
            End With
        Next chrt
    End With

    MsgBox "Completed.", vbInformation
End Sub

How to Use

  1. Enter the X-axis start value in Cell A1 (e.g., 0).
  2. Enter the X-axis end value in Cell A2 (e.g., 100).
  3. Enter the X-axis interval in Cell A3 (e.g., 10).
  4. Click the Run Button.

Just by doing this, the X-axis for all graphs will be updated instantly.

Application Tips

  • You can batch update the Y-axis (vertical axis) using similar syntax.
  • This is useful when you want to switch graph views while changing periods for time-series data.
  • If you assign different setting patterns to separate macro buttons, you can instantly switch between multiple period comparisons.

Summary

  • Manually adjusting the X-axis for multiple charts in Excel is inefficient.
  • Using VBA, you can apply X-axis settings in a batch using cell inputs and a single button.
  • This is ideal for automating dashboards and charts that use time axes.
  • This method is highly useful for anyone looking to streamline visualization tasks in Excel.
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次