2017-06-26 4 views
2

別のExcelワークブックにデータが格納されている2000以上のアイテムの個別のグラフを作成するために使用するマクロを作成しました。マクロはforループを通って新しいチャートを作成し、他のExcelワークブックのデータを使用してチャートシリーズを設定し、その後、すべてのフォーマット作業を行います。しかし、私はまだ各ループの後にシリーズ範囲を動的に更新しようとすると問題が発生しています。一連のサンプルは以下に見出される:Excel:forループ内のChart Seriesを動的に更新する

for i = 1 to Row.Count 
    ActiveChart.FullSeriesCollection(1).Values = _ 
    "='[Simplified Interactive - V2.xlsm]Maint. FDC'!$D$2:$BA$2" 
私は、第三のチャート使用する第2のチャートは、列3からのデータを使用するように、上記の静的範囲を変更し、それを動的にすることができるだろうか迷った

4行目からのデータは全範囲で順に処理されます。

リファレンスに対する全コード:

Sub Macro4() 


Dim ws, ws2 As Worksheet 
Dim graphName As String 
Dim i As Integer 
Dim srange As Range 
Dim grp As Chart 
Dim lw As Long 


Set ws = Sheets("Interactive Data") 
Set ws2 = Sheets("Graphs") 


    For i = 1 To 3 'Row.Count 

    ws2.Shapes.AddChart2(227, xlLine).Select 
    With ActiveChart 
     .Parent.Name = ws.Cells(i + 1, 1) 
    End With 
    ActiveChart.SeriesCollection.NewSeries 
    ActiveChart.FullSeriesCollection(1).Name = "=""Annual Inv.""" 
    ActiveChart.FullSeriesCollection(1).Values = _ 
     "='[Simplified Interactive - V2.xlsm]Maint. FDC'!$D$" & i + 1 & ":$BA$" & i + 1 '*****ADJUST THIS 
    ActiveChart.SeriesCollection.NewSeries 
    ActiveChart.FullSeriesCollection(2).Name = "=""Status Quo""" 
    ActiveChart.FullSeriesCollection(2).Values = _ 
     "='[Simplified Interactive - V2.xlsm]No Maint. FDC'!$D$" & i + 1 & ":$BA$" & i + 1 '*****ADJUST THIS 
    ActiveChart.FullSeriesCollection(2).XValues = "=Graphs!$A$1:$AW$1" 
    ActiveChart.Axes(xlValue).Select 
    ActiveChart.Axes(xlValue).ReversePlotOrder = True 
    ActiveChart.Axes(xlValue).Crosses = xlMaximum 
    ActiveChart.ChartArea.Select 
    ActiveChart.Axes(xlValue).Select 
    ActiveChart.Axes(xlValue).TickLabels.Font.Color = RGB(0, 0, 0) 
    ActiveChart.Axes(xlCategory).Select 
    ActiveChart.Axes(xlCategory).TickLabels.Font.Color = RGB(0, 0, 0) 
    ActiveChart.Axes(xlValue).MajorGridlines.Select 
    ActiveChart.SetElement (msoElementLegendRight) 
    ActiveChart.Legend.Select 
    ActiveChart.Legend.Select 
    Selection.Position = xlBottom 
    ActiveChart.Legend.LegendEntries(2).Select 
    ActiveChart.Legend.LegendEntries(1).Select 
    ActiveChart.Legend.Select 
    With Selection.Format.TextFrame2.TextRange.Font.Fill 
     .Visible = msoTrue 
     .ForeColor.RGB = RGB(0, 0, 0) 
     .Transparency = 0 
     .Solid 
    End With 
    ActiveChart.ChartTitle.Select 
    Selection.Characters.Text = "Degradation" 
    With Selection.Format.TextFrame2.TextRange.Font.Fill 
     .Visible = msoTrue 
     .ForeColor.RGB = RGB(0, 0, 0) 
     .Transparency = 0 
     .Solid 
    End With 
    ActiveChart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis) 
    ActiveChart.Axes(xlCategory).AxisTitle.Select 
    Selection.Format.TextFrame2.TextRange.Font.Bold = msoTrue 
    With Selection.Format.TextFrame2.TextRange.Font.Fill 
     .Visible = msoTrue 
     .ForeColor.RGB = RGB(0, 0, 0) 
     .Transparency = 0 
     .Solid 
    End With 
    ActiveChart.Axes(xlCategory, xlPrimary).AxisTitle.Text = "Year" 
    Selection.Format.TextFrame2.TextRange.Characters.Text = "Year" 
    With Selection.Format.TextFrame2.TextRange.Characters(1, 4).ParagraphFormat 
     .TextDirection = msoTextDirectionLeftToRight 
     .Alignment = msoAlignCenter 
    End With 
    With Selection.Format.TextFrame2.TextRange.Characters(1, 4).Font 
     .BaselineOffset = 0 
     .Bold = msoTrue 
     .NameComplexScript = "+mn-cs" 
     .NameFarEast = "+mn-ea" 
     .Fill.Visible = msoTrue 
     .Fill.ForeColor.RGB = RGB(0, 0, 0) 
     .Fill.Transparency = 0 
     .Fill.Solid 
     .Size = 10 
     .Italic = msoFalse 
     .Kerning = 12 
     .Name = "+mn-lt" 
     .UnderlineStyle = msoNoUnderline 
     .Strike = msoNoStrike 
    End With 
    ActiveChart.SetElement msoElementPrimaryValueAxisTitleBelowAxis 
    ActiveChart.Axes(xlValue).AxisTitle.Select 
    With Selection.Format.TextFrame2.TextRange.Characters(1, 10).Font.Fill 
     .Visible = msoTrue 
     .ForeColor.RGB = RGB(0, 0, 0) 
     .Transparency = 0 
     .Solid 
    End With 
    Selection.Format.TextFrame2.TextRange.Font.Bold = msoTrue 
    ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Condition" 
    Selection.Format.TextFrame2.TextRange.Characters.Text = "Condition" 
    With Selection.Format.TextFrame2.TextRange.Characters(1, 30).ParagraphFormat 
     .TextDirection = msoTextDirectionLeftToRight 
     .Alignment = msoAlignCenter 
    End With 
    With Selection.Format.TextFrame2.TextRange.Characters(1, 8).Font 
     .BaselineOffset = 0 
     .Bold = msoTrue 
     .NameComplexScript = "+mn-cs" 
     .NameFarEast = "+mn-ea" 
     .Fill.Visible = msoTrue 
     .Fill.ForeColor.RGB = RGB(0, 0, 0) 
     .Fill.Transparency = 0 
     .Fill.Solid 
     .Size = 10 
     .Italic = msoFalse 
     .Kerning = 12 
     .Name = "+mn-lt" 
     .UnderlineStyle = msoNoUnderline 
     .Strike = msoNoStrike 
    End With 
    With Selection.Format.TextFrame2.TextRange.Characters(9, 22).Font 
     .BaselineOffset = 0 
     .Bold = msoTrue 
     .NameComplexScript = "+mn-cs" 
     .NameFarEast = "+mn-ea" 
     .Fill.Visible = msoTrue 
     .Fill.ForeColor.RGB = RGB(0, 0, 0) 
     .Fill.Transparency = 0 
     .Fill.Solid 
     .Size = 10 
     .Italic = msoFalse 
     .Kerning = 12 
     .Name = "+mn-lt" 
     .UnderlineStyle = msoNoUnderline 
     .Strike = msoNoStrike 
    End With 
    ActiveChart.FullSeriesCollection(1).Select 
    With Selection.Format.Line 
     .Visible = msoTrue 
     .ForeColor.RGB = RGB(192, 0, 0) 
     .Transparency = 0 
    End With 
    ActiveChart.FullSeriesCollection(2).Select 
    With Selection.Format.Line 
     .Visible = msoTrue 
     .ForeColor.RGB = RGB(0, 112, 192) 
     .Transparency = 0 
    End With 
    ActiveChart.ChartArea.Select 
    ActiveChart.Shapes.AddTextbox(msoTextOrientationHorizontal, 53.6250393701, _ 
     113.1250393701, 76.5, 15.75).Select 
    Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = _ 
     "Status Quo" 
    Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 28).ParagraphFormat. _ 
     FirstLineIndent = 0 
    With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 28).Font 
     .NameComplexScript = "+mn-cs" 
     .NameFarEast = "+mn-ea" 
     .Size = 8 
     .Name = "+mn-lt" 
    End With 
    Selection.ShapeRange.ScaleWidth 1.568627451, msoFalse, msoScaleFromTopLeft 
    Selection.ShapeRange.ScaleHeight 1.0476190476, msoFalse, msoScaleFromTopLeft 
    Selection.ShapeRange.IncrementLeft -6 
    Selection.ShapeRange.IncrementTop 6 
    With Selection.ShapeRange.TextFrame2.TextRange.Font.Fill 
     .Visible = msoTrue 
     .ForeColor.RGB = RGB(0, 0, 0) 
     .Transparency = 0 
     .Solid 
    End With 
    ActiveChart.PlotArea.Select 
    ActiveChart.Shapes.Range(Array("TextBox 1")).Select 
    ActiveChart.Shapes.AddTextbox(msoTextOrientationHorizontal, 58.8750393701, _ 
     42.3750393701, 67.5, 12.75).Select 
    ActiveChart.Axes(xlValue).MajorGridlines.Select 
    ActiveChart.PlotArea.Select 
    ActiveChart.Shapes.AddTextbox(msoTextOrientationHorizontal, 67.8750393701, _ 
     45.6250393701, 104.25, 11.25).Select 
    Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = _ 
     "Optimal" 
    Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 17).ParagraphFormat. _ 
     FirstLineIndent = 0 
    With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 7).Font 
     .NameComplexScript = "+mn-cs" 
     .NameFarEast = "+mn-ea" 
     .Size = 8 
     .Name = "+mn-lt" 
    End With 
    With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(8, 10).Font 
     .BaselineOffset = 0 
     .NameComplexScript = "+mn-cs" 
     .NameFarEast = "+mn-ea" 
     .Size = 8 
     .Name = "+mn-lt" 
    End With 
    Selection.ShapeRange.ScaleHeight 1.4666666667, msoFalse, msoScaleFromTopLeft 
    Selection.ShapeRange.ScaleWidth 0.7553956835, msoFalse, msoScaleFromTopLeft 
    Selection.ShapeRange.ScaleHeight 1.0454545455, msoFalse, _ 
     msoScaleFromBottomRight 
    Selection.ShapeRange.IncrementLeft -22.5 
    Selection.ShapeRange.IncrementTop 12.75 
    Range("R16").Select 


    Next i 

End Sub 

答えて

1

私は上記の静的範囲を変更することができるだろうか疑問、及び第二のチャートは、第三の行3からのデータを使用するように、それは動的にしましたチャートは4行目からのデータを使用し、全範囲にわたってデータを使用します。

変数を使用すると?

rw = 2 

ActiveChart.FullSeriesCollection(1).Values = _ 
"='[Simplified Interactive - V2.xlsm]Maint. FDC'!$D$" & rw & ":$BA$" & rw 

そして、あなたはそれがあなたのForループに "接続" したい場合は代わりにrw変数を使用する、すなわち、あなたがiを使用したい、その後

ActiveChart.FullSeriesCollection(1).Values = _ 
"='[Simplified Interactive - V2.xlsm]Maint. FDC'!$D$" & i & ":$BA$" & i 

EDIT

グラフ最初のグラフの点のみをプロットしますが、グラフ2以降のグラフはプロットしません。データは存在するので、それがなぜそれを行うのだろうか? - Xcelrate

このコードは私の3つのグラフのすべてをプロットしています。ところで、Activechartの使用を避けてください。 Objectsで作業してください。ここでは、コードがどのように見えるかの非常に基本的な例を示します。

Sub Sample() 
    Dim ws As Worksheet 
    Dim objChrt As ChartObject 
    Dim myChart As Chart 
    Dim chartTop As Long 

    Set ws = Sheets("Graphs") 

    '~~> This will define the "Left" of the chart 
    chartleft = 10 

    For i = 1 To 3 
     Set objChrt = ws.ChartObjects.Add(chartleft, 10, 200, 200) 
     Set myChart = objChrt.Chart 

     With myChart 
      .SeriesCollection.NewSeries 
      .FullSeriesCollection(1).Name = "Test" 
      .FullSeriesCollection(1).Values = "='Maint. FDC'!$D$" & i + 1 & ":$BA$" & i + 1 
     End With 

     chartleft = chartleft + 220 
    Next i 
End Sub 

ワークシートMaint。 FDC

enter image description here

ワークシートのグラフ

enter image description here

興味深い読む

ChartObjects.Add Method

+0

は、だから私は= "'にそれを変更'[Simplified Interactive - V2.xlsm] Maint。 FDC '!$ D $ "&i + 1&":$ BA $ "&i + 1'になりましたが、グラフは最初のグラフの点のみをプロットしますが、グラフ2以降のグラフはプロットしません。データが存在するので、それがなぜ起こっているのでしょうか? – Xcelrate

+0

あなたは 'ActiveChart'を言及しているので、アクティブなチャートはそれを変更します。 –

+0

私はすべてのループの始めに 'ws2.Shapes.AddChart2(227、xlLine).Select'を使って新しいグラフを作成します。ここでws2はグラフを作成しているワークシートです。だから私は範囲を反復する新しいチャートはアクティブなものだと思いますか? – Xcelrate

関連する問題