2015-12-01 16 views
0

當我使用下面的宏時,它僅適用於一個系列數據集。我怎樣才能適應它,以便我可以包含多個系列?如何使我的VBA適應包含第二系列數據

目前,如果我嘗試用它來標記第二組將刪除第一個等等...

在此先感謝

Sub AddXYLabels() 
    If Left(TypeName(Selection), 5) <> "Chart" Then 
     MsgBox "Please select the chart first." 
     Exit Sub 
    End If 
    Set StartLabel = _ 
    Application.InputBox("Click on the cell containing the first(top) label", Type:=8) 
    Application.ScreenUpdating = False 
    For Each pt In ActiveChart.SeriesCollection(1).Points 
     pt.ApplyDataLabels xlDataLabelsShowValue 
     pt.DataLabel.Caption = StartLabel.Value 
     Set StartLabel = StartLabel.Offset(1) 
    Next 
End Sub 
+0

請解釋一下目前你在做什麼,什麼是不工作,你喜歡才達到的。 – blckbird

+1

如果您訪問了SeriesCollection集合,而不是Points集合,它可能會工作。 您擁有的代碼只能訪問圖表上的第一個系列。 – Alex4336

+0

嗨,大家好,謝謝你的回覆。我想要做的是將標籤添加到氣泡圖上的每個點 - 但是,氣泡圖有多個系列。以上代碼僅適用於具有單個系列的圖表。 –

回答

1

您的代碼不工作的休息該系列既然你硬編碼的範圍內只有一個系列

For Each pt In ActiveChart.SeriesCollection(1).Points 

請試試這個

Sub AddXYLabels() 
 
If Left(TypeName(Selection), 5) <> "Chart" Then 
 
MsgBox "Please select the chart first." 
 
Exit Sub 
 
End If 
 
Dim mySeries As Series 
 
    Dim seriesCol As SeriesCollection 
 
    Dim I As Integer 
 
    I = 1 
 
    Set seriesCol = ActiveChart.SeriesCollection 
 
    For Each mySeries In seriesCol 
 
     Set mySeries = ActiveChart.SeriesCollection(I) 
 
     With mySeries 
 
      .ApplyDataLabels xlDataLabelsShowValue 
 
         
 
     End With 
 
     I = I + 1 
 
    Next 
 
End Sub

+0

謝謝:) - 你是完全正確的,我已經具體到第一個數據系列。 –

+0

您的歡迎:)如果它對你有用請投票它... – Linga

相關問題