2013-10-10 105 views
4

我的「圖表數據範圍」是='sheet1'!$A$1:$Z$10。我想製作一個VBA宏(或者如果有人知道我可以使用的公式,但我無法想出一個),以便在每次運行宏時將chart1的範圍的結束列增加1。所以基本上:VBA:修改圖表數據範圍

chart1.endCol = chart1.endCol + 1

,這是什麼使用ActiveChart或是否有更好的方法的語法?

回答

2

假設你要擴大範圍(通過添加一個額外的列)添加一個多觀察每個系列中,你圖(而不是添加一個新的系列),您可以使用此代碼:

Sub ChangeChartRange() 
    Dim i As Integer, r As Integer, n As Integer, p1 As Integer, p2 As Integer, p3 As Integer 
    Dim rng As Range 
    Dim ax As Range 

    'Cycles through each series 
    For n = 1 To ActiveChart.SeriesCollection.Count Step 1 
     r = 0 

     'Finds the current range of the series and the axis 
     For i = 1 To Len(ActiveChart.SeriesCollection(n).Formula) Step 1 
      If Mid(ActiveChart.SeriesCollection(n).Formula, i, 1) = "," Then 
       r = r + 1 
       If r = 1 Then p1 = i + 1 
       If r = 2 Then p2 = i 
       If r = 3 Then p3 = i 
      End If 
     Next i 


     'Defines new range 
     Set rng = Range(Mid(ActiveChart.SeriesCollection(n).Formula, p2 + 1, p3 - p2 - 1)) 
     Set rng = Range(rng, rng.Offset(0, 1)) 

     'Sets new range for each series 
     ActiveChart.SeriesCollection(n).Values = rng 

     'Updates axis 
     Set ax = Range(Mid(ActiveChart.SeriesCollection(n).Formula, p1, p2 - p1)) 
     Set ax = Range(ax, ax.Offset(0, 1)) 
     ActiveChart.SeriesCollection(n).XValues = ax 

    Next n 
End Sub 
+2

如果你要聲明的變量(你應該),你應該聲明爲正確類型:'暗淡我作爲整數, r爲Integer,n爲Integer,p1爲Integer,p2爲Integer'。你的聲明創建'i,r,n,p1'作爲'Variant' :) –

+0

@DavidZemens我其實並沒有意識到這一點。謝謝! –

+0

不用擔心!這是一個非常常見的錯誤,我認爲:) –

4

Offset function動態範圍使之成爲可能。

的樣本數據

enter image description here

步驟

  • 定義一個動態命名區域 =OFFSET(Sheet1!$A$2,,,1,COUNTA(Sheet1!$A$2:$Z$2)),並給它一個名字 mobileRange
  • 日GHT點擊圖
  • 點擊選擇數據

該屏幕會在下面圖例條目Edit

enter image description here

點擊(手機被選中)

enter image description here

  • 將系列值更改爲指向mobileRange命名的範圍。
  • 現在如果將來月份的數據添加到移動銷售中,它將自動反映在圖表中。
+6

+1,不需要宏。 –

+0

我同意這對單個數據系列更好,但不能在多個系列的「圖表數據範圍」中使用此功能。由於我有大約一百個系列,我寧願不這樣做。 – Stuart

2

假設你只運行一個Chart Selected的宏,我的想法是改變公式中每個Series的範圍。您可以將更改應用於工作表中的所有圖表。

UPDATE:是否更改代碼以適應屏幕截圖多個串聯

Sub ChartRangeAdd() 
    On Error Resume Next 
    Dim oCht As Chart, aFormulaOld As Variant, aFormulaNew As Variant 
    Dim i As Long, s As Long 
    Dim oRng As Range, sTmp As String, sBase As String 

    Set oCht = ActiveSheet.ChartObjects(1).Chart 
    oCht.Select 
    For s = 1 To oCht.SeriesCollection.count 
     sTmp = oCht.SeriesCollection(s).Formula 
     sBase = Split(sTmp, "(")(0) & "(<FORMULA>)" ' "=SERIES(" & "<FORMULA>)" 
     sTmp = Split(sTmp, "(")(1) ' "..., ..., ...)" 
     aFormulaOld = Split(Left(sTmp, Len(sTmp) - 1), ",") ' "..., ..., ..." 
     aFormulaNew = Array() 
     ReDim aFormulaNew(UBound(aFormulaOld)) 
     ' Process all series in the formula 
     For i = 0 To UBound(aFormulaOld) 
      Set oRng = Range(aFormulaOld(i)) 
      ' Attempt to put the value into Range, keep the same if it's not valid Range 
      If Err.Number = 0 Then 
       Set oRng = oRng.Worksheet.Range(oRng, oRng.Offset(0, 1)) 
       aFormulaNew(i) = oRng.Worksheet.Name & "!" & oRng.Address 
      Else 
       aFormulaNew(i) = aFormulaOld(i) 
       Err.Clear 
      End If 
     Next i 
     sTmp = Replace(sBase, "<FORMULA>", Join(aFormulaNew, ",")) 
     Debug.Print "Series(" & s & ") from """ & oCht.SeriesCollection(s).Formula & """ to """ & sTmp & """" 
     oCht.SeriesCollection(s).Formula = sTmp 
     sTmp = "" 
    Next s 
    Set oCht = Nothing 
End Sub 

樣本數據 - 初始

InitialData

第一次運行後:

FirstRun

第二輪:

SecondRun

第三輪:

ThirdRun

+0

打印輸出似乎正在工作(使用'MsgBox'),但圖表沒有更新。 – Stuart

+0

@Stuart代碼已更新爲在Activesheet中的第一個圖表上工作,如果它不起作用,請立即窗口輸出。 – PatricK

+0

由於某些原因,@Søren方法'Mid'功能對我無效。 @PatricK選項使用'分裂'救了我。 – ZygD