ソース
Sub graf_drow_day()
'変数の型を宣言
Dim x_ax As Range
Dim y_ax1 As Range
Dim y_ax2 As Range
Dim x_title As String
Dim y_title As String
Dim GRF_name As String
'ワークシートを取得する
Dim ws As Worksheet
Set ws = ActiveSheet
'データ範囲を指定する
Dim lastRow As Long
lastRow = ws.Cells(ws.Rows.Count, 3).End(xlUp).Row
Set x_ax = ws.Range("C2:C" & lastRow)
Set y_ax1 = ws.Range("D2:D" & lastRow)
Set y_ax2 = ws.Range("E2:E" & lastRow)
'横軸縦軸の名前を指定する
x_title = "Date"
y_title = "Value"
'横軸縦軸の文字サイズを指定する
Dim ax_fontsize As Integer
ax_fontsize = 14
'グラフ作成のための変数を宣言する
Dim startDate As Date
Dim endDate As Date
Dim currentDate As Date
Dim chartIndex As Integer
'グラフ作成の開始日と終了日を設定する
startDate = WorksheetFunction.Min(x_ax)
endDate = WorksheetFunction.Max(x_ax)
'日ごとのグラフを作成する
currentDate = startDate
chartIndex = 1
Do While currentDate <= endDate
'グラフのタイトルを指定する
GRF_name = "daily-graf (" & Format(currentDate, "yyyy/mm/dd") & ")"
'日付に対応するデータを抽出する
Dim rngX As Range
Dim rngY1 As Range
Dim rngY2 As Range
Set rngX = x_ax.Cells.Find(What:=currentDate, LookIn:=xlValues, LookAt:=xlWhole)
If Not rngX Is Nothing Then
Set rngY1 = y_ax1.Cells(rngX.Row - y_ax1.Row + 1)
Set rngY2 = y_ax2.Cells(rngX.Row - y_ax2.Row + 1)
'グラフを挿入する
Dim chart As Shape
Set chart = ws.Shapes.AddChart2(240, xlXYScatter)
chart.Name = "Chart" & chartIndex
With chart.chart
'指定したデータでグラフ作成
.SeriesCollection.NewSeries
.SeriesCollection(1).XValues = rngX
.SeriesCollection(1).Values = rngY1
'グラフの名前、タイトル、軸を変更する
.HasTitle = True
.ChartTitle.Text = GRF_name
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Text = x_title
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Text = y_title
.Axes(xlCategory).AxisTitle.Format.TextFrame2.TextRange.Font.Size = ax_fontsize
.Axes(xlValue).AxisTitle.Format.TextFrame2.TextRange.Font.Size = ax_fontsize
'2つ目のデータ系列を追加する
.SeriesCollection.NewSeries
.SeriesCollection(2).XValues = rngX
.SeriesCollection(2).Values = rngY2
'系列2のグラフの種類を折れ線グラフに設定する
.SeriesCollection(2).ChartType = xlLine
'系列2のデータポイントにマーカーを表示する
.SeriesCollection(2).MarkerStyle = -4142 'xlMarkerStyleNone
'グラフの範囲を自動調整する
.AutoScaling = True
End With
'次の日に進む
currentDate = currentDate + 1
chartIndex = chartIndex + 1
Else
'次の日に進む
currentDate = currentDate + 1
End If
Loop
End Sub