Access 2007: Hide Data Labels on Chart Object via vba with 0 values?

我们两清 提交于 2019-12-06 09:18:13

You are using a Graph.Chart instead of a Chart. They are more limited in what you can do with them, which is what I was afraid of. But perhaps this can help anyways.

The idea is to first ensure that the series data labels are being displayed.

Once we know they are displayed, iterate the points and selectively manipulate the point's DataLabel.Text property, based on it's DataLabel.Text property. I'm assuming the value here being displayed is 0, and that you simply want to hide labels if it's 0, and do nothing to the other labels.

Within your procedure we will call another sub to do this:

Set tstChart = [Forms]!testing!barEquip.Object
With tstChart
    .HasTitle = True
    .ChartTitle.Font.Size = 14
    .ChartTitle.Text = VBA.Strings.MonthName(VBA.DatePart("m", VBA.Date()) - 1) & " " &     VBA.DatePart("yyyy", VBA.Date()) & _
                        " Test Title"

    Call AdjustDataLabels(tstChart) 'Call a procedure to modify the labels as needed

End With

So that code will now call on another sub-procedure:

Sub AdjustDataLabels(cht As Graph.Chart)

Dim srs As Graph.Series
Dim pt As Graph.Point
Dim vals As Variant

For Each srs In cht.SeriesCollection
    'First, ensure the dataLabels are ON
    srs.ApplyDataLabels
    For Each pt In srs.Points
        'Now, check the datalabels one by one, testing for your criteria
        If pt.DataLabel.Text = " some condition " Then
            'Criteria met, so blank out this datalabel
            'pt.HasDataLabel = False
            'OR:
             pt.DataLabel.Text = vbNullString

        Else
            'If you need to make any adjustments to other labels, 
            ' you can do that here.
            ' For example you could simply append the series name.
            ' Modify as needed.
            pt.DataLabel.Text = pt.DataLabel.Text & " -- " & srs.Name


        End If
    Next
Next
End Sub

SOLVED: (Simple, but works fine) Thanks for all the help!

Sub AdjustDataLabels(cht As Chart)

Dim srs As Series
Dim pt As Point
Dim vals As Variant

For Each srs In cht.SeriesCollection
    'Apply Value labels
    srs.ApplyDataLabels (xlDataLabelsShowValue)

    For Each pt In srs.Points
        'Check for empty labels
        If pt.DataLabel.Text = "" Then
           'Do nothing
        Else
           'Add Series Name then remove Value
           pt.DataLabel.ShowSeriesName = True
           pt.DataLabel.ShowValue = False
        End If
    Next
Next
End Sub
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!