任何人都可以帮助改进我的VBA代码吗?
我想用图表创建一个线图来绘制起点和终点坐标。
目前我必须复制,粘贴和修改每行的代码。
任何有关如何修改代码以允许任意行数的帮助都将非常受欢迎。
谢谢,
Sub creatmychart()
Dim Chart1 As Chart
Set Chart1 = Charts.Add
With Chart1
.ChartType = xlXYScatterLines
.SeriesCollection.NewSeries
.SeriesCollection(1).Name = "=members!$A2"
.SeriesCollection(1).XValues = "=members!$D$2,members!$F$2"
.SeriesCollection(1).Values = "=members!$E$2,members!$G$2"
.SeriesCollection.NewSeries
.SeriesCollection(2).Name = "=members!$A3"
.SeriesCollection(2).XValues = "=members!$D$3,members!$F$3"
.SeriesCollection(2).Values = "=members!$E$3,members!$G$3"
.SeriesCollection.NewSeries
.SeriesCollection(3).Name = "=members!$A4"
.SeriesCollection(3).XValues = "=members!$D$4,members!$F$4"
.SeriesCollection(3).Values = "=members!$E$4,members!$G$4"
.SeriesCollection.NewSeries
.SeriesCollection(4).Name = "=members!$A5"
.SeriesCollection(4).XValues = "=members!$D$5,members!$F$5"
.SeriesCollection(4).Values = "=members!$E$5,members!$G$5"
End With
End Sub
答案 0 :(得分:0)
只需创建一个循环。尝试以下(假设您的信息最多为11):
Sub creatmychart()
Dim Chart1 As Chart
Set Chart1 = Charts.Add
Dim i As Long
With Chart1
.ChartType = xlXYScatterLines
For i = 1 To 11
.SeriesCollection.NewSeries
.SeriesCollection(i).Name = "=members!$A" & i + 1
.SeriesCollection(i).XValues = "=members!$D$" & i + 1 & ",members!$F$" & i + 1
.SeriesCollection(i).Values = "=members!$E$" & i + 1 & ",members!$G$" & i + 1
Next i
End With
End Sub