2016-11-04 188 views
1

我有一些代碼,我已經使用excel圖表顏色已經有好幾年了,它運行良好,(儘管有可能有更好的方法來實現)。這些圖表包含2個系列,第一個系列具有價值,第二個系列具有目標。目標沒有着色,但vba根據vba中硬編碼的目標循環遍歷第一個系列和顏色。Excel VBA,基於系列值比較選擇圖表顏色

我現在面臨的問題是我添加了一個圖表,其目標可以逐月更改,因此硬編碼不起作用。我如何使用相同的理論,但直接比較系列1數據與系列2數據以確定顏色(案例是系列1點>系列2點等)。我嘗試了很多方法,但沒有成功,所以我們將不勝感激。以下是成熟技術的代碼。

Private Sub Worksheet_Activate() 
Dim cht As Object 
Dim p As Object 
Dim V As Variant 
Dim Counter As Integer 

For Each cht In ActiveSheet.ChartObjects 
Counter = 0 
V = cht.Chart.SeriesCollection(1).Values 
For Each p In cht.Chart.SeriesCollection(1).Points 
Counter = Counter + 1 
Select Case V(Counter) 

'Case Is = 1 
    'p.Shadow = False 
    'p.InvertIfNegative = False 
    'p.Fill.OneColorGradient Style:=msoGradientVertical, Variant:=3, _ 
    ' Degree:=0.78 
    'p.Fill.Visible = True 
    'p.Fill.ForeColor.SchemeColor = 5 

Case Is < 0.98 
    p.Shadow = False 
    p.InvertIfNegative = False 
    p.Fill.OneColorGradient Style:=msoGradientVertical, Variant:=3, _ 
     Degree:=0.78 
    p.Fill.Visible = True 
    p.Fill.ForeColor.SchemeColor = 3 

'Case Is < 0.98 
    'p.Shadow = False 
    'p.InvertIfNegative = False 
    'p.Fill.OneColorGradient Style:=msoGradientVertical, Variant:=4, _ 
    ' Degree:=0.38 
    'p.Fill.Visible = True 
    'p.Fill.ForeColor.SchemeColor = 6 

Case Is <= 1 
    p.Shadow = False 
    p.InvertIfNegative = False 
    p.Fill.OneColorGradient Style:=msoGradientVertical, Variant:=3, _ 
     Degree:=0.78 
    p.Fill.Visible = True 
    p.Fill.ForeColor.SchemeColor = 10 

End Select 
Next 
Next 
End Sub 

回答

1

你嘗試類似:

Case Is > .SeriesCollection(2).Values()(Counter)

還修訂擺脫一些明顯的冗餘性(如果需要一個循環計數器變量,例如,循環幾個集合時/數組並行),似乎更好的IMO只是通過索引循環,而不是For Each _object_與單獨的計數器。

Private Sub Worksheet_Activate() 
Dim cht As Object 
Dim p As Object 
Dim V As Variant 
Dim Counter As Integer 

For Each cht In ActiveSheet.ChartObjects 
    Counter = 0 
    With cht.Chart 
     V = .SeriesCollection(1).Values 
     For Counter = 1 to.SeriesCollection(1).Points.Count 

      'Assign your Point object, if needed elsewhere 
      Set p = .SeriesCollection(1).Points(Counter) 

      Select Case V(Counter) 

       Case Is > .SeriesCollection(2).Values()(Counter) 
       'DO STUFF HERE. 

       'Add other cases if needed... 

      End Select 
     Next 
    End With 
Next 
End Sub 

除非你需要在陣列中V值某些其他原因,這可以進一步減少:

Private Sub Worksheet_Activate() 
Dim cht As Object 
Dim p As Object 
Dim val1, val2 
Dim Counter As Integer 

For Each cht In ActiveSheet.ChartObjects 
    Counter = 0 
    With cht.Chart 
     For Counter = 1 to.SeriesCollection(1).Points.Count 

      'Assign your Point object, if needed elsewhere 
      Set p = .SeriesCollection(1).Points(Counter) 
      ' extract specific point value to variables: 
      val1 = .SeriesCollection(1).Values()(Counter) 
      val2 = .SeriesCollection(2).Values()(Counter) 
      Select Case V(Counter) 

       Case val1 > val2 
       'DO STUFF HERE. 

       'Add other cases if needed... 

      End Select 
     Next 
    End With 
Next 
End Sub 
+0

David,感謝您的快速響應。我做了調整並理解了這個想法,但我仍然遇到運行時錯誤。試試我爲系列2值創建了一個變量,並給出了一個鏡頭,但我得到了同樣的錯誤。 –

+0

請參閱修訂版本:'Case Is> .SeriesCollection(2).Values()(Counter)' –

+0

運行時錯誤'451':屬性讓過程未定義,並且屬性獲取過程未返回對象 –

0

與最終代碼編輯,需要2次刷新梯度以完全填充在, (我必須打另一個選項卡然後返回),所以我添加了一個循環來運行代碼兩次,現在它第一次更新完美。希望這可以幫助別人。這允許一個完全動態的圖表。再次感謝大衛。

Private Sub Worksheet_Activate() 
Dim cht As Object 
Dim p As Object 
Dim V As Variant 
Dim Counter As Integer 
Dim L As Integer 

For L = 1 To 2 

    For Each cht In ActiveSheet.ChartObjects 
     Counter = 0 
     With cht.Chart 
      V = cht.Chart.SeriesCollection(1).Values 
      For Counter = 1 To .SeriesCollection(1).Points.Count 
       Set p = .SeriesCollection(1).Points(Counter) 

       Select Case V(Counter) 

       'Blue Gradient 
        'Case Is = .SeriesCollection(2).Values()(Counter) 
         'p.Shadow = False 
         'p.InvertIfNegative = False 
         'p.Fill.OneColorGradient Style:=msoGradientVertical, Variant:=3, _ 
         ' Degree:=0.78 
         'p.Fill.Visible = True 
         'p.Fill.ForeColor.SchemeColor = 5 

       'Red Gradient 
        Case Is < .SeriesCollection(2).Values()(Counter) 
         p.Shadow = False 
         p.InvertIfNegative = False 
         p.Fill.OneColorGradient Style:=msoGradientVertical, Variant:=3, _ 
          Degree:=0.78 
         p.Fill.Visible = True 
         p.Fill.ForeColor.SchemeColor = 3 

       'Yellow Gradient 
        'Case Is < .SeriesCollection(2).Values()(Counter) 
         'p.Shadow = False 
         'p.InvertIfNegative = False 
         'p.Fill.OneColorGradient Style:=msoGradientVertical, Variant:=4, _ 
         ' Degree:=0.38 
         'p.Fill.Visible = True 
         'p.Fill.ForeColor.SchemeColor = 6 

       'Green Gradient 
        Case Is >= .SeriesCollection(2).Values()(Counter) 
         p.Shadow = False 
         p.InvertIfNegative = False 
         p.Fill.OneColorGradient Style:=msoGradientVertical, Variant:=3, _ 
          Degree:=0.78 
         p.Fill.Visible = True 
         p.Fill.ForeColor.SchemeColor = 10 

       End Select 
      Next 
     End With 
    Next 
Next L 
End Sub