Paul Grimshaw
Paul Grimshaw

Reputation: 21014

Set the chart colors to automatic in Excel VBA

I have a macro that sets all the series on a stacked bar chart to have the same color, as well as a couple other bits like this:

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

However I need the option to change the series back to how the default stacked bar chart was, with the colors selected automatically to a palette of sorts.

Let me know if you need any further information.

Upvotes: 1

Views: 4133

Answers (1)

Paul Grimshaw
Paul Grimshaw

Reputation: 21014

Ok, solved this. There is a method you can call on the chart to reset the styles:

ActiveChart.ClearToMatchStyle

Hope that helps someone!

Upvotes: 1

Related Questions