散点图代码输出太多系列 Excel VBA



我对VBA很陌生。我正在尝试编写一个将输出 33 个系列的代码。第一个系列的 x 值位于 A 列中,第一个序列的 y 值位于 C 列中。然后是 D 和 F。然后G和我....等等。(所以基本上 x 值和 y 值分别是从 A 和 C 开始的每三列)。这些值位于第 2 行到第 25 行中。

此外,每个系列的名称都在第一行,每第三列以 2 开头。

前 33 个数据点结果不错,但它继续产生一堆意外的数据点。我的循环有问题吗?就像我说的,我很新,所以这可能是非常明显的东西。谢谢!

这是我的代码:

Sub Scatter
Dim i As Int
Dim name As String

    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlXYScatterLinesNoMarkers
For i = 1 To 33
    name = Cells(1, 3 * i - 1)
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(i).name = name
    With Worksheets("Sheet1")
    ActiveSheet.SeriesCollection(i).XValues = .range(.Cells(2, 3 * i - 2), .Cells(25, 3 * i - 2))
    ActiveSheet.SeriesCollection(i).Values = .range(.Cells(2, 3 * i), .Cells(25, 3 * i))
    End With

Next i

End Sub
Sub Scatter()
Dim i As Integer
Dim cht As Chart, sht As Worksheet
    Set sht = ActiveSheet
    Set cht = sht.Shapes.AddChart().Chart
    cht.ChartType = xlXYScatterLinesNoMarkers
    For i = 1 To 33
        With cht.SeriesCollection.NewSeries()
            .Name = sht.Cells(1, (3 * i) - 1)
            .XValues = sht.Range(sht.Cells(2, (3 * i) - 2), sht.Cells(25, (3 * i) - 2))
            .Values = sht.Range(sht.Cells(2, 3 * i), sht.Cells(25, 3 * i))
        End With
    Next i
End Sub

最新更新