我对VBA很新。我正在尝试编写一个输出33系列的代码。第一个系列的x值在A列中,第一个系列的y值在C列中。然后是D和F.然后是G和I ....依此类推。 (所以基本上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
答案 0 :(得分:0)
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