VBA 数据透视图数据标签未出现

VBA Pivot Chart data labels not appear

目前我正在尝试显示我的数据透视图的数据标签。我不知道为什么它没有按预期出现。这是我的编码:

Sub Dimmaer()
    Sheets("Closed INC Table").Select
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlColumnClustered
    ActiveChart.SetSourceData Source:=Range(Range("A1").CurrentRegion.Select)
    ActiveChart.SetElement (msoElementDataLabelCenter)
End Sub

这是我得到的:

同时它应该是这样的:

请注意,我不知道表格的范围,因为该行会不时更改。 我该如何解决这个问题?谢谢!

您可以手动强制使用数据标签,而不是依赖图表样式在创建时包含它们

Sub Dimmaer()
    Sheets("Closed INC Table").Select
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlColumnClustered
    ActiveChart.SetSourceData Source:=Range(Range("A1").CurrentRegion.Address)
    ActiveChart.SeriesCollection(1).ApplyDataLabels   '<~~ This
    'optionally set position
    ActiveChart.SeriesCollection(1).DataLabels.Position = xlLabelPositionCenter
    ActiveChart.SeriesCollection(2).ApplyDataLabels   '<~~ And this
    'optionally set position
    ActiveChart.SeriesCollection(2).DataLabels.Position = xlLabelPositionCenter
    ActiveChart.SetElement (msoElementDataLabelCenter)
End Sub

如果数据标签已经是图表的一部分,则这些标签将无效;例如他们不会被关闭。