Series = 0的Excel VBA宏不显示值

时间:2015-08-04 03:33:58

标签: excel charts point series

我有这段代码可以帮助我编辑代码吗?只有.showvalue什么时候不同于0?

Sub test()
    Dim myChart As ChartObject 'A "ChartObject" contains a Chart, in which the "SeriesCollection" resides
    Dim chartSeries As Series 'Multiple "Series" can be found in a "SeriesCollection"
    Dim scPoint As Point

    Set myChart = ActiveSheet.ChartObjects("Chart 1")
    Set chartSeries = myChart.Chart.SeriesCollection(1) 'Get the first "Series" in the "SeriesCollection" for this chart

    'loop through the points in this Series. As it loops the point will be available in the "scPoint" variable. 
    For Each scPoint In chartSeries.Points
        With scPoint.DataLabel 'Finally the "DataLabel" is part of the "Point"
            .ShowValue = True 'Just show the value. There are other options here, just create a new line and start typing with a period to see what other options are available for a "DataLabel"
        End With
    Next scPoint

    'Instead of iterating, if you just want to address a single point's datalabel then:
    chartSeries.Points(2).DataLabel.ShowValue
End Sub

谢谢!

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

Sub test()

    Dim myChart As ChartObject
    Dim chartSeries As Series
    Dim pts As Points
    Dim vals, i

    Set myChart = ActiveSheet.ChartObjects("Chart 1")
    Set chartSeries = myChart.Chart.SeriesCollection(1) 'Get the first "Series" in the "SeriesCollection" for this chart

    Set pts = chartSeries.Points 'the points themselves
    vals = chartSeries.Values    'underlying (Y)Values (vs. XValues)

    chartSeries.ApplyDataLabels  'show labels on this series

    'loop over the points in the series
    For i = 1 To pts.Count
        'show/hide based on the point's corresponding (y)value
        pts(i).DataLabel.ShowValue = (vals(i) > 0)
    Next i

End Sub