在Excel VBA中将图表颜色设置为自动

我有一个macros设置堆叠条形图上的所有系列具有相同的颜色,以及像这样的其他几个位:

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 

不过,我需要select将系列更改回默认堆叠条形图的方式,并自动将颜色select为各种调色板。

让我知道你是否需要任何进一步的信息。

好的,解决了这个问题 有一种方法可以调用图表来重置样式:

 ActiveChart.ClearToMatchStyle 

希望帮助别人!