Excel VBA仅显示每个系列的最大值的标签

编程入门 行业动态 更新时间:2024-10-10 07:30:22
本文介绍了Excel VBA仅显示每个系列的最大值的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图创建一个宏,它在图表中循环遍历,只显示最大/最小标签取决于max / min值。

I am trying to create a macro which loops through series in a chart and only shows the maximum / minimum label dependent on what the max / min value is.

一些系列将只有负值,在这些情况下,我希望只显示最小数据点标签,反之亦然,对于0或更大的值。

Some of the series will only have negative values and in these cases I wish to only show the minimum datapoint label, and vice versa for series with 0 or greater values.

代码我到目前为止:

Sheets("Curve").ChartObjects("Chart 14").Activate For Each serie In ActiveChart.SeriesCollection Dim pointCount As Integer Dim pointValues As Variant pointCount = serie.Points.Count pointValues = serie.Values For pointIndex = 1 To pointCount If pointValues(pointIndex) < 1000 Then serie.Points(pointIndex).HasDataLabel = True End If Next pointIndex Next serie End Sub

当我手动输入阈值,但我想替换为'1000'与Max(系列)值,

Which works fine when I manually enter the threshold, but I want to replace the '1000' with Max(series) value instead, so that each series in the chart has only one label visible.

推荐答案

以下修改的例程包括MaxPoint,MaxPointIndex,MinPoint和MinPointIndex变量,在 For 循环中计算每个系列的点。

The following modified routine includes MaxPoint, MaxPointIndex, MinPoint, and MinPointIndex variables which are calculated in the For loop on each serie's points. It then sets the label for the maximum point if the series has only positive value and minimum point otherwise.

Option Explicit Sub chart() Dim serie As Variant Dim Pointindex As Long Dim MaxPoint As Long Dim MaxPointIndex As Long Dim MinPoint As Long Dim MinPointIndex As Long Sheets("Curve").ChartObjects("Chart 14").Activate For Each serie In ActiveChart.SeriesCollection Dim pointCount As Integer Dim pointValues As Variant pointCount = serie.Points.Count pointValues = serie.Values MinPoint = 10000 'set to value greater than any point in any serie MaxPoint = 0 For Pointindex = 1 To pointCount If pointValues(Pointindex) > MaxPoint Then MaxPoint = pointValues(Pointindex) MaxPointIndex = Pointindex ElseIf pointValues(Pointindex) < MinPoint Then MinPoint = pointValues(Pointindex) MinPointIndex = Pointindex End If Next Pointindex If MinPoint >= 0 Then serie.Points(MaxPointIndex).HasDataLabel = True Else serie.Points(MinPointIndex).HasDataLabel = True End If Next serie End Sub

更多推荐

Excel VBA仅显示每个系列的最大值的标签

本文发布于:2023-10-28 10:38:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1536400.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最大值   标签   系列   Excel   VBA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!