奥が深い...
グラフ作成のサンプルをWebで探すと,
ActiveSheet.ChartObjects.Add Left:=0, Top:=0, Width:=320, Height:=240 ActiveChart.ChartType = xlXYScatter ActiveChart.ほげほげの設定 ...
こんなのがでてきます.
でも,動きません.
ActiveChartを使うと"Error 91"がでてきます.
ウンウンうなりながら,試行錯誤を繰り返したところ,
' コードを標準モジュールへ移動させた後...
Worksheets(sheetname).Select
' グラフ作成
With ActiveSheet
Dim rc As Range
Set rc = .Range(Cells(2, 1 + codeCalcNum * stepCol + 1), Cells(22, 1 + codeCalcNum * stepCol + 1 + 7))
Dim performChart As ChartObject
'Set performChart = Worksheets(sheetname).ChartObjects.Add(Left:=rc.Left, Top:=rc.Top, Width:=rc.Width, Height:=rc.Height)
Set performChart = .ChartObjects.Add(Left:=rc.Left, Top:=rc.Top, Width:=rc.Width, Height:=rc.Height)
' グラフ形式を指定し,データをセット
performChart.chart.ChartType = xlLineMarkers
'performChart.chart.SetSourceData Source:=.Range(Cells(2, 1), Cells(3, baseCol + codeCalcNum * stepCol - 1)), PlotBy:=xlRows
.Range(Cells(3, 1), Cells(3, baseCol + codeCalcNum * stepCol - 1)).Copy
performChart.chart.SeriesCollection.Paste
performChart.chart.SeriesCollection(1).XValues = .Range(Cells(2, baseCol), Cells(2, baseCol + codeCalcNum * stepCol - 1))
.Range(Cells(5, 1), Cells(5, baseCol + codeCalcNum * stepCol - 1)).Copy
performChart.chart.SeriesCollection.Paste
performChart.chart.SeriesCollection(2).XValues = .Range(Cells(2, baseCol), Cells(2, baseCol + codeCalcNum * stepCol - 1))
' タイトルを設定
performChart.chart.HasTitle = True
performChart.chart.ChartTitle.Text = "ほげほげ:" + CStr(ir * 100) + "%"
performChart.chart.Axes(xlCategory).HasTitle = True
performChart.chart.Axes(xlCategory).AxisTitle.Text = .Cells(2, 1).Value
performChart.chart.Axes(xlValue).HasTitle = True
performChart.chart.Axes(xlValue).AxisTitle.Text = .Cells(3, 1).Value
End Withこうすると正しく動作することがわかりました.
何なんだよ,いったい.もう疲れたよ...
でも,朝までには完成させないとなぁ...