2016-05-05 16 views
2

Excelで以下のコードを書き、XY散布図を作成し、ポイントをフォーマットして、特定の期間内に行われたイベント/決定のビジュアルタイムラインを作成します(sDateおよびeDate)。ループ内のifステートメントを使用すると、無効なパラメータエラーが発生する

Option Explicit 

Sub UpdateTimeline() 

'Updates timeline chart with dates in specified range and updates formatting 

    Dim timelineChart As Chart 
    Dim recordCount As Long 
    Dim record As Range 

    Application.ScreenUpdating = False 
    timelineSheet.Unprotect 
    Call ClearSeries 
    Set timelineChart = Worksheets("Timeline").ChartObjects("chtDecisionTimeline").Chart 
    recordCount = 0 
    For Each record In Range(decisionRecordSheet.Range("D7"), decisionRecordSheet.Range("D7").End(xlDown)) 
     recordCount = recordCount + 1 
     If record.Value >= timelineSheet.Range("sDate") Then 
      If record.Value <= timelineSheet.Range("eDate") Then 
       timelineChart.SeriesCollection.NewSeries 
       With timelineChart.SeriesCollection(recordCount) 
        .Name = "='Decision Record'!" & record.Offset(0, 1).Address 
        .XValues = "='Decision Record'!" & record.Address 
        .Values = "='Decision Record'!" & record.Offset(0, -2).Address 
        .AxisGroup = 2 
        .MarkerStyle = 8 
        .MarkerSize = 7 
        .MarkerBackgroundColor = RGB(228, 10, 56) 
        .MarkerForegroundColor = -2 
        .Format.Line.Visible = False 
        .ApplyDataLabels 
        .DataLabels.ShowValue = False 
        .DataLabels.ShowSeriesName = True 
        .DataLabels.Orientation = xlUpward 
         If record.Offset(0, -2).Value Mod 2 <> 0 Then 
          timelineChart.SeriesCollection(recordCount).DataLabels.Position = xlLabelPositionRight 
         Else 
          timelineChart.SeriesCollection(recordCount).DataLabels.Position = xlLabelPositionLeft 
         End If 
       End With 
      End If 
     End If 
    Next 
    timelineChart.SetElement (msoElementSecondaryValueAxisNone) 
    With timelineChart.Axes(xlCategory, xlPrimary) 
     .MaximumScale = timelineSheet.Range("eDate").Value 
     .MinimumScale = timelineSheet.Range("sDate").Value 
    End With 
    With timelineChart.Axes(xlValue, xlPrimary) 
     .MaximumScale = lookupSheet.Range("yMax").Value 
     .MinimumScale = lookupSheet.Range("yMin").Value 
    End With 
    With timelineSheet 
     .Protect DrawingObjects:=True, Contents:=True, Scenarios:=True 
     .EnableSelection = xlUnlockedCells 
    End With 

    Application.ScreenUpdating = True 

End Sub 

decisionRecordSheetが列BFで、次の形式でデータが含まれています

Image showing layout of decisionRecordSheet data

そして、次のようにtimelineSheetが出て設定されている:

enter image description here

コードが正常に動作します(私はそれがかなりではないことは分かっていますが)、ユーザーが指定した日付範囲( sDateおよびeDate)は、0-2レコードだけをグラフにプロットする必要があることを意味します。その場合、With timelineChart.SeriesCollection(recordCount)の無効なパラメータエラーがスローされます。

私は18,19,42行目と43行目をコメントアウトしていますが、問題の原因となっている条件を満たすレコード数を減らすif文には表示されます。私はまた、0-2しかないように、合計データセットを減らそうとしましたが、同じエラーが発生します。

もちろん、これはプロットされているレコードの数とは関係ありませんが、これらはこの動作を一貫して複製した唯一のテストです。

EDIT:

プロットする2つの以上の項目がある場合、私は、実行時エラーを取得するが、私は自分のコードにライン18,19,42および43を含んで持っている場合にのみ - 私はそれらをコメントする場合アウト、私はもはやエラーを取得します。

Error message

私はデバッグするとき、問題がtimelineChart.SetElement (msoElementSecondaryValueAxisNone)であることが表示されますが、私は、なぜ分かりません。

答えて

0

あなたは私たちがここに持っていませんが、コードを呼び出すように私はそれを複製することはできません: -

あなたはtimelineChart.SeriesCollection.NewSeriesの後に来る、または

または

変更を変更するにはrecordCount = recordCount + 1を移動した場合timelineChart.SeriesCollection(recordCount)~timelineChart.SeriesCollection(timelineChart.SeriesCollection.Count)

これは、肯定的な結果をもたらし得る。コレクションを見つけるために使用されていたrecordCountは毎回増分されましたが、毎回シリーズが作成されないことを意味するifという文があります。

関連する問題