在 Excel VBA 中确定点的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15122329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:03:15  来源:igfitidea点击:

Determining the value of a point in Excel VBA

excelvbacharts

提问by Joe

I am trying to have the points in a chart change color if they are within certain value paramaters (i.e., >1 is green, <1 is red, anything else is blue). I cannot determine how to get VBA to give me the value of any given point.

我试图让图表中的点在某些值参数范围内改变颜色(即,>1 是绿色,<1 是红色,其他任何东西都是蓝色)。我无法确定如何让 VBA 为我提供任何给定点的价值。

In this thread, previously answered, the answer (very helpful in other ways) indicates that points(num).value will return the value at the point. However, I am getting an error message doing this, and nowhere else online or in the VBA help can I find a method that corresponds to this. Has anyone else had any success with this?

此线程中,先前已回答,答案(在其他方面非常有帮助)表明 points(num).value 将返回该点的值。但是,我在执行此操作时收到一条错误消息,并且在其他任何在线或 VBA 帮助中都找不到与此对应的方法。有没有其他人在这方面取得过任何成功?

Here's the snippet of code giving me trouble:

这是给我带来麻烦的代码片段:

For Count = 1 To 7
    If Worksheets("Sheet1").ChartObjects("ChartName").Chart.SeriesCollection(1).Points(Count).Value > 1 Then
    '... do stuff

Because of the way the data are stored in the dataset, it would definitely be better to get the value from the chart directly. I could figure out a workaround using the dataset itself, but I would rather avoid that.

由于数据存储在数据集中的方式,直接从图表中获取值肯定会更好。我可以使用数据集本身找出解决方法,但我宁愿避免这种情况。

回答by Tim Williams

Sub Tester()

Dim cht As Chart, s As Series, p As Point
Dim vals, x As Integer

    Set cht = ActiveSheet.ChartObjects(1).Chart
    Set s = cht.SeriesCollection(1)

    vals = s.Values

    For x = LBound(vals) To UBound(vals)
      If vals(x) > 10 Then
        With s.Points(x)
            .MarkerBackgroundColor = RGB(255, 0, 0)
            .MarkerForegroundColor = RGB(255, 0, 0)
        End With
      End If
    Next x

End Sub