gpt4 book ai didi

excel - 如果连续值高于基线值,则更改图表标记颜色

转载 作者:行者123 更新时间:2023-12-04 20:23:04 28 4
gpt4 key购买 nike

如果图表中的值由超过基线值 0.7 的 3 个连续尖峰组成,我目前正在创建一个 VBA 宏来更改图表标记的颜色。
例如,在下图中,我创建了一个宏,如果值高于基线值,则将所有标记颜色更改为红色,但如果有 3 个连续值高于基线值,则不会。
Example Chart
我的代码
这是我尝试过的 - 如果值超过 0.7,则将标记颜色更改为红色

Sub Tester()

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

Set cht = ThisWorkbook.Worksheets("mySheet3").ChartObjects("Chart 1").Chart
Set s = cht.SeriesCollection(1)
vals = s.Values

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

End Sub

最佳答案

为此,您需要使用窗口大小来检查总是连续检查 3 个点,如果它们高于基线,如果它们是着色的,并进一步移动 1 以检查连续的下 3 个点。

Option Explicit

Sub Tester()
Dim cht As Chart
Set cht = ThisWorkbook.Worksheets("mySheet3").ChartObjects("Chart 1").Chart

Dim s As Series
Set s = cht.SeriesCollection(1)

Dim vals As Variant
vals = s.Values

Const WindowSize As Long = 3
Dim Colorize As Boolean

Dim x As Long
For x = LBound(vals) To UBound(vals)
If x + WindowSize - 1 <= UBound(vals) Then
Colorize = True
Dim w As Long
For w = x To x + WindowSize - 1
If Not vals(w) > 0.7 Then
Colorize = False
Exit For
End If
Next w
If Colorize Then
For w = x To x + WindowSize - 1
With s.Points(w)
.MarkerBackgroundColor = RGB(255, 0, 0)
.MarkerForegroundColor = RGB(255, 0, 0)
End With
Next w
End If
End If
Next x
End Sub
enter image description here

关于excel - 如果连续值高于基线值,则更改图表标记颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67916195/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com