February 15, 2016

Excel VBAで配列を渡してグラフを作成する

VBAの計算結果を手軽にグラフで確認するための関数。データを渡すと棒グラフを作成します。ChartTypeなどを調整すれば好みのグラフを生成する関数に応用できます。

Sub plotDataColumn(ByRef XData As Variant, ByRef YData As Variant, Optional XAxisTitle As String = "X Axis", Optional YAxisTitle As String = "Y Axis", Optional ChartTitle As String = "Chart Title", Optional GapWidth As Long = 10)
    With Charts.Add
        .ChartType = xlColumnClustered
        
        With .SeriesCollection.NewSeries
            .Values = YData
            .XValues = XData
        End With
        
        .ChartGroups(1).GapWidth = GapWidth
        
        .HasTitle = True
        .ChartTitle.Text = ChartTitle
        .Axes(xlCategory, xlPrimary).HasTitle = True
        .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = XAxisTitle
        .Axes(xlValue, xlPrimary).HasTitle = True
        .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = YAxisTitle
    End With
End Sub

No comments :

Post a Comment