Master3’s blog

LaTeXやExcelVBAなどの作例集

ExcelVBA作例10(グラフの書式テンプレ)

  • 今回ご紹介するのは、任意のグラフの書式をちょっと美しくするマクロになります。

    Excelのデフォルトはこんな感じ

マクロ実行後はこんな感じ
  • まずは、てきとうなデータを使ってグラフを挿入します
  • 作成したグラフは、ひとつだけ選択されているようにします(複数個選択されているとマクロは実行されません)(選択しないでマクロを実行すると、シート内のすべてのグラフにマクロが適用されます)
  • 次のマクロを実行します

Option Explicit
Sub グラフ書式()

Dim chrtObj As ChartObject
Dim Obj As Object
Dim Obj2 As Object

Application.DisplayAlerts = False
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

Set Obj = Selection
If TypeName(Obj) <> "DrawingObjects" Then 'グラフが1つ選択された場合、選択されていない場合
    If Not ActiveChart Is Nothing Then 'グラフが1つ選択された場合
        With ActiveChart
            .HasTitle = False
        .PlotArea.Format.Fill.Visible = False
            With .ChartArea.Format.Line
                .Visible = msoFalse
            End With
            With .Axes(xlValue, 1).TickLabels
                .Font.Color = RGB(0, 0, 0) '文字色
                .Font.Size = 12 'サイズ
            End With
            With .Axes(xlValue)
                .HasTitle = True
               .AxisTitle.Font.Color = RGB(0, 0, 0)
                .AxisTitle.Format.TextFrame2.TextRange.Font.Size = 12 
     .MajorTickMark = xlInside '主目盛内側
                .MajorGridlines.Delete '目盛線消去
                .MinorTickMark = xlInside '補助目盛内側
                With .Format.Line
                    .Visible = msoTrue
                    .ForeColor.ObjectThemeColor = msoThemeColorText1
                    .ForeColor.TintAndShade = 0
                    .ForeColor.Brightness = 0
                    .Transparency = 0
                End With
            End With
            With .Axes(xlCategory).TickLabels
                .Font.Color = RGB(0, 0, 0) '文字色
                .Font.Size = 12 'サイズ
            End With
            With .Axes(xlCategory)
                .HasTitle = True
     .AxisTitle.Font.Color = RGB(0, 0, 0)
                .AxisTitle.Format.TextFrame2.TextRange.Font.Size = 12
                .MajorTickMark = xlInside '主目盛内側
                .MajorGridlines.Delete '目盛線消去
                .MinorTickMark = xlInside '補助目盛内側
                    With .Format.Line
                        .Visible = msoTrue
                        .ForeColor.ObjectThemeColor = msoThemeColorText1
                        .ForeColor.TintAndShade = 0
                        .ForeColor.Brightness = 0
                        .Transparency = 0
                    End With
            End With
    .ChartArea.Format.Fill.Transparency = 0.99
        End With
    Else 'グラフが選択されていない場合(全てのグラフを変更する)
        For Each chrtObj In ActiveSheet.ChartObjects
            With chrtObj.Chart
            .HasTitle = False
        .PlotArea.Format.Fill.Visible = False
            With .ChartArea.Format.Line
                .Visible = msoFalse
            End With
            With .Axes(xlValue, 1).TickLabels
                .Font.Color = RGB(0, 0, 0) '文字色
                .Font.Size = 12 'サイズ
            End With
            With .Axes(xlValue)
                .HasTitle = True
     .AxisTitle.Font.Color = RGB(0, 0, 0)
                .AxisTitle.Format.TextFrame2.TextRange.Font.Size = 12
                .MajorTickMark = xlInside '主目盛内側
                .MajorGridlines.Delete '目盛線消去
                .MinorTickMark = xlInside '補助目盛内側
                With .Format.Line
                    .Visible = msoTrue
                    .ForeColor.ObjectThemeColor = msoThemeColorText1
                    .ForeColor.TintAndShade = 0
                    .ForeColor.Brightness = 0
                    .Transparency = 0
                End With
            End With
            With .Axes(xlCategory).TickLabels
                .Font.Color = RGB(0, 0, 0) '文字色
                .Font.Size = 12 'サイズ
            End With
            With .Axes(xlCategory)
                .HasTitle = True
     .AxisTitle.Font.Color = RGB(0, 0, 0)
                .AxisTitle.Format.TextFrame2.TextRange.Font.Size = 12
                .MajorTickMark = xlInside '主目盛内側
                .MajorGridlines.Delete '目盛線消去
                .MinorTickMark = xlInside '補助目盛内側
                    With .Format.Line
                        .Visible = msoTrue
                        .ForeColor.ObjectThemeColor = msoThemeColorText1
                        .ForeColor.TintAndShade = 0
                        .ForeColor.Brightness = 0
                        .Transparency = 0
                    End With
            End With
    .ChartArea.Format.Fill.Transparency = 0.99
        End With
        Next
    End If
End If


End Sub

  • うまくいけば、グラフに変化が現れます
  • 以上です。最後までお読みいただきありがとうございます!