Thanks for coming here, I hope you are enjoying learning here, I have also written some books in case you want to learn a bit more :)
If you need my help with Drupal, Linux, Jira, Scripting, Automation or want to contact me then raise a ticket for me please :) and I will get back to you, promise

Change/Preserve the color of bars in Excel Graph

Every week I prepare one report in Excel with lot of graphs. The data in the excel sheet is added in every report and the graphs that are generated based on that data are of course also updated. The client was satisfied with the report but asked me to preserve the color of the various countries that appear on the stacked bar chart. Initially for couple of weeks I didn't really paid much attention but then the client got little demanding and requested me again in harsh tone to do it. I still use Excel 2003 and not sure whether this feature is there in 2007 version, I guess not. After searching on the internet I did found a solution that requires creation of a macro that can be used to hard code the values and assign them specific color.

Here is the code. I hope it will be helpful to you. Enjoy.

  1. Sub change_bar_color()
  4.     NumPoints = ActiveChart.SeriesCollection.Count
  6.     For x = 1 To NumPoints
  8.         thispt = ActiveChart.SeriesCollection(x).Name      
  10.         Select Case thispt
  11.             Case "India"
  12.                 ActiveChart.SeriesCollection(x).Interior.ColorIndex = 2
  13.             Case "USA"
  14.                 ActiveChart.SeriesCollection(x).Interior.ColorIndex = 14
  15.             Case "Africa"
  16.                 ActiveChart.SeriesCollection(x).Interior.ColorIndex = 44
  17.             Case "Australia"
  18.                 ActiveChart.SeriesCollection(x).Interior.ColorIndex = 5
  19.             Case "Other"
  20.                 ActiveChart.SeriesCollection(x).Interior.ColorIndex = 21
  22.             Case Else
  23.                 ' Add code here to handle an unexpected label
  24.         End Select
  26.     Next x
  29. End Sub

Of course you need to modify the above code as per your needs. Though I am a hard core linux user but for few things I still use windows xp and Excel is one tool that I believe is one of the best and most powerful tool built by Microsoft. Lot of amazing things can be done in Excel and its power can be extended with the help of VBA to create applications and automating.

Similar posts

Ravi Sagar Newsletter

Honestly I hardly send out mails but I guess there is no harm in at least having a newsletter. I will only send newsletter when I have something important to share or an update. People are busy and no one has time to read emails these days. If you really like my content then I guess you will remember my site and come back for more.

Want to contact me?