2012-09-03 3 views
1

j'ai une macro qui définit toutes les séries sur un graphique à barres empilées pour avoir la même couleur, ainsi que deux autres bits comme celui-ci:Définissez les couleurs de graphique à automatique dans Excel VBA

Sub RefreshLabels() 

    ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh 

    Dim ch As Chart 
    ActiveSheet.ChartObjects("ProjectChart").Activate 
    Set ch = ActiveChart 
    ch.SetElement (msoElementDataLabelCenter) 

    Dim sc As SeriesCollection 
    Set sc = ch.SeriesCollection 

    Dim showLabel As Boolean 
    If (Range("showLabels").Value = "Y") Then 
     showLabel = True 
    Else 
     showLabel = False 
    End If 

    Dim sameColor As Boolean 
    If (Range("sameColor").Value = "Y") Then 
     sameColor = True 
    Else 
     sameColor = False 

    End If 

    Dim s As Series 
    For Each s In sc 

     If (sameColor = True) Then 
      s.Border.Color = RGB(Range("rgb_r"), Range("rgb_g"), Range("rgb_b")) 
      s.Interior.Color = RGB(Range("rgb_r"), Range("rgb_g"), Range("rgb_b")) 
     Else 
      'CODE HERE TO MAKE COLORS AUTOMATICALLY SELECTED FROM PALLETTE 
     End If 

     Set dl = s.DataLabels 
     dl.ShowSeriesName = showLabel 
     dl.ShowValue = False 


    Next 


End Sub 

Cependant J'ai besoin de l'option pour changer la série à la façon dont le graphique à barres empilé par défaut était, avec les couleurs sélectionnées automatiquement à une palette de sortes. Faites-moi savoir si vous avez besoin de plus d'informations.

Répondre

1

Ok, résolu ceci. Il existe une méthode que vous pouvez appeler sur le graphique pour réinitialiser les styles:

ActiveChart.ClearToMatchStyle 

Espérons que cela aide quelqu'un!

+0

Très cool. Vous devriez accepter votre propre réponse comme réponse. –

Questions connexes