+ Reply to Thread
Results 1 to 21 of 21

Change colour of chart series with VBA

  1. #1
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Change colour of chart series with VBA

    Hi all,

    What would be the right line of code (no selection statements if possible) to change the colour of a bar or line series within a chart?

    I want to be able to switch between colour schemes, but only change the blue colours and leave the rest untouched!

    See image below...

    Colour scheme.PNG

  2. #2
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,714

    Re: Change colour of chart series with VBA

    Hi
    with only a picture its a bit hard to see what's going on in your sheet, but this could get you started.
    It changes the line colour of the first series in your chart to red, and the fill colour of the second series (assuming its a bar or similar) to yellow.

    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: Change colour of chart series with VBA

    Sorry, maybe this wasn't very clear. I have attached a sample workbook to work with. I basically want to select a colour from the Form Control ComboBox and then have it take the colour code from the control sheet and apply it to all charts where bars or lines are blue, while leaving the white bars or lines white...

    P.S. Ignore the other metrics form control combobox
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,714

    Re: Change colour of chart series with VBA

    Hi
    unfortunately your combo box links to a file on your C:\ drive, so I can't see how it works exactly.
    assuming your combobox links to a range containing colours, and the first of these cells is the blue you want to replace (as on your second sheet) then assuming your colour range has the name "ColRange", and the output cell is names OutRange, try this

    Please Login or Register  to view this content.
    Last edited by NickyC; 10-16-2017 at 05:57 AM. Reason: remove msgbox check

  5. #5
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: Change colour of chart series with VBA

    As I had mentioned, just ignore the other form control combobox, it only moves the series range down or up and isn't relevant for this. I just want to change all blue bars, lines into orange when I select orange in my colour combobox and back to blue whenever I select blue.

    I just tried your line of code and get below error message. Also, I wasn't exactly sure what range I need to define as "ColRange" and which as "Outrange"? Shall "ColRange" refer to cells F10:I10 where my 4 different colour are? and then "Outrange" refers to the form control cell link with the selected colour?

    forum.PNG

    This was the line of code with the error:

    Please Login or Register  to view this content.

  6. #6
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,714

    Re: Change colour of chart series with VBA

    Hi
    Yes, ColRange should be a range of cells each filled with a colour corresponding to the colours in your combobox, and outrange if the form control cell. The macro should change the colour of any series in the active chart whose line colour is the same as the first colour in your drop down list to the colour you select. So it needs 3 things - a named range ColRange, a named cell Outrange, and for you to have a chart activated.

    I got the same error when I had the drop down box selected rather than the chart - perhaps that is the issue.

  7. #7
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: Change colour of chart series with VBA

    You are right, if I select a chart before, the error does not appear, though, it also doesn't change any bar or line colour...

  8. #8
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,714

    Re: Change colour of chart series with VBA

    Hi

    the colour it searches for is the colour of the first cell in ColRange. If no series has that colour, none will change.

    It may be than none of your series lines is exactly that colour.

    Colours in excel are very precise - there are more than 16 million possible colours, and thousands of these could be considered "blue". you don't appear to be using the standard excel blue shade (xlBlue, or RGB(0,0,255)), which is why I tied the macro to a cell in your colour range, not a particular colour value.

    It may be possible to specify colours that are blue-ish (ie more blue than red or green on the RGB spectrum) but that will pick up a lot of colours that don't look blue.

  9. #9
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: Change colour of chart series with VBA

    It may be possible to specify colours that are blue-ish (ie more blue than red or green on the RGB spectrum) but that will pick up a lot of colours that don't look blue.
    Hence, I guess it might be possible to just change ALL data series that are NOT white-ish to the colour I select in my combobox?

  10. #10
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Change colour of chart series with VBA

    white-ish is not a technical term
    you need to set all your charts to be exactly the color you have setup in control tab

    secondary issue is the syntax for changing color in charts is different when it is a line chart compared to when it is a bar chart
    so you also need to cater for that too
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  11. #11
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: Change colour of chart series with VBA

    Hi humdingaling

    Glad to see you again on one of my threads

    white-ish is not a technical term
    you need to set all your charts to be exactly the color you have setup in control tab

    secondary issue is the syntax for changing color in charts is different when it is a line chart compared to when it is a bar chart
    so you also need to cater for that too
    alright, this seems to be getting to complicated for such a minor thing. Can I just use a Macro that calls each of the chart lines and bars separately and changes its colour to what is selected in my Form Control ComboBox? Basically a more specific approach with defining chart names and lines rather than a general "change certain lines that have a certain colour" approach...

    Something like the below...

    Please Login or Register  to view this content.

  12. #12
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Change colour of chart series with VBA

    that was a lot more painful than i first thought
    however in the end...it works out better than i thought it would

    Please Login or Register  to view this content.
    code is linked to your combobox which changes control tab
    Attached Files Attached Files
    Last edited by humdingaling; 10-17-2017 at 12:50 AM. Reason: added the glow effect back into chart 11

  13. #13
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Change colour of chart series with VBA

    if there is something in the code that requires further explanation let me know
    you had extra thing to worry about with grouping and then another thing with doughnut chart having to deal with data points instead of data series

    in the end i went with brute force over write which is probably the best way to do it

    it will directly go to the specific chart and specific series/data point and change whatever RGB it had to the one specified by your combobox

    now of course this means if you change any names or order of data series etc you will need to remap the code again
    Last edited by humdingaling; 10-17-2017 at 12:44 AM. Reason: grammar

  14. #14
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,714

    Re: Change colour of chart series with VBA

    actually white is a bit harder than blue because its not a single colour but a large concentration of other colours. This variation will change the colour of all pale lines (with red, green and blue values in the RGB formula all > 200) to the selected color in the combobox

    Please Login or Register  to view this content.

  15. #15
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: Change colour of chart series with VBA

    Impressive how fast you solve these issue humdingaling! Thanks!!

    I have just tried and it works incredible well in the sample workbook, however, my actual workbook is using slightly different white-ish colours I just realised and it doesn't work for some charts anymore. Hence, since I believe this is a brilliant method, but at the same time a little too flexible (in case colours change in the future), I would probably prefer an approach such as below, where each chart and its data series is called and then its colour changed based on what is selected in my combobox. Can you help me adjust below code to do just exactly that for all charts? I assume I would just have to change the chart name and sometimes "line" to "points" as you had mentioned with doughnut charts?

    Please Login or Register  to view this content.
    Also, what is the syntax for "glow"? how would I need to refer to that in my code?

  16. #16
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Change colour of chart series with VBA

    arr yes i should just do it right down to bare bones
    would only need to do loops if multiple series/lines/data points per chart is required


    Please Login or Register  to view this content.
    Attached Files Attached Files

  17. #17
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: Change colour of chart series with VBA

    Thank You!!! Just what I was looking for! I think this a lot simpler to implement than the other method.

    I just somehow don't seem to get the code right with the glow transparency! Can you help me fix below code?

    Please Login or Register  to view this content.

  18. #18
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Change colour of chart series with VBA

    you've got it under the wrong "with"

    peg back the with statement to a "lower level"

    try these instead
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    when in doubt...macro recorder it

  19. #19
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: Change colour of chart series with VBA

    you've got it under the wrong "with"

    peg back the with statement to a "lower level"

    try these instead
    That actually wasn't it! I tried back and fourth, still didn't work! Figured it was the transparency value itself as it should be between 0 and 1 as it is a percentage. working perfectly fine now

    when in doubt...macro recorder it
    I tried! Somehow it doesn't capture chart formatting for me...

    One last thing (promise!), If I have a heat map with a colour range of 5 or 10 shades. How can I get the Macro to change that? I have all colour shades already defined on my control sheet...

    heat map.pmg.PNG

  20. #20
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Change colour of chart series with VBA

    sorry cannot help
    i dont have access to heat maps in my version of excel so cannot test the syntax out
    i did a quick google and it looks like i found the something that looks like the base code

    Please Login or Register  to view this content.
    if this works then you just need to change the forecolor as everything else would be the same setting?
    so maybe
    Please Login or Register  to view this content.

  21. #21
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: Change colour of chart series with VBA

    i dont have access to heat maps in my version of excel so cannot test the syntax out
    i did a quick google and it looks like i found the something that looks like the base code
    Damn it! No worries, will try it out. Thanks for doing a google search and all your other help so far!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Chart Series Automatic Colour
    By AndersonM in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 06-01-2016, 06:37 AM
  2. VBA to change column chart series colour
    By Jabba69 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-29-2014, 11:51 AM
  3. [SOLVED] Change series colour for all charts on a worksheet.
    By DonaldDump in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-28-2013, 06:12 PM
  4. Colour change column chart based on cell colour
    By Alice21 in forum Excel General
    Replies: 11
    Last Post: 04-05-2011, 10:10 AM
  5. Change colour of chart series
    By downunderthunder in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-06-2010, 09:20 PM
  6. Stacked Area Chart Series Colour Change
    By downunderthunder in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-06-2010, 09:18 PM
  7. Conditional Series Collection Colour for Chart
    By benno87 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-24-2009, 09:54 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1