[英]Variable Range in Excel VBA
我正在嘗試在Excel中繪制折線圖。
我將x軸設置為恆定范圍,但是我試圖將Y軸設置為可變范圍。 我的數據是B2-F2,B3-F3,B4-F4,B5-F5,B6-F6,B7-F7。 我想以恆定的X軸范圍將它們作為Y軸繪制,但是無法弄清楚如何使Y軸數據可變。
我得到了6張以B2-F2為Y軸的圖表。
這是我到目前為止的內容:
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
您必須使用循環。 這樣的事情應該使您接近:
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
我認為圖形部分不太正確,因此希望您可以對其進行調整以獲得期望的結果,但這至少可以解決您Y軸不更新的直接問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.