2008-11-14 59 views
1

我有一個圖表,其中包含一系列表示離散度量的大集合(1000)的圖表。其中一些是不好的測量,我想根據描述測量精度的另一組數據爲系列着色。不好的測量結果應該是紅色的,好的測量結果是綠色的,中間的測量結果是從紅色到黃色到綠色的某種梯度。如何以編程方式更改Excel 2007中圖表中一個系列的線條顏色

這應該可以用VBA進行編程,但是我不知道該怎麼做。任何人都可以給我一些提示嗎?

回答

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 
+0

謝謝,你能簡單介紹一下它的功能嗎?什麼應該在'壞'範圍內? – geometrikal 2008-11-14 23:17:08

0

您是否鎖定在VBA中?一種可以實現的方法是打開OOXML .xlsx文檔歸檔文件(它實際上是一個Zip歸檔文件)。然後,您可以自由訪問組成文檔本身的XML數據。這可以通過XSL樣式表或您選擇的任何其他腳本來運行,然後進行重新編碼。

+0

謝謝,我沒有意識到這一點。我會看看。 – geometrikal 2008-11-14 06:02:55

相關問題