Excel VBA中的variables范围

我正试图在Excel中绘制线条图。

我有X轴作为一个恒定的范围,但我想使Y轴的可变范围。 我的数据是B2-F2,B3-F3,B4-F4,B5-F5,B6-F6,B7-F7。 我想绘制每个这些作为我的常量X轴范围的Y轴,但无法弄清楚如何获得Y轴数据是可变的。

我用B2-F2作为Y轴得到6张图。

这是我迄今为止:

Dim rowno As Integer Dim colno As Range Dim time As Range Dim pressure As Range Dim Startrow As Integer Dim Lastrow As Integer Startrow = 2 Lastrow = 7 Set time = Range("B1:F1") ' THIS LINE HERE IS THE LINE IM STRUGGLING WITH Set pressure = "B"&Startrow&":"&"F"&Lastrow ActiveSheet.Shapes.AddChart.Select ActiveChart.ChartType = xlLine ActiveChart.SeriesCollection.NewSeries ActiveChart.SeriesCollection(1).XValues = time ActiveChart.SeriesCollection(1).Values = pressure 

你必须使用一个循环。 像这样的东西应该让你closures:

 Sub test() Dim rowno As Integer Dim colno As Range Dim time As Range Dim pressure As Range Dim Startrow As Integer Dim Lastrow As Integer Dim i As Long Startrow = 2 Lastrow = 7 Set time = Range("B1:F1") For i = 2 To 7 Set pressure = Range("B" & i & ":F" & i) ActiveSheet.Shapes.AddChart.Select ActiveChart.ChartType = xlLine ActiveChart.SeriesCollection.NewSeries ActiveChart.SeriesCollection(1).XValues = time ActiveChart.SeriesCollection(1).Values = pressure Next i End Sub 

我不认为图表部分是非常正确的,所以希望您能够调整以获得预期的结果,但这至less可以解决Y轴不会更新的问题。