2016-12-16 403 views
0

我有一個宏,它將數據標籤添加到氣泡圖中。此代碼非常適合提供Y軸的值,現在我想將其更改爲顯示SeriesName。VBA添加具有系列名稱的數據標籤到氣泡圖

Sub AddDataLabels() 

Dim bubbleChart As ChartObject 
Dim mySrs As Series 
Dim myPts As Points 

With ActiveSheet 
    For Each bubbleChart In .ChartObjects 
     For Each mySrs In bubbleChart.Chart.SeriesCollection 
      Set myPts = mySrs.Points 
      myPts(myPts.Count).ApplyDataLabels Type:=xlShowValue 
     Next 
    Next 
End With 

End Sub 

我嘗試改變

myPts(myPts.Count).ApplyDataLabels Type:=xlShowValue 

分爲:

myPts(myPts.Count).ApplyDataLabels Type:=xlShowSeriesName 

但它給了我一個 '無效的過程調用或參數'。

有關如何更改代碼以顯示SeriesName而不是Y軸值的任何建議?

Screenshot

回答

0

添加With語句下面我的代碼你的代碼中,並調整根據自己的需要裏面的參數。

在下面的代碼Daralabels將顯示SeriesName,但不是類別或值。

Sub AddDataLabels() 

Dim bubbleChart As ChartObject 
Dim mySrs As Series 
Dim myPts As Points 

With ActiveSheet 
    For Each bubbleChart In .ChartObjects 
     For Each mySrs In bubbleChart.Chart.SeriesCollection 
      Set myPts = mySrs.Points 

      myPts(myPts.Count).ApplyDataLabels 

      With myPts(myPts.Count).DataLabel 
       .ShowSeriesName = True 
       .ShowCategoryName = False 
       .ShowValue = False 
       ' optional parameters 
       .Orientation = 0 
       .Font.Size = 10 
       .Font.Bold = True 
      End With 

     Next 
    Next 
End With 

End Sub 
+0

令人驚歎的是,這件作品像一個魅力!謝謝! –

+0

@RobinEdsmyr your'e歡迎,請標記爲「答案」(通過在我的帖子旁標記一個** V **) –

+0

完成!再次感謝。 –

0

這是否適合您?

bubbleChart.ApplyDataLabels xlDataLabelsShowLabel

+0

不幸的不是。刪除'myPts(myPts.Count).ApplyDataLabels類型:= xlShowValue'並添加'bubbleChart.ApplyDataLabels xlDataLabelsShowLabel'它會給出錯誤。但是,將'myPts(myPts.Count).ApplyDataLabels類型:= xlShowValue'更改爲'myPts(myPts.Count).ApplyDataLabels類型:= xlDataLabelsShowLabel',它給出了x軸的值而不是Y軸的值。但不是系列名稱。 –

+0

奇怪。它用我的測試泡泡圖表爲我工作。可能會在稍後查看它。另一點:我調查'myPts(myPts.Count).'會導致錯誤,因爲'myPts'的索引從0開始,並且您嘗試使用索引'myPts.Count'訪問單個項目(顯然計數從1開始) –