こんにちは、masaです。
グラフの内容を変更するときは右クリックを使いがちだったり、操作性がいまいちだったりでイライラするなぁと感じる日々です。
今回は散布図(縦軸と横軸にそれぞれデータが入力されているグラフ)を自動生成するマクロを作ったのでメモがてら機能について紹介していこうと思います。
▽(B6:C22)、(E6:F22)、(I6:J22)、(L6:M22)にデータが入った状態でマクロを起動すると下にある散布図が自動的に作成されます。
結論:作ったマクロの全コード
いきなり結論ですがこんなコードです。
Sub 散布図の作成()
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Integer
'【a=2=BC列が座標、b=5=EF列が座標、c=9=IJ列が座標、d=12=LM列が座標】
'【B列、E列、I列、L列を下から検索して最深行を特定】
a = Cells(Rows.Count, 2).End(xlUp).Row
b = Cells(Rows.Count, 5).End(xlUp).Row
c = Cells(Rows.Count, 9).End(xlUp).Row
d = Cells(Rows.Count, 12).End(xlUp).Row
'一番上の行が6行目
E = 6
Range("a1").Select
'新規のグラフ作成
ActiveSheet.Shapes.AddChart2(240, xlXYScatter).Select
'【1本目の作図】
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(1).Name = "=""凡例1"""
ActiveChart.FullSeriesCollection(1).XValues = Range(Cells(E, 2), Cells(a, 2))
ActiveChart.FullSeriesCollection(1).Values = Range(Cells(E, 3), Cells(a, 3))
'黒線化
ActiveChart.SeriesCollection(1).Select
With Selection.Format.Line
.DashStyle = msoLineSolid
.ForeColor.ObjectThemeColor = msoThemeColorText1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
.Weight = 1
'マーカー無し
ActiveChart.SeriesCollection(1).Select
Selection.MarkerStyle = -4142
End With
'【2本目の作図】
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(2).Name = "=""凡例2"""
ActiveChart.FullSeriesCollection(2).XValues = Range(Cells(E, 5), Cells(b, 5))
ActiveChart.FullSeriesCollection(2).Values = Range(Cells(E, 6), Cells(b, 6))
'黒線化
ActiveChart.SeriesCollection(2).Select
With Selection.Format.Line
.DashStyle = msoLineSolid
.ForeColor.ObjectThemeColor = msoThemeColorText1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
.Weight = 1
'マーカー無し
ActiveChart.SeriesCollection(2).Select
Selection.MarkerStyle = -4142
End With
'【3本目の作図】
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(3).Name = "=""凡例3"""
ActiveChart.FullSeriesCollection(3).XValues = Range(Cells(E, 9), Cells(c, 9))
ActiveChart.FullSeriesCollection(3).Values = Range(Cells(E, 10), Cells(c, 10))
'赤線化
ActiveChart.SeriesCollection(3).Select
With Selection.Format.Line
.DashStyle = msoLineSolid
.ForeColor.RGB = RGB(255, 0, 0)
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
.Weight = 1
'マーカー無し
ActiveChart.SeriesCollection(3).Select
Selection.MarkerStyle = -4142
End With
'【4本目の作図】
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(4).Name = "=""凡例4"""
ActiveChart.FullSeriesCollection(4).XValues = Range(Cells(E, 12), Cells(d, 12))
ActiveChart.FullSeriesCollection(4).Values = Range(Cells(E, 13), Cells(d, 13))
'赤線化
ActiveChart.SeriesCollection(4).Select
With Selection.Format.Line
.DashStyle = msoLineSolid
.ForeColor.RGB = RGB(255, 0, 0)
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
.Weight = 1
'マーカー無し
ActiveChart.SeriesCollection(4).Select
Selection.MarkerStyle = -4142
End With
'①、②はグラフ選択状態でのみ使用可
'①軸ラベルの設定開始
yoko_jiku = "軸力N(kN)"
tate_jiku = "曲げモーメントM(kN・m)"
With ActiveChart
With .Axes(xlCategory, xlPrimary)
.HasTitle = True
.AxisTitle.Text = yoko_jiku '横軸(x軸)
End With
With .Axes(xlValue, xlPrimary)
.HasTitle = True
.AxisTitle.Text = tate_jiku '縦軸(y軸)
End With
End With
'①軸ラベルの設定終了
'②数値の整数表示開始
ActiveChart.Axes(xlCategory).Select '
Selection.TickLabels.NumberFormatLocal = "0_ "
ActiveChart.Axes(xlValue).Select
Selection.TickLabels.NumberFormatLocal = "0_ "
'②数値の整数表示終了
End Sub
各工程の説明
①事前準備:グラフの座標を指定の場所に貼っておく
それぞれのグラフの座標をBC列(6行目)、EF列(6行目)、IJ列(6行目)、LM列(6行目)に貼り付けておく必要があります。
以下のコードでその設定をしています。赤字の2、5、9、12を変更すれば参照先のセルを変更できます。
Sub 散布図の作成()
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Integer
'【a=2=BC列が座標、b=5=EF列が座標、c=9=IJ列が座標、d=12=LM列が座標】
'【B列、E列、I列、L列を下から検索して最深行を特定】
a = Cells(Rows.Count, 2).End(xlUp).Row
b = Cells(Rows.Count, 5).End(xlUp).Row
c = Cells(Rows.Count, 9).End(xlUp).Row
d = Cells(Rows.Count, 12).End(xlUp).Row
'一番上の行が6行目
E = 6
②散布図の作成
散布図を自動作成するコードは以下のとおりです。(1)が1本目の散布図を指しています。2や3は列を示しています。ちなみに2はB列、3はC列を示しています。
線の色は黒と赤があります。直下は黒線のコードで、その下に赤線のコードを書いておきます。
'新規のグラフ作成
ActiveSheet.Shapes.AddChart2(240, xlXYScatter).Select
'【1本目の作図】
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(1).Name = "=""凡例1"""
ActiveChart.FullSeriesCollection(1).XValues = Range(Cells(E, 2), Cells(a, 2))
ActiveChart.FullSeriesCollection(1).Values = Range(Cells(E, 3), Cells(a, 3))
'黒線化
ActiveChart.SeriesCollection(1).Select
With Selection.Format.Line
.DashStyle = msoLineSolid
.ForeColor.ObjectThemeColor = msoThemeColorText1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
.Weight = 1
'マーカー無し
ActiveChart.SeriesCollection(1).Select
Selection.MarkerStyle = -4142
End With
▽赤線のコード
'赤線化
ActiveChart.SeriesCollection(4).Select
With Selection.Format.Line
.DashStyle = msoLineSolid
.ForeColor.RGB = RGB(255, 0, 0)
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
.Weight = 1
③グラフの書式補正
グラフの書式補正として①縦軸横軸の軸ラベルを設定しました、②軸の数値を整数表示に設定しました。
'①、②はグラフ選択状態でのみ使用可
'①軸ラベルの設定開始
yoko_jiku = "軸力N(kN)"
tate_jiku = "曲げモーメントM(kN・m)"
With ActiveChart
With .Axes(xlCategory, xlPrimary)
.HasTitle = True
.AxisTitle.Text = yoko_jiku '横軸(x軸)
End With
With .Axes(xlValue, xlPrimary)
.HasTitle = True
.AxisTitle.Text = tate_jiku '縦軸(y軸)
End With
End With
'①軸ラベルの設定終了
'②数値の整数表示開始
ActiveChart.Axes(xlCategory).Select '
Selection.TickLabels.NumberFormatLocal = "0_ "
ActiveChart.Axes(xlValue).Select
Selection.TickLabels.NumberFormatLocal = "0_ "
'②数値の整数表示終了
End Sub
関連する記事のリンク