0

I'm writing a macro to create an XY Scatter chart in Excel 2010 but having some trouble. I am trying to assign .Name, .XValues, and .Value using a loop but whenever my macro completes there are several unneeded entries in the chart. Here is my code:

    For m = 4 To 36
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(m).Name = Range("A" & m)
    ActiveChart.SeriesCollection(m).XValues = Range("Q" & m)
    ActiveChart.SeriesCollection(m).Values = Range("R" & m)
    Next m

For some reason the first three series being added are M1:M2, N1:N2, and O1:O2 but I have no idea why. I only want data from columns A, Q, and R. Also at the bottom of the chart there are always ten addition series (e.g. Series1 through Series10). Does anyone know why this happening and what I can do to fix it?

4

1 に答える 1

1

以下のようなことを試してください: SeriesCollection インデックスに依存するよりも安全です。また、コードでチャートを作成している場合、デフォルトでは、現在選択されている範囲からデータが取得されます。安全のために、必要なデータを追加する前に、チャートを作成した直後にループして既存のシリーズを削除する必要があります。

Dim s 

For m = 4 To 36
    Set s = ActiveChart.SeriesCollection.NewSeries()
    With s
        .Name = Range("A" & m)
        .XValues = Range("Q" & m)
        .Values = Range("R" & m)
    End With
Next m
于 2013-04-01T19:20:38.187 に答える