[英]Change chart marker color if there is continuous value higher than a baseline value
我目前正在創建一個 VBA 宏來更改圖表標記的顏色,如果圖表中的值包含超過基線值 0.7 的 3 個連續尖峰。
例如,在下圖中,我創建了一個宏,如果值高於基線值,則將所有標記 colors 更改為紅色,但如果有 3 個連續值高於基線值,則不會。
我的代碼
這是我嘗試過的 - 如果值超過 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
為此,您需要使用 window 尺寸來檢查始終連續 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.