2015-03-13 175 views
1

我想在Excel中繪製線條圖。Excel中的變量範圍VBA

我有X軸作爲一個常量範圍,但我試圖使Y軸變量範圍。我的數據是B2-F2,B3-F3,B4-F4,B5-F5,B6-F6,B7-F7。我想把它們中的每一個都繪製成Y軸和恆定的X軸範圍,但無法弄清楚如何讓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 

回答

0

你必須使用一個循環。像這樣的東西應該得到您關閉:

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軸沒有更新的即時問題。

+0

令人驚歎!謝謝! :) – Fiona 2015-03-13 17:22:14

+0

任何想法如何移動圖?我有這個:ActiveChart.ChartArea.Top = rowheight * rowno - 270 ActiveChart.ChartArea.Height = rowheight ActiveChart.ChartArea.Width = 3 * rowheight ...但這意味着所有6個圖移動到相同的位置,我只需要知道如何分開移動圖表?謝謝! :) – Fiona 2015-03-13 17:23:27