Access 2007:通过带有0值的vba隐藏图表对象上的数据标签?

时间:2014-09-19 17:15:39

标签: vba charts access-vba ms-access-2007

我在Access 2007中有一个表单,其中包含一个根据当前日期动态生成的堆积条形图对象,并输出图表的PDF。

一切都生成并正常工作,但正在发生的事情是即使对于具有Null或0值的系列也会应用数据标签。这导致各个地方的文本混乱。

我正在寻找一种通过VBA去除属于没有值的系列的任何标签的方式。

我已经尝试从SQL查询中排除空值,并设置格式选项,以便0值不会显示。我已经尝试循环遍历该系列并在值为>时应用标签。 0,但如果我将其设置为应用系列名称,它仍会将其设置为空值。

enter image description here

编辑当前代码:

Option Compare Database

Private Sub Form_Load()
Dim tstChart As Graph.Chart

   On Error GoTo Form_Load_Error

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"

    For Each srs In .SeriesCollection
        For Each pt In srs.Points
            pt.DataLabel.Text = "Y"
        Next
    Next
End With

   On Error GoTo 0
   Exit Sub

Form_Load_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Form_Load of VBA Document Form_testing"

End Sub

我能够更改每个标签,但我似乎无法找到检查系列点中每个点的方法。

编辑: 的解决 (简单,但工作正常)

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

2 个答案:

答案 0 :(得分:2)

您使用的是Graph.Chart而不是Chart。他们对你可以做的事情更加有限,这是我所害怕的。但也许这可以帮助。

我们的想法是首先确保显示系列数据标签。

一旦我们知道它们被显示,就迭代这些点并根据它的DataLabel.Text属性选择性地操纵点DataLabel.Text属性。我假设此处显示的值为0,您只是想隐藏标签0,并且对其他标签不执行任何操作。

在你的程序中,我们将调用另一个sub来执行此操作:

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

因此,代码现在将调用另一个子过程:

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

答案 1 :(得分:2)

已解决:(简单,但工作正常)感谢您的帮助!

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