从图表 Excel 中仅删除 0 个值

问题描述

我一直试图从 Excel 图表中删除这些 0,我观看了多个使用 Format Control #,##0;-#,##0;;. 的视频以及 Google 上提供的不同内容,但没有任何反应。我已将 0 值替换为 nothing#

去提前取消选中Show a zero in cells that have zero values 然后转到选择数据源 => 隐藏和空单元格,但仍然没有任何反应。

问题仍未解决,有人可以帮我解决这个问题,我将非常感谢您的帮助。

如果可以通过 VBA 解决,请分享代码或手动解决方案。

enter image description here

我曾尝试编写记录代码,但我真的不知道该怎么做。

Sub Macro1()
'
' Macro1 Macro
'

'
    ActiveSheet.ChartObjects("Chart 16").Activate
    ActiveChart.FullSeriesCollection(3).Select
    ActiveChart.SetElement (msoElementDataLabelCenter)
    ActiveChart.FullSeriesCollection(3).DataLabels.Select
    ActiveChart.FullSeriesCollection(3).Points(1).DataLabel.Select
    Selection.Delete
    ActiveSheet.ChartObjects("Chart 16").Activate
    ActiveChart.FullSeriesCollection(3).DataLabels.Select
    ActiveChart.FullSeriesCollection(3).Points(9).DataLabel.Select
    Selection.Delete
    ActiveSheet.ChartObjects("Chart 16").Activate
    ActiveChart.FullSeriesCollection(1).Select
    ActiveChart.SetElement (msoElementDataLabelCenter)
    ActiveChart.FullSeriesCollection(1).DataLabels.Select
    ActiveChart.FullSeriesCollection(1).Points(9).DataLabel.Select
    Selection.Delete
    ActiveSheet.ChartObjects("Chart 16").Activate
    ActiveChart.FullSeriesCollection(2).Select
    ActiveChart.SetElement (msoElementDataLabelCenter)
    ActiveChart.FullSeriesCollection(2).DataLabels.Select
    ActiveChart.PlotArea.Select
    ActiveChart.FullSeriesCollection(2).DataLabels.Select
    ActiveChart.FullSeriesCollection(2).Points(9).DataLabel.Select
    Selection.Delete
    ActiveSheet.ChartObjects("Chart 16").Activate
    ActiveChart.FullSeriesCollection(2).DataLabels.Select
    ActiveChart.FullSeriesCollection(2).Points(8).DataLabel.Select
    Selection.Delete
    ActiveSheet.ChartObjects("Chart 16").Activate
    ActiveChart.FullSeriesCollection(2).DataLabels.Select
    ActiveChart.FullSeriesCollection(2).Points(6).DataLabel.Select
    Selection.Delete
    ActiveSheet.ChartObjects("Chart 16").Activate
    ActiveChart.FullSeriesCollection(2).DataLabels.Select
    ActiveChart.FullSeriesCollection(2).Points(5).DataLabel.Select
    Selection.Delete
    ActiveSheet.ChartObjects("Chart 16").Activate
    ActiveChart.FullSeriesCollection(3).DataLabels.Select
    ActiveChart.ChartArea.Select
End Sub

我已经尝试过这个,但它删除了所有标签而不是 0。

Sub chartth()

Sheet5.ChartObjects("Chart 16").Activate

With ActiveChart.SeriesCollection(1)
    For i = 1 To .Points.Count
        If .Points(i).HasDataLabel = False Then
            .Points(i).Select
            ActiveChart.SetElement (msoElementDataLabelShow)
                If .Points(i).DataLabel.Text = 0 Then
                    .Points(i).HasDataLabel = False
                    .Points(i).DataLabel.ShowValue = False
                End If
        ElseIf .Points(i).DataLabel.Text = 0 Then
            .Points(i).HasDataLabel = False
            .Points(i).DataLabel.ShowValue = False
        End If
    Next i
End With

With ActiveChart.SeriesCollection(2)
    For i = 1 To .Points.Count
        If .Points(i).HasDataLabel = False Then
            .Points(i).Select
            ActiveChart.SetElement (msoElementDataLabelShow)
                If .Points(i).DataLabel.Text = 0 Then
                    .Points(i).HasDataLabel = False
                    .Points(i).DataLabel.ShowValue = False
                End If
        ElseIf .Points(i).DataLabel.Text = 0 Then
            .Points(i).HasDataLabel = False
            .Points(i).DataLabel.ShowValue = False
        End If
    Next i
End With


With ActiveChart.SeriesCollection(3)
    For i = 1 To .Points.Count
        If .Points(i).HasDataLabel = False Then
            .Points(i).Select
            ActiveChart.SetElement (msoElementDataLabelShow)
                If .Points(i).DataLabel.Text = 0 Then
                    .Points(i).HasDataLabel = False
                    .Points(i).DataLabel.ShowValue = False
                End If
        ElseIf .Points(i).DataLabel.Text = 0 Then
            .Points(i).HasDataLabel = False
            .Points(i).DataLabel.ShowValue = False
        End If
    Next i
End With

解决方法

也许可以尝试以下操作:

Sub ApplyLabelsAndClearZeros(ByVal chrt As Chart)
    Dim ser As Series
    For Each ser In chrt.SeriesCollection
        ser.ApplyDataLabels
        
        Dim pnt As Point
        For Each pnt In ser.Points       
            If pnt.DataLabel.Text = "0" Then
                pnt.HasDataLabel = False
            End If          
        Next
    Next
End Sub

像下面这样称呼它:

Sub Test()
    ApplyLabelsAndClearZeros Sheet1.ChartObjects(1).Chart
End Sub