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

时间:2012-09-03 16:35:11

标签: excel excel-vba 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

但是我需要选择将系列更改回默认堆积条形图的方式,并自动选择颜色到各种调色板。

如果您需要任何进一步的信息,请与我们联系。

1 个答案:

答案 0 :(得分:1)

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

ActiveChart.ClearToMatchStyle

希望能帮助别人!