如何以编程方式更改Excel 2007中图表中系列的线条颜色

时间:2008-11-14 02:58:55

标签: excel excel-vba charts vba

我有一个图表,其中一系列表示一组大的(1000)离散测量值。其中一些是不好的测量,我想根据描述测量精度的另一组数据为系列着色。测量结果不好应为红色,测量结果为绿色,测量值介于两者之间,从红色到黄色再到绿色。

这应该可以用VBA编程但是我不知道该怎么做。谁能给我一些提示?

2 个答案:

答案 0 :(得分:4)

在VBA中为图表线着色很容易。这是一些笔记。

Dim cht  As Chart
Dim sc As Series
Dim blnBad As Boolean
Dim j

j = 85 'RGB orange '
blnBad = False

'This is a chart called Chart 1, it would be possible '
'to use the charts collection '
Set cht = ActiveSheet.ChartObjects("Chart 1").Chart
'A chart is composed of series of data ... '
For Each sc In cht.SeriesCollection
    ' ... that you can iterate through to pick up '
    ' the individual data values, or a data range. '
    ' Values in this case. '
    For i = LBound(sc.Values) To UBound(sc.Values)
        ' That can be checked against another set of '
        ' values in the range Bad. '
        With ActiveSheet.Range("Bad")
            ' So, look for the value ... '
            Set c = .Find(sc.Values(i), lookat:=xlWhole, LookIn:=xlValues)
            ' and if it is found ... '
            If Not c Is Nothing Then
                ' ... then set the Bad flag '
                blnBad = True
            End If
        End With
    Next
    ' So, this range contains a Bad value '
    ' and we will colour it red ... '
    If blnBad Then
        sc.Border.Color = RGB(255, 0, 0)
        ' ... not forgetting the markers '
        sc.MarkerForegroundColor = RGB(255, 0, 0)
    Else
        ' Otherwise, use an increasingly yellow colour '
        sc.Border.Color = RGB(255, j, 0)
        sc.MarkerForegroundColor = RGB(255, j, 0)

        j = j + 30 ' getting more yellow
        ' Debug.Print j ' uncomment to see j in the immediate window '
    End If
    blnBad = False
Next
End Sub

答案 1 :(得分:1)

你被锁定在VBA吗?可以实现的一种方法是打开您的OOXML .xlsx文档存档(它实际上是一个Zip存档)。然后,您可以自由访问构成文档本身的XML数据。这可以通过XSL样式表或您选择的任何其他脚本运行,然后重新压缩。