VBA Excel图表DataLabels显示运行时错误

时间:2018-02-06 13:10:03

标签: excel vba excel-vba

我正在尝试为两个不同的饼形图表编写子程序。一个在DataLabel ShowValue和ShowPercentage上正常工作但在第二个子例程中我得到运行时错误。这是代码:

Sub Grafik_5()

ActiveSheet.Shapes.AddChart2(251, xlPie).Select
ActiveChart.SetSourceData Source:=Sheets("Grafikler").Range("C7:C9")
ActiveChart.FullSeriesCollection(1).XValues = "=Grafikler!$A$7:$A$9"
With ActiveChart.Parent
    .Height = Range("C113:C123").Height
    .Width = Range("C113:E113").Width
    .Top = Range("c113").Top
    .Left = Range("c113").Left
End With
With ActiveChart.SeriesCollection(1).DataLabels
    .ShowValue = False
    .ShowPercentage = True
End With
End Sub

它在ActiveChart.SeriesCollection(1).DataLabels行

上出错

感谢所有帮助, 谢谢,

1 个答案:

答案 0 :(得分:0)

在使用它们之前,您需要ApplyDatalabels。这应该可以解决错误。

Sub Grafik_5()

    ActiveSheet.Shapes.AddChart2(251, xlPie).Select
    ActiveChart.SetSourceData Source:=Sheets("Grafikler").Range("C7:C9")
    ActiveChart.FullSeriesCollection(1).XValues = "=Grafikler!$A$7:$A$9"
    With ActiveChart.Parent
        .Height = Range("C113:C123").Height
        .Width = Range("C113:E113").Width
        .Top = Range("c113").Top
        .Left = Range("c113").Left
    End With
    With ActiveChart.SeriesCollection(1)
        .ApplyDataLabels
        .DataLabels.ShowValue = False
        .DataLabels.ShowPercentage = True
    End With
End Sub