1

Hi I can't believe I can't find an answer to this but I have looked.

In Excel I want to restore the default colour palette order to a chart that I have deleted some series from.

Ie if my colour palette goes: red, green, blue, yellow, orange, grey and I originally have a chart with 6 lines they are in this order. Start with this graph

If I then delete the green, blue and yellow lines:

Deleted some series

I am hoping there is a way to update the chart so instead of the remaining red, orange, grey I can return it to red, green, blue - Ie what I would get if I recreated the final chart from scratch: Want to end up with this

Obviously I could recreate from scratch or manually change colours but I do this fairly frequently and its often a lot easy to select all columns and delete the ones I don't want than to go through and make a chart line by line.

I'm using corporate excel with minimal permissions so no addins please. Just want to know if this is possible in standard excel environment.

Sarah
  • 3,022
  • 1
  • 19
  • 40
  • How are you deleting the data? The only way I can see to do this is to effectively recreate the chart by changing the chart date range to not include the requried rows. Just deleting the line from the chart or remvoing the series dosent seem to refresh the chart – A.Steer May 24 '22 at 07:58
  • Is VBA allowed? – Evil Blue Monkey May 24 '22 at 10:53

3 Answers3

1

I did some testing, most macros that just adjust the colors seem to create a mess, especially if you want to adjust the chart by using the "chart design->change colours" option. So I went for "remove all series and add them again" approach. This works fine on my PC (Office 365). Select the chart and run this macro (you can assign a shortcut key to make this faster: https://www.excelcampus.com/vba/keyboard-shortcut-run-macro/ ):

Sub ChartRemoveReAddData()

    Dim DataArr() As String
    Dim n As Long
    
    Set AChart = Application.ActiveChart
    If Not AChart Is Nothing Then
        'First add series to DataArr
        i = 0
        For Each Ser1 In AChart.SeriesCollection
            sFmla = Ser1.Formula
            ReDim Preserve DataArr(i)
            DataArr(i) = sFmla
            i = i + 1
        Next Ser1
        'Again, but now reverse to remove them
        For n = AChart.SeriesCollection.Count To 1 Step -1
            AChart.SeriesCollection(n).Delete
        Next n
        AChart.ClearToMatchStyle
        AChart.ClearToMatchColorStyle
        For i = 0 To UBound(DataArr)
            Set S = AChart.SeriesCollection.NewSeries
            S.Formula = DataArr(i)
        Next i
    End If
   
End Sub

And a bit more detail to the colouring: Excel has 10 theme colours (plus 2 for hyperlinks) that you can find if you click e.g. the font colour or background colour button. The last 6 of that overview are the relevant ones for charts. The are named Accent 1 to Accent 6 (https://learn.microsoft.com/en-us/office/vba/api/Office.MsoThemeColorIndex). And as you can see those colours are showing up when you select your chart and select "chart design - change colours". So e.g. ChartColor = 11 (in VBA) means that your chart shows the colours Accent 1 (first series), Accent 3 (second series), Accent 5 (3rd series).

Excel Theme colour example

Koen Rijnsent
  • 230
  • 1
  • 13
  • Thank you! This macro works exactly as I hoped. I have to confess I'm still confused by your explanation of the chart colours, but adding and removing process is working so I'm happy with that :) – Sarah May 30 '22 at 23:10
  • 1
    Sorry to complicate matters... I was trying to over-clarify how the colours in Excel work (especially the part of themes), as I haven't found a comprehensive guide on that topic yet. Anyhow, glad it works! – Koen Rijnsent May 31 '22 at 17:15
0

A manual procedure is to save the graph model and reload it once the series have been deleted.

A VBA solution might be similar to this one:

Sub SubParallelMeridianFormat()
    
    'Declarations.
    Dim IntCounter01 As Integer
    Dim Ser01 As Series
    Dim Char01 As Object
    Dim LngColourPalette(1 To 6) As Long
    
    'Setting LngColourPalette.
    LngColourPalette(1) = vbRed
    LngColourPalette(2) = vbGreen
    LngColourPalette(3) = vbBlue
    LngColourPalette(4) = vbYellow
    LngColourPalette(5) = RGB(255, 165, 0)
    LngColourPalette(6) = 16711680
    
    'Setting Char01.
    Set Char01 = ActiveSheet.Shapes("Graph 1")
    
    'If there are not enouth colour, the macro is terminated.
    If Char01.Chart.SeriesCollection.Count > UBound(LngColourPalette) Then
        MsgBox "The graph contains " & Char01.Chart.SeriesCollection.Count & " series while only " & UBound(LngColourPalette) & " colours have been specified. No changes will be applied. Add more colours to the code and try again.", vbCritical + vbOKOnly, "Not enouth colour"
        Exit Sub
    End If
    
    'Changing colours.
    For Each Ser01 In Char01.Chart.SeriesCollection
        IntCounter01 = IntCounter01 + 1
        Ser01.Format.Line.ForeColor.RGB = LngColourPalette(IntCounter01)
    Next
    
End Sub

You might need to edit the settings of LngColourPalette and Char01.

In order to obtain a list of colour from a pre-existing graph, you might use this code:

Sub SubColourList()
    
    'Declarations.
    Dim Ser01 As Series
    Dim Char01 As Object
    Dim IntCounter01 As Integer
    
    'Setting Char01.
    Set Char01 = ActiveSheet.Shapes("Graph 1")
    
    'Reporting colours.
    For Each Ser01 In Char01.Chart.SeriesCollection
        IntCounter01 = IntCounter01 + 1
        Debug.Print "LngColourPalette(" & IntCounter01 & ") = "; Ser01.Format.Line.ForeColor.RGB
    Next
    
End Sub

Here you have a list of colour constants for VBA and a list of colour codes in multiple coding style.

Evil Blue Monkey
  • 2,276
  • 1
  • 7
  • 11
  • Is there a way to get a list of the colour of the actual theme no the specific chart? I want to return to the default theme instead of using any colours actually on the plot? Would ideally not be hard coded colours so if I change the default colour (chart design - change colours) it would update, not using VBA default colours or hard coded numbers? – Sarah May 30 '22 at 00:29
  • I haven't found an "address" to the default style of graph. The problem (considering your objective) is that once you delete a series, the graph doesn't change the [PlotColorIndex](https://learn.microsoft.com/it-it/dotnet/api/microsoft.office.interop.excel.series.plotcolorindex?view=excel-pia#microsoft-office-interop-excel-series-plotcolorindex) property (which is also a read-only property). Thus far i can't come up with a solution different from what @Koen Rijnsent suggested (delete and add back again the series). Test his solution and send feedback. – Evil Blue Monkey May 30 '22 at 14:09
0

This approach will go through the chart, capture the formulas of the existing series, delete the existing series, and add them back from the stored formulas. In so doing, Excel will reapply the default colors.

I think it's simpler than the other proposed answers, even the one marked "Answer", and it will actually do what is requested.

Sub RebuildChartWithDefaultSeriesColors()
  With ActiveChart.SeriesCollection
    Dim nSrs As Long
    nSrs = .Count
    Dim vSrsFmla As Variant
    ReDim vSrsFmla(1 To nSrs)
    Dim iSrs As Long
    For iSrs = nSrs To 1 Step -1
      vSrsFmla(iSrs) = .Item(iSrs).Formula
      .Item(iSrs).Delete
    Next
    For iSrs = 1 To nSrs
      With .NewSeries
        .Formula = vSrsFmla(iSrs)
      End With
    Next
  End With
End Sub
Jon Peltier
  • 5,895
  • 1
  • 27
  • 27