【问题标题】:Unable to read series point values无法读取系列点值
【发布时间】:2021-12-18 17:59:16
【问题描述】:

我正在尝试识别0 下的系列点,然后将条置于红色。

以下循环应该可以解决问题,但它会引发错误:

For i = 1 To cht.FullSeriesCollection(1).Points.Count
        If (cht.FullSeriesCollection(1).Values(i) < 0) Then cht.FullSeriesCollection(1).Points(i).Format.ForeColor.RGB = RGB(255, 0, 0)
    Next i

cht 在哪里:

Set cht = Ws.ChartObjects("Chart 5").Chart

抛出的错误是:

property let 过程未定义且 property get 过程未返回对象

当执行停止时,以下代码行高亮显示:

cht.FullSeriesCollection(1).Values(i)

我也试过了:

cht.SeriesCollection(1).Values(i) < 0

数据集: X轴=

18.3 
11.5
6.1 
4.2
1.8

Y 轴 =

-1.2
-4.8
-9.6
 17.8 
 0.9

【问题讨论】:

    标签: excel vba charts


    【解决方案1】:

    你很接近;问题在于.Points(i).Format.ForeColor.RGB,应该是:

    .Points(i).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
    

    为了提高可读性,请尝试以下操作:

    Dim cht As Chart
    Set cht = Ws.ChartObjects("Chart 5").Chart
    
    Dim s As Series
    Set s = cht.FullSeriesCollection(1)
    
    Dim i As Long
    For i = 1 To s.Points.Count
        If s.Values(i) < 0 Then
            s.Points(i).Format.Fill.ForeColor.RGB = vbRed
        End If
    Next
    

    示例图表:

    示例图表 2:

    【讨论】:

    • 还是一样的错误,好像If (cht.SeriesCollection(1).Values(i) &lt; 0)抛出错误
    • 我无法重现这个。使用示例条形图添加了非常适合我的代码。我也会避免内联If
    • 我在帖子中添加了数据集
    • 尝试将If 更改为多行语法,如我的回答所示。是否仍然有错误,如果有,哪一行会抛出它?
    • If cht.FullSeriesCollection(1).Values(i) &lt; 0 Then 抛出错误
    猜你喜欢
    • 2021-12-18
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多