+ Reply to Thread
Results 1 to 6 of 6

Conditional formatting pie charts

  1. #1
    Registered User
    Join Date
    06-07-2016
    Location
    London
    MS-Off Ver
    2010
    Posts
    6

    Conditional formatting pie charts

    Hi everyone

    I have a question about conditional formatting pie charts. What I would like to achieve is the following:

    I have 11 storage spaces, which I would like to represent in a pie charts as all equal in size.
    Furthermore, each storage space can be either empty or full (0 or 1).
    What I would like to do is to represent all 11 storage spaces in a pie chart regardless whether it is
    full or empty and have color coding where a full storage space would be red and an empty storage space
    would be green.

    Does anyone know how to achieve that?

    Regards

    AS

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: Conditional formatting pie charts

    I got this mostly by recording a macro and then wrapping it in a loop. I made a pie chart with equal sized wedges (Column C) and then assign colors based on whether the space is Full or Empty.

    I trigger the event off a change in Cells B2:B12 to run the macro FillColor.
    Attached Files Attached Files
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  3. #3
    Registered User
    Join Date
    06-07-2016
    Location
    London
    MS-Off Ver
    2010
    Posts
    6

    Re: Conditional formatting pie charts

    Hei

    Thank you so much for that, that is exactly what I needed!

    I was looking at the code and trying to understand it, so that I can modify and add to it if needed. I have some limited experience with C and Java but never used macro.

    A question about the code:

    Please Login or Register  to view this content.
    Here you use three different variables: "k", "sh" and "R"

    Now I see that "sh" is defined as Sheet1.

    What about "R" and "k" ? What type of variables are they?

    Also the next part of the code:

    Please Login or Register  to view this content.
    So I see that this is the loop. But you are looping through k+1. Did you define "k" as the column containing the information whether the cell is empty or full or does it have another meaning?

    Also how do you refer to the pie chart? Which part of the code tells the chart to color in the data?

    Thank you so much for your help!

    Cheers

    Alex

  4. #4
    Registered User
    Join Date
    06-07-2016
    Location
    London
    MS-Off Ver
    2010
    Posts
    6

    Re: Conditional formatting pie charts

    Ok I figured out what the "R" was. It is part of the colors "R" "B" "G" right?

    Also when I try changing the space from "Full" to "Empy" it opens macro and gives me an error:

    "Compile error: Method or data member not found and it highlights
    Please Login or Register  to view this content.
    Am i missing a package or something?

  5. #5
    Registered User
    Join Date
    06-07-2016
    Location
    London
    MS-Off Ver
    2010
    Posts
    6

    Re: Conditional formatting pie charts

    Also with the compile error, the "Sub FillColor()" line gets highlighted in yellow allong with ".FullSeriesCollection" becoming highlited

  6. #6
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: Conditional formatting pie charts

    I’m an ex-C programmer myself. Tell me, does it bother you as much as it bothers me about how “loose” VBA is with variables? .

    I have my personal naming conventions for my coding. One of them is that any variable starting with i has something to do with rows and any variable that starts with j has something to do with columns. I picked k as the next letter of the alphabet to be my first choice as an index or counter.

    Each wedge of the chart is controlled by the “index” of the point: ActiveChart.FullSeriesCollection(1).Points(k).Format.Fill there are 11 wedges in the pie, so k here goes from 1 to 11.

    I also have the status in 11 rows in column B from B2:B12. So one loop does two tasks. It reads the status: If sh.Cells(k + 1, 2) = "Full" Then

    And if sets the corresponding wedge color With ActiveChart.FullSeriesCollection(1).Points(k).Format.Fill.

    I added in the change event as an afterthought. Based on the cell that is changed, I could have read that specific status and set that specific wedge rather than loop through all 11 items. In theory, this would be more efficient code. But with only 11 things to loop through and only one thing to set or calculate per item, I decided to leave “good enough” alone.

    -=o=-

    I just read your follow-on posts.

    I can’t duplicate your error. You say it occurs here: With ActiveChart.FullSeriesCollection(1).Points(k).Format.Fill.

    There are two things I can think of if you have changed anything.

    One is that the chartobject has a different name: sh.ChartObjects("Chart 1").Activate

    The other is that you are plotting a different FullSeriesCollection.

    OR

    I am using Excel 2013 and you are using Excel 2010. It could be that some things have been “depreciated” as Microsoft puts it which means they’ve changed it and the codes isn’t backwards compatible.

    I suggest you record a macro that selects and individual pie wedge and change its color. That’s what I did to get the syntax. This should also tell you what chart object and FullSeriesCollection is active.

    P.S. please feel free to change to name of the macro to FillColour .

+ 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. Conditional Formatting for Charts
    By benjthom in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-31-2013, 12:06 AM
  2. Charts and Conditional Formatting
    By swatkins1977 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-07-2012, 10:43 AM
  3. Gantt Charts done with Conditional Formatting
    By nielsoneo in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-16-2012, 02:25 PM
  4. Conditional formatting on bar charts
    By cosmo6 in forum Excel General
    Replies: 1
    Last Post: 02-25-2009, 07:05 AM
  5. Conditional Formatting on Bar Charts
    By Paul Sheppard in forum Excel General
    Replies: 2
    Last Post: 06-30-2008, 04:03 AM
  6. Conditional formatting of charts.
    By SER01 in forum Excel Charting & Pivots
    Replies: 10
    Last Post: 11-04-2007, 08:36 PM
  7. [SOLVED] conditional formatting in charts
    By Dan_Green in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 03-01-2006, 09:15 AM
  8. Conditional formatting in Charts?
    By Todd in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 01-11-2005, 03:06 PM

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