Excel VBA Series = 0 的宏不显示值

Excel VBA Macro for Series = 0 do not show value

我有这段代码,你能帮我编辑代码吗?只有 .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

谢谢!

尝试这样的事情:

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