【问题标题】:Excel VBA chart, show data label on last point onlyExcel VBA图表,仅在最后一点显示数据标签
【发布时间】:2014-05-13 13:06:56
【问题描述】:

我只想将数据标签添加到折线图上的最后一点,目前我正在使用下面的方法,它工作正常,但前提是我知道最后一点是什么数字。 我做了很多搜索,并在 excel 帮助中找到了 points(points.count) 对象,但我似乎无法让它对我有用。 请您建议一种仅在我的图表上显示最后一点或(理想情况下)在工作表上显示所有图表的方法。

Sub Data_Labels()
'
' Data_Labels Macro

    ActiveSheet.ChartObjects("Menck Chart").Activate
    ActiveChart.SeriesCollection(1).DataLabels.Select
    Selection.Delete
    ActiveSheet.ChartObjects("Menck Chart").Activate
    ActiveChart.SeriesCollection(1).Select
    ActiveChart.SeriesCollection(1).Points(59).Select
    ActiveChart.SeriesCollection(1).Points(59).ApplyDataLabels
    ActiveChart.SeriesCollection(1).DataLabels.Select
    Selection.Format.TextFrame2.TextRange.Font.Size = 9


End Sub

【问题讨论】:

  • 您是否尝试过 For |每个语句?

标签: excel vba label


【解决方案1】:

简答

 Dim NumPoints as Long
 NumPoints = ActiveChart.SeriesCollection(1).Count
 ActiveChart.SeriesCollection(1).Points(NumPoints).ApplyDataLabels

长答案

ActiveChart 的使用很模糊,需要额外的步骤来选择感兴趣的图表。如果您明确指定您感兴趣的图表,您的宏将更加健壮且更易于阅读。我还建议使用With 块,或者创建中间变量,因为一遍又一遍地阅读ActiveChart.SeriesCollection(1).Points 会很痛苦并且会使您的代码混乱。尝试后一种方法如下:

 Dim chartMenck As Chart, menckPoints as Points, menckDataLabel as DataLabel
 Set chartMenck = Sheet1.ChartObjects("Menck Chart").Chart 
 Set menckPoints  = chartMenck SeriesCollection(1).Points
 menckPoints(menckPoints.Count).ApplyDataLabels
 Set menckDataLabel = menckPoints(menckPoints.Count).DataLabel
 menckDataLabel.Font.Size = 9

在我看来,这几乎是原版的一半,而且更容易阅读。

【讨论】:

    【解决方案2】:

    试试这个。首先它将数据标签应用于所有点,然后从除最后一个点之外的每个点中删除它们。

    我使用Points.Count - 1 这样For/Next 循环在最后一点之前停止。

    Sub Data_Labels()
    '
    Data_Labels Macro
    Dim ws As Worksheet
    Dim cht as Chart
    Dim srs as Series
    Dim pt as Point
    Dim p as Integer
    Set ws = ActiveSheet
    Set cht = ws.ChartObjects("Menck Chart")
    Set srs = cht.SeriesCollection(1)
        '## Turn on the data labels
        srs.ApplyDataLabels
        '## Iterate the points in this series
        For p = 1 to srs.Points.Count - 1 
            Set pt = srs.Points(p)
            '## remove the datalabel for this point
            p.Datalabel.Text = ""
        Next
        '## Format the last datalabel to font.size = 9
        srs.Points(srs.Points.Count).DataLabel.Format.TextFrame2.TextRange.Font.Size = 9
    
    
    End Sub
    

    【讨论】:

      【解决方案3】:

      VBA 中的另一种方式(例如,粘贴为个人工作簿中的新热键宏):https://peltiertech.com/Excel/Charts/LabelLastPoint.html

      对于不耐烦,用 ShowValue:=True:

      Option Explicit
      
      Sub LastPointLabel()
        Dim mySrs As Series
        Dim iPts As Long
        Dim bLabeled As Boolean
        If ActiveChart Is Nothing Then
          MsgBox "Select a chart and try again.", vbExclamation, "No Chart Selected"
        Else
          For Each mySrs In ActiveChart.SeriesCollection
            bLabeled = False
            With mySrs
              For iPts = .Points.count To 1 Step -1
                If bLabeled Then
                  ' handle error if point isn't plotted
                  On Error Resume Next
                  ' remove existing label if it's not the last point
                  mySrs.Points(iPts).HasDataLabel = False
                  On Error GoTo 0
                Else
                  ' handle error if point isn't plotted
                  On Error Resume Next
                  ' add label
                  mySrs.Points(iPts).ApplyDataLabels _
                      ShowSeriesName:=True, _
                      ShowCategoryName:=False, _
                      ShowValue:=True, _
                      AutoText:=True, LegendKey:=False
                  bLabeled = (Err.Number = 0)
                  On Error GoTo 0
                End If
              Next
            End With
          Next
        End If
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-24
        • 2013-05-19
        • 2020-11-07
        • 2021-11-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多