简体   繁体   English

宏以选择折线图中的最后一个数据点并添加数据标签

[英]Macro to select last data point in line chart and add a data label

I have a line chart with 60 different data series in Excel that updates once a week. 我在Excel中有一个包含60个不同数据系列的折线图,每周更新一次。 In this chart, I would like to program a macro that selects the most recent data points in the line charts and adds a data label in the format 100.00 (number with two decimal places). 在此图表中,我想编程一个宏,该宏选择折线图中的最新数据点并添加格式为100.00(带两位小数的数字)的数据标签。

I am now using the code below to delete all data labels: 我现在使用下面的代码删除所有数据标签:

Sub Delete_labels()

ActiveChart.ApplyDataLabels xlDataLabelsShowNone

End Sub

To add data labels, I would like to use a code as well. 要添加数据标签,我也想使用一个代码。 I have been experimenting with the code below: 我一直在尝试以下代码:

Sub Add_last_label()

ActiveChart.ApplyDataLabels xlDataLabelsShowLabel, , , , True, False, False, False, False

End Sub

However, this code adds data labels to each data point, creating a huge mess. 但是,此代码将数据标签添加到每个数据点,从而造成巨大混乱。 The string ", , , , True, False, False, False, False" must be adapted to only add the last data label in the chart, but I am unsure how. 字符串“,,,,True,False,False,False,False”必须经过修改,仅在图表中添加最后一个数据标签,但我不确定如何。

Does anyone have an idea how to data labels to the last point in each series in the format 100.00 (with two decimal points)? 有谁知道如何将标签的数据标注到每个系列的最后一点,格式为100.00(带两个小数点)?

I found the answer. 我找到了答案。 The code below works: 以下代码有效:

Sub Update_labels()

ActiveChart.ApplyDataLabels xlDataLabelsShowNone

Dim iSrs As Long
Dim iPts As Long
For iSrs = 1 To ActiveChart.SeriesCollection.Count
With ActiveChart.SeriesCollection(iSrs)
iPts = .Points.Count
With .Points(iPts)
.ApplyDataLabels
' .DataLabel.ShowSeriesName = -1
' .DataLabel.ShowLegendKey = -1

.DataLabel.NumberFormat = "#,##0.00"

'ActiveChart.SeriesCollection(1).DataLabels.NumberFormat = "#,##0.00"

End With
End With
Next

End Sub

Uncomment ShowSeriesName and ShowLegendKey to also show those items. 取消注释ShowSeriesName和ShowLegendKey也可以显示这些项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM