2017-01-20 87 views
0

我在寫一個腳本來創建圖表,每行一個在我的表格中。這個想法是得到一個線圖來顯示歷史。使用vba腳本創建圖表

表看起來是這樣的:

date modem capacity used 
16/01/2017 PUT  20gb 54% 20/01/2017 73% 21/01/2017 80% 
16/01/2017 PATITIFA 20gb 73% 20/01/2017 79% 21/01/2017 90% 
16/01/2017 HAWAIKI 40gb 44% 20/01/2017 55% 21/01/2017 70% 
16/01/2017 NUI  60gb 48% 20/01/2017 61% 21/01/2017 80% 
16/01/2017 ITI  20gb 75% 20/01/2017 84% 21/01/2017 90% 
16/01/2017 huawei 20gb 37% 20/01/2017 45% 21/01/2017 60% 

正試圖創建表我的VBA腳本是在這裏:

Sub addchart() 
Dim chartPut, chartPatitifa, chartHawaiki, chartNui, chartIti, chartHuawei As Chart 
Dim iRow, iCol As Long 
Dim date1, date2, date3, date4, date5, date6 As String 
Dim conso1, conso2, conso3, conso4, conso5, conso6 As String 
Dim forfait As String 
Dim modem As String 


iRow = 2 
iCol = 1 


Do Until IsEmpty(Cells(iRow, 1)) 
    date1 = Cells(iRow, 1).Value 
    modem = Cells(iRow, 2).Value 
    forfait = Cells(iRow, 3).Value 
    conso1 = Cells(iRow, 4).Value 
    date2 = Cells(iRow, 5).Value 
    conso2 = Cells(iRow, 6).Value 
    date3 = Cells(iRow, 7).Value 
    conso3 = Cells(iRow, 8).Value 
    date4 = Cells(iRow, 9).Value 
    conso4 = Cells(iRow, 10).Value 
    date5 = Cells(iRow, 11).Value 
    conso5 = Cells(iRow, 12).Value 
    date6 = Cells(iRow, 13).Value 
    conso6 = Cells(iRow, 14).Value 

    Set chartPut = Charts.Add 
    With chartPut 
     .SetSourceData Source:=Range("STATS!$A$2:$F$2") 
     .SeriesCollection(1).Name = modem 
     'conso values 
     .SeriesCollection(1).Values = "conso1; conso2; conso3; conso4; conso5; conso6" 
     'date values 
     .SeriesCollection(1).XValues = "date1; date2; date3; date4; date5; date6 " 

    End With 

Loop 



End Sub 

的事情是,我不能讓「CONSO」和「日期」值作爲數據值被接受到表中。...... 有什麼想法?

+0

所以,這是一個*圖*你正在嘗試創建,而不是*表* –

回答

0

要爲曲線設置恆定值,請使用數組。

.SeriesCollection(1).Values = Array(conso1, conso2, conso3, conso4, conso5, conso6) 

.SeriesCollection(1).XValues = Array(date1, date2, date3, date4, date5, date6) 

編輯

所以,你想你的系列指那些細胞動態的,你所面臨的問題是,你不知道先驗的範圍內的最後一排。所有你需要的是設置你的範圍,並將其提供給圖表系列。

Sub addConsoCharts() 
    Dim iRow As Long, modem As String, dates As Range, consos As Range 

    Application.ScreenUpdating = False 
    Application.EnableEvents = False 
    On Error GoTo Finished 
    With Sheets("Sheet1") ' <-- Set to the name of your worksheet 
     iRow = 2 
     Do Until IsEmpty(.Cells(iRow, 1)) 
      modem = .Cells(iRow, 2).Value 
      Set dates = Union(.Cells(iRow, 1), .Cells(iRow, 5), .Cells(iRow, 7), .Cells(iRow, 9), .Cells(iRow, 11), .Cells(iRow, 13)) 
      Set consos = Union(.Cells(iRow, 4), .Cells(iRow, 6), .Cells(iRow, 8), .Cells(iRow, 10), .Cells(iRow, 12), .Cells(iRow, 14)) 
      With ThisWorkbook.Charts.Add 
       .ChartArea.ClearContents 
       .Name = "Conso_" & iRow 
       With .SeriesCollection.NewSeries 
        .Name = modem 
        .XValues = dates 
        .Values = consos 
       End With 
      .SeriesCollection.NewSeries.Values = Range("STATS!$A$2:$F$2") 

      End With 
      iRow = iRow + 1 
      Loop 
    End With 
Finished: 
    Application.ScreenUpdating = True 
    Application.EnableEvents = True 
End Sub 

Sub CleanupConsosCharts() ' <-- to delete all these charts if needed 
    Application.DisplayAlerts = False 
    Dim ch As Chart 
    For Each ch In ThisWorkbook.Charts 
     If ch.Name Like "Conso_*" Then ch.Delete 
    Next 
    Application.DisplayAlerts = True 
End Sub 

p.s.我已經添加了一個清理例程,您可能需要它來刪除這些圖表,並且由於它們很多,手動刪除它們將非常繁瑣。

+0

獲取數組但不是源代碼好: – did12345

+0

你是什麼意思?你想讓你的值是動態的嗎?細胞?從閱讀你的代碼,它看起來像你想硬編碼他們從現在的價值。看到'conso1 = Cells(iRow,4).Value' ... –

+0

是首先,我的意思是<< Source:= Range (「STATS!$ A $ 2:$ F $ 2」)>>應該是動態的,因爲在實際的代碼中它會被硬編碼到一定的範圍。其次,這些值將是理想的動態值,所以當值被添加到表中時,圖表只會顯示5個最新值 – did12345