2017-07-16 58 views
0

我有一個動態圖表,並且遇到了將數據從中清除的困難。在Excel中刪除圖表中的數據集VBA 2016

據我所知,數據庫指數是累積的。這意味着,如果我從另一張表格中複製了12張乾淨製作的數據庫中的「圖表4」。數據庫的數量= 12 = ActiveSheet.ChartObjects(「Chart 4」)。Chart.SeriesCollection.count。並且這些系列的索引從1到12.

現在,如果刪除1個系列和一個新系列,數據庫的數量將繼續爲12,但索引現在將從1-11和13 。

所以當我試圖刪除它們時,通過計算系列的數量並刪除帶有索引的系列1:ActiveSheet.ChartObjects(「Chart 4」)。Chart.SeriesCollection.count如果系列已經刪除並添加。

因此,爲了克服這個問題,我想「爲每個..在圖4 ..選項:

For Each Series In ActiveSheet.ChartObjects("Chart 4") 
'For Each FullSeriesCollection In ActiveSheet.ChartObjects("Chart 4") 
    ActiveChart.FullSeriesCollection.Delete 
Next 

對於我得到一個錯誤,指出: 」對象不支持此屬性或方法」

所以我看這個問題在這裏對堆棧溢出,發現由於圖表dataseries的指數的勤奮過程中,計數器需要往下走:

因此,我複製和調整VBA deleting chart series

Dim iSrs As Long 
With ActiveChart 
    For iSrs = .SeriesCollection.count To 1 Step -1 
     If InStr(LCase$(.SeriesCollection(iSrs).name), "series") > 0 Then 
      .SeriesCollection(iSrs).Delete 
     End If 
    Next 
End With 
MsgBox (count_non_existant_series & " and the nr of series still present = " & ActiveSheet.ChartObjects("Chart 4").Chart.SeriesCollection.count) 

這不會刪除所有數據系列,爲後來它仍然顯示:數= 27

我嘗試了其他幾個配方與不與「上錯誤刪除所有,(有時無論結果繼續對下一個」,它會刪除它的一半,捨去奇數時),以及完整的代碼是:

'select workbook, worksheet 
Workbooks("N.xlsm").Worksheets("day_visual").Activate 
Workbooks("N.xlsm").Worksheets("day_visual").range("A1").Select 
'select chart 
ActiveSheet.ChartObjects("Chart 4").Activate 
ActiveSheet.ChartObjects("Chart 4").Select 
'remove all series(0 to xx?) 
MsgBox (ActiveSheet.ChartObjects("Chart 4").Chart.SeriesCollection.count) 
'For Remove = 1 To ActiveSheet.ChartObjects("Chart 4").Chart.SeriesCollection.count 
' 'On Error Resume Next 
' ActiveChart.FullSeriesCollection(Remove).Select 
' Selection.Delete 
' 
' 'ActiveChart.FullSeriesCollection(Remove).Delete 
' 'MsgBox ("hi") 
' count_non_existant_series = 1 + count_non_existant_series 
'Next Remove 

ActiveSheet.ChartObjects("Chart 4").Activate 
ActiveSheet.ChartObjects("Chart 4").Select 
'For x = Workbooks("N.xlsm").Worksheets("day_visual").ChartObjects("Chart 4").SeriesCollection.count To 2 Step -1 
'For x = Workbooks("N.xlsm").Worksheets("day_visual").ChartObjects("Chart 4").FullSeriesCollection.count To 2 Step -1 
' ActiveSheet.ChartObjects("Chart 4").SeriesCollection(x).Delete 
'Next x 
Dim iSrs As Long 
With ActiveChart 
    For iSrs = .SeriesCollection.count To 1 Step -1 
     If InStr(LCase$(.SeriesCollection(iSrs).name), "series") > 0 Then 
      .SeriesCollection(iSrs).Delete 
     End If 
    Next 
End With 
'For Each Series In ActiveSheet.ChartObjects("Chart 4") 
For Each FullSeriesCollection In ActiveSheet.ChartObjects("Chart 4") 
    ActiveChart.FullSeriesCollection.Delete 
Next 


MsgBox (count_non_existant_series & " and the nr of series still present = " & ActiveSheet.ChartObjects("Chart 4").Chart.SeriesCollection.count) 

'With ActiveSheet.ChartObjects("Chart 4") 
''Do While .SeriesCollection.count >= 1 
'.SeriesCollection(.SeriesCollection.count).Delete 
'Loop 
'End With 
Dim add_chartlabels As Long 

我如何索引的確切性質的理解是用Excel存儲的欠缺,使我嘗試不當的解決方案。任何人都可以:

  1. 請告訴我,我對圖表系列索引 的理解是否不正確。
  2. 解釋爲什麼代碼在「for each」方法上產生運行時483錯誤 ?
  3. 解釋爲什麼手動迭代 代碼不會刪除所有系列?
  4. 顯示一個功能代碼, 從圖表中刪除任何和所有系列,同時不刪除 圖表本身?

或者如果您有任何其他貢獻帶來洞察力,您會讓我非常高興。

+0

#3 - 你只是刪除一系列**如果**系列名稱包含子串「系列」'。 –

+0

@ a.t。你有沒有嘗試過下面的答案?任何反饋 ? –

回答

1

運行483上的錯誤For Each方法 - 因爲使用這種方法意味着你是從第一個到最後一個循環。刪除對象時,您需要向後循環。因此,爲此目的,您需要使用For iSrs = .SeriesCollection.count To 1 Step -1

嘗試下面的代碼,在代碼中的解釋(作爲註釋):

Option Explicit 

Sub DeleteChartSer() 

Dim Sht As Worksheet 
Dim ChtObj As ChartObject 
Dim Ser As Series 
Dim iSrs As Long 

' set the worksheet object (this will work only if "Nutrition planner v42.xlsm" is open) 
Set Sht = Workbooks("Nutrition planner v42.xlsm").Worksheets("day_vita_visual") 

' set the ChartObject 
Set ChtObj = Sht.ChartObjects("Chart 4") 

MsgBox ChtObj.Chart.SeriesCollection.Count 

With ChtObj.Chart ' <-- there's no need to select the Chart, use fullay qualified objects instead 
    If .SeriesCollection.Count >= 0 Then 
     For iSrs = .SeriesCollection.Count To 1 Step -1 ' allways loop backwards when deleting objects 
      If LCase(.SeriesCollection(iSrs).Name) Like "*series*" Then 
       .SeriesCollection(iSrs).Delete 
      End If 
     Next iSrs 
    End If 
End With 

'MsgBox (count_non_existant_series & " and the nr of series still present = " & ActiveSheet.ChartObjects("Chart 4").Chart.SeriesCollection.Count) 

End Sub 

編輯1:如果你想刪除所有Series,只評一個If,下面的一個,因爲在這裏您檢查Series.Name是否包含作品「系列」:

If LCase(.SeriesCollection(iSrs).Name) Like "*series*" Then 

因此,將您的la st部分代碼與:

With ChtObj.Chart ' <-- there's no need to select the Chart, use fullay qualified objects instead 
    If .SeriesCollection.Count >= 0 Then 
     For iSrs = .SeriesCollection.Count To 1 Step -1 ' allways loop backwards when deleting objects 
      .SeriesCollection(iSrs).Delete 
     Next iSrs 
    End If 
End With 
+0

謝謝Shao Rado,它讓我的夜晚變得更有魅力!我將研究爲什麼你的負面步驟確實起作用,爲什麼我的負面步驟不起作用,無論是我的編程/打字錯誤,還是實際的代碼差異。 感謝您通過首先調用完全限定對象來向我展示ettiquette的改進。最重要的是一個手寫,工作的解決方案! 我打算給出1.和2.在我自己提出的這個問題中,一旦我認爲我完全理解它。 –

+0

'For Each srs In .SeriesCollection'不在意它向前發展,因爲它不使用索引,而是使用集合。 –

1

刪除if語句後,它將起作用。

With ActiveChart 
    If .SeriesCollection.count >0 then 
     For iSrs = .SeriesCollection.count To 1 Step -1 
     'If InStr(LCase$(.SeriesCollection(iSrs).name), "series") > 0 Then 
      .SeriesCollection(iSrs).Delete 
     'End If 
     Next 
    end if 
End With 
+0

謝謝!確實,我認爲Shai Rado的答案更完整一些,所以我接受了這個答案。然而,你也是符合資格的。我感謝你的貢獻和努力! –