基本的に、これは記録されたマクロの一般的なバージョンであり、すべての列、すべての行をループします。
Sub PlotLineGraph()
Dim height As Long
Dim width As Long
Dim sourceWs As Worksheet
Dim targetWs As Worksheet
Dim targetWorksheetName As String
Dim targetRange As Range
Dim targetChart As Chart
Dim chartsPerRow As Long
Dim chartWidth As Double
Dim chartHeight As Double
'customize the parameters
chartsPerRow = 2
chartWidth = 300
chartHeight = 300
Set sourceWs = Worksheets("Sheet1") ' replace Sheet1 with the sheet name the data is stored ( Case sensitive)
targetWorksheetName = "Sheet2" ' the output sheet name
Set targetWs = Worksheets(targetWorksheetName)
'--------------------------------------------------------------------------------
'optional
'this part deletes all previous shapes on the target worksheet
For Each Shape In targetWs.Shapes
Shape.Delete
Next Shape
'--------------------------------------------------------------------------------
With sourceWs
height = .Cells(.Rows.Count, 1).End(xlUp).Row ' getting height of column A
width = .Cells(1, .Columns.Count).End(xlToLeft).Column 'getting columns of row 1
If width > 1 Then
For j = 2 To width
targetWs.Activate
targetWs.Shapes.AddChart.Select
ActiveChart.ChartType = xlLine
Set targetChart = ActiveChart
.Activate
Set targetRange = Union(.Range(.Cells(1, 1), .Cells(height, 1)), .Range(.Cells(1, j), .Cells(height, j)))
targetChart.SetSourceData Source:=targetRange
With targetChart.Parent
.height = chartHeight
.width = chartWidth
.Top = CLng((j - 2) / chartsPerRow) * chartHeight
.Left = ((j - 2) Mod chartsPerRow) * chartWidth
End With
Next j
End If
End With
End Sub