こちらが「エラー件数推移を時系列で折れ線グラフ化する」VBAサンプルです。
日付ごとにエラー件数を集計し、その推移を折れ線グラフで可視化します。
サンプルコード
Sub ErrorTrendLineChart()
Dim wsSrc As Worksheet, wsDst As Worksheet
Dim lastRow As Long, i As Long
Dim dict As Object
Dim key As Variant
Dim chartObj As ChartObject
' チェック結果シート(例:A列=日付、C列=エラー内容)
Set wsSrc = Sheets("チェック結果")
lastRow = wsSrc.Cells(wsSrc.Rows.Count, 1).End(xlUp).Row
' 集計用シートを準備
On Error Resume Next
Set wsDst = Sheets("エラー推移")
If wsDst Is Nothing Then
Set wsDst = Sheets.Add
wsDst.Name = "エラー推移"
Else
wsDst.Cells.Clear
End If
On Error GoTo 0
' Dictionaryで日付ごとに件数集計
Set dict = CreateObject("Scripting.Dictionary")
For i = 2 To lastRow
If IsDate(wsSrc.Cells(i, 1).Value) Then
If dict.exists(CDate(wsSrc.Cells(i, 1).Value)) Then
dict(CDate(wsSrc.Cells(i, 1).Value)) = dict(CDate(wsSrc.Cells(i, 1).Value)) + 1
Else
dict.Add CDate(wsSrc.Cells(i, 1).Value), 1
End If
End If
Next i
' 集計結果を出力(日付昇順にソート)
wsDst.Range("A1").Value = "日付"
wsDst.Range("B1").Value = "エラー件数"
i = 2
For Each key In dict.keys
wsDst.Cells(i, 1).Value = key
wsDst.Cells(i, 2).Value = dict(key)
i = i + 1
Next key
wsDst.Range("A2:B" & i - 1).Sort Key1:=wsDst.Range("A2"), Order1:=xlAscending
' 折れ線グラフ作成
Set chartObj = wsDst.ChartObjects.Add(Left:=250, Top:=50, Width:=500, Height:=300)
With chartObj.Chart
.ChartType = xlLineMarkers
.SetSourceData Source:=wsDst.Range("A1:B" & i - 1)
.HasTitle = True
.ChartTitle.Text = "エラー件数推移"
.Axes(xlCategory).HasTitle = True
.Axes(xlCategory).AxisTitle.Text = "日付"
.Axes(xlValue).HasTitle = True
.Axes(xlValue).AxisTitle.Text = "件数"
End With
MsgBox "エラー件数推移を折れ線グラフ化しました。"
End Sub
VB✅ 処理の流れ
- 「チェック結果」シートから日付を取得(A列想定)
- Dictionaryで日付ごとに件数を集計
- 「エラー推移」シートに日付と件数を出力
- 昇順ソートして時系列を整列
- 折れ線グラフを作成(日付を横軸、件数を縦軸)
💡 応用ポイント
- 複数のエラー種別ごとに系列を分ければ「種別別の推移比較」も可能
- グラフ種類を
xlLine→xlColumnClusteredに変えれば棒グラフ化もできる - 日付を「週単位」「月単位」に集計すれば長期トレンド分析に活用可能
👉 このマクロを使えば、エラー件数の増減を時系列で把握できるレポートを自動生成できます。


