ChartObjects の編集

by

in

 Excel で複数のグラフの様式を統一したい、というニーズが稀にあります。
 これをマウスの操作で行おうとすると、かなり神経をつかううえ時間がかかります。で、これを VBA で一気に処理しようというのが今回のネタです。

 実際の使ったコードを以下に例示します。
 今回は主に筆者の備忘用なので解説はなし! 上記のコードの不明な箇所、あるいは該当する処理がない場合は Excel のオブジェクト階層オンラインマニュアルと格闘してください。
 気に入るまで何度でもやり直しが利くので臆することなく楽をしましょう。ただし、自信のない処理を行う場合は一旦セーブすることをお忘れなく。

Public Sub Unify_Chart(num_s)

With ChartObjects("object_name")

    .Width = 420
    .Height = 450

    With .Chart

        With .PlotArea
            .Left = 20
            .Top = 20
            .Width = 350
            .Height = 400
        End With

        With .Axes(xlCategory)
            .HasTitle = True
            .MinimumScale = 0
            .MaximumScale = 216000
            .MajorUnit = 43200
            With .AxisTitle
                .Caption = "x"
                .Characters(1, 1).Font.Italic = True
                .Font.Size = 11
            End With
        End With

        With .Axes(xlValue)
            .HasTitle = True
            .MinimumScale = -3
            .MaximumScale = 0
            .MajorUnit = 0.5
            .Crosses = xlMinimum
            .TickLabels.NumberFormat = "0.0"
            With .AxisTitle
                .Caption = "y"
                .Characters(5, 1).Font.Italic = True
                .Left = 5
                .Font.Size = 11
            End With
        End With

        For itm = 1 To num_s
            With .SeriesCollection(itm)
                .MarkerSize = 2
            End With
        Next

        For itm = num_s + 1 To .SeriesCollection.Count
            With .SeriesCollection(itm)
                .Type = xlXYScatter
                With .Format.Line
                    .ForeColor.RGB = RGB(0, 0, 0)
                    .Weight = 0.25
                End With
            End With
        Next

        With .Legend
            .Top = 100
            .Left = 370
            For itm = .LegendEntries.Count To num_s + 1 Step -1
                .LegendEntries(itm).Delete
            Next
        End With

    End With
End With

End Sub