无法删除Excel图表标签

时间:2019-01-09 21:22:34

标签: excel vba office365

我正在尝试删除Excel图表中每个系列的所有数据标签,但是我的代码不知何故不删除图表上的任何数据标签。请让我知道下面的代码中是否缺少某些内容。我当前正在使用Microsoft Office 365,文件是xlsb。

在此先感谢您,非常感谢您的帮助。

Sub DeleteDataLabels()
Sheets("Sheet1").Select
Call UnprotectSheet
ActiveSheet.ChartObjects("Chart 2").Activate


SeriesCount = ActiveChart.SeriesCollection.Count
MsgBox SeriesCount

' To delete the code
For i = 1 To SeriesCount
ActiveChart.SeriesCollection(i).Select
ActiveChart.ChartArea.Select
If ActiveChart.SeriesCollection(i).HasDataLabels Then
            ActiveChart.SeriesCollection(i).DataLabels.Select
            Selection.Delete
End If
Next i
End Sub

1 个答案:

答案 0 :(得分:1)

Thanks to @Rory's comment. Replace the if statement with the line of code below the comment.

 Sub DeleteDataLabels()

 Sheets("Sheet1").Select
 Call UnprotectSheet
 ActiveSheet.ChartObjects("Chart 2").Activate


 SeriesCount = ActiveChart.SeriesCollection.Count
 MsgBox SeriesCount

 'Replace the if statement with this line of code
 ActiveChart.ApplyDataLabels xlDataLabelsShowNone
 End Sub