如何使用vba标记 xlSparkLine 的最大值和最小值
时间: 2024-01-24 16:02:18 浏览: 143
您可以使用以下代码来标记 xlSparkLine 的最大值和最小值:
```vba
Sub MarkMinMax()
Dim cell As Range
Dim minVal As Double, maxVal As Double
Dim sparkline As Sparkline
For Each cell In Selection
If cell.SparklineGroups.Count > 0 Then
Set sparkline = cell.SparklineGroups(1).Item(1)
minVal = Application.WorksheetFunction.Min(sparkline.Points)
maxVal = Application.WorksheetFunction.Max(sparkline.Points)
For i = 1 To sparkline.Points.Count
If sparkline.Points(i) = minVal Then
sparkline.Points(i).MarkerStyle = xlMarkerStyleCircle
sparkline.Points(i).MarkerBackgroundColor = vbGreen
ElseIf sparkline.Points(i) = maxVal Then
sparkline.Points(i).MarkerStyle = xlMarkerStyleCircle
sparkline.Points(i).MarkerBackgroundColor = vbRed
End If
Next i
End If
Next cell
End Sub
```
这个代码会循环遍历选定的单元格,并检查每个单元格中是否有 xlSparkLine。如果有,则找到第一个 xlSparkLine,找到其最大值和最小值,并将其标记为红色(最大值)和绿色(最小值)圆圈。
阅读全文