我想创建一个用于Excel 2016的宏,它将数据添加到图表中。我需要帮助创建循环,此时我手动编写它,因为我创建循环的尝试还没有成功。长路代码如下所示:
Sub Button1_Click()
ActiveSheet.Shapes.AddChart2(240, xlXYScatterSmoothNoMarkers).Select
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(1).Name = "='TO DP Compressor
Maps'!$A$3"
ActiveChart.FullSeriesCollection(1).XValues = _
"='TO DP Compressor Maps'!$B$4:$B$23"
ActiveChart.FullSeriesCollection(1).Values = _
"='TO DP Compressor Maps'!$C$4:$C$23"
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(2).Name = "='TO DP Compressor
Maps'!$D$3"
ActiveChart.FullSeriesCollection(2).XValues = _
"='TO DP Compressor Maps'!$E$4:$E$23"
ActiveChart.FullSeriesCollection(2).Values = _
"='TO DP Compressor Maps'!$F$4:$F$23"
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(3).Name = "='TO DP Compressor
Maps'!$G$3"
ActiveChart.FullSeriesCollection(3).XValues = _
"='TO DP Compressor Maps'!$H$4:$H$23"
ActiveChart.FullSeriesCollection(3).Values = _
"='TO DP Compressor Maps'!$I$4:$I$23"
End Sub
如您所见,该模式会重演。有人可以帮我设置一个循环吗?
非常感谢
答案 0 :(得分:1)
例如:
Sub Button1_Click()
ActiveSheet.Shapes.AddChart2(240, xlXYScatterSmoothNoMarkers).Select
Dim i As Long
With ActiveChart
For i = 1 To 3
With .SeriesCollection.NewSeries
.name = "='TO DP Compressor Maps'!" & Range("$A$3").Offset(, (i - 1) * 3).Address
.XValues = "='TO DP Compressor Maps'!" & Range("$B$4:$B$23").Offset(, (i - 1) * 3).Address
.values = "='TO DP Compressor Maps'!" & Range("$B$4:$B$23").Offset(, (i - 1) * 3 + 1).Address
End With
Next
End With
End Sub