VBA 使えるソースコード

つたないソースコードを載せます。これは、他人の書いたソースコードを読む練習に最適です。初心者の方は、どうしたらきれいになるかなど考えながら活用してください。

VBA 折れ線グラフをVBAで作ってみよう

Sub Macro7()
'
' Macro7 Macro
'

'
Dim x_title As String, y_title As String, data_title As String, title As String


data_title = InputBox(" データの名称を入力してください。")
x_title = InputBox("横軸の名称を入力してください。")
y_title = InputBox("縦軸の名称を入力してください。")
title = InputBox(" グラフタイトルを入力してください。")

Dim n As Integer: n = 0

Do Until Cells(n + 5, 1).Value = ""
n = n + 1
Loop

Cells(2, 7).Value = n

Range("B5:B" & n + 4).Select
ActiveSheet.Shapes.AddChart2(240, xlXYScatterSmooth).Select
ActiveChart.SetSourceData Source:=Range("Sheet1!$B$5:$B$14")
Application.CutCopyMode = False
Application.CutCopyMode = False
ActiveChart.FullSeriesCollection(1).Name = data_title
ActiveChart.FullSeriesCollection(1).XValues = "=Sheet1!$A$5:$A$" & n + 4
ActiveChart.SetElement (msoElementLegendRight)
ActiveChart.Axes(xlCategory, xlPrimary).HasTitle = True
ActiveChart.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = x_title
ActiveChart.Axes(xlValue, xlPrimary).HasTitle = True
ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = y_title
ActiveSheet.ChartObjects(1).Chart.HasTitle = True
ActiveSheet.ChartObjects(1).Chart.ChartTitle.Text = title
End Sub