Change Chart Colour Scheme

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Change Chart Colour Scheme

Post by Rudi »

Hi,

I have multiple workbooks with several charts in each.
I have code to open each workbook and select the charts, but I'd like to update the overall colour scheme of each chart to a different colour (see image)

The recorder gives me the .ChartColor = 11 (property), however, when i run it in my code, it debugs with Object does not support this property??
Any idea how I can change the default Excel chart colours to a theme colour as in the image?

My code:

Code: Select all

    For iChart = 1 To ActiveSheet.ChartObjects.Count
    Set chtob = ActiveSheet.ChartObjects(iChart)
    With chtob
        .Activate
        .ChartColor = 11
    End With
    Next
TX
1.jpg
You do not have the required permissions to view the files attached to this post.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Change Chart Colour Scheme

Post by Rudi »

Actually...my silly mistake.
I solved it!

The .ChartColor property is actually a property of the ActiveChart not of ChartObjects!
This code works:

Code: Select all

    For iChart = 1 To ActiveSheet.ChartObjects.Count
        ActiveSheet.ChartObjects(iChart).Select
        ActiveChart.ChartColor = 11
    Next
    Range("A1").Select
I'm not sure if one *needs* to select the chart in order to change the ChartColor?
I select Range("A1") to deactivate the chart after the macro runs.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78643
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Change Chart Colour Scheme

Post by HansV »

ChartColor must be new in Excel 2013 - it's not available in Excel 2010.

As usual, you don't need to select:

Code: Select all

    For iChart = 1 To ActiveSheet.ChartObjects.Count
        ActiveSheet.ChartObjects(iChart).Chart.ChartColor = 11
    Next iChart
or

Code: Select all

    Dim cht as ChartObject
    For Each cht In ActiveSheet.ChartObjects
        cht.Chart.ChartColor = 11
    Next cht
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Change Chart Colour Scheme

Post by Rudi »

Excellent. I see it is hooked up to the "Chart" Object
Whoever came up with, The Chart object is an object inside the ChartObject object :drop:

I was aware of it...just forgot!
TX

BTW: If ChartColor is new, is there an alternative for 2010? The code I am setting up is actually supposed to run in Excel 2010.
TX
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78643
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Change Chart Colour Scheme

Post by HansV »

A ChartObject is a shape that contains a chart.

I don't think there is an equivalent to ChartColor in Excel 2010 or earlier. You can set the ThemeColorScheme but this applies to the entire workbook and hence to all charts in the workbook. And you can set the ChartStyle but that changes other aspects in addition to the color scheme, such as 3D effects.
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Change Chart Colour Scheme

Post by Rudi »

TX.
Appreciate the insights.

The code is actually to update a few hundred financial sheets with different colour etc.
I have code to loop through all the files in a folder, find cells with certain colours, and update them to other colours...update fonts and certain measurements of the printout. There are a few charts in each file too, so I plan to create a colour scheme and then run the code to update the charts with that colour scheme. If I go this route I will need to do the updating of these sheets on my laptop. It is only a once off, so if Excel 2013 (with the chartcolor property can make it easier), then that's the way to go.

Cheers
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.