Solved

VBA Color Code Pie Slices

Posted on 2013-12-24
11
520 Views
Last Modified: 2014-01-09
Could someone help me update this code need to account for if there is no Reason Code would like the slice of the pie to be Black (using index color (1)).

'Sub ColorPieSlices()
Dim NumPoints As Long, x As Long
Dim SavePtLabel As String, ThisPt As String
Dim ws As Worksheet
Dim tbReasonCodes As Range
Dim Colors As Variant, Labels As Variant, v As Variant
Dim pt As Point

On Error Resume Next
For Each ws In ActiveWorkbook.Worksheets
    Set tbReasonCodes = ws.ListObjects("tbReasonCodes").DataBodyRange
    If Not tbReasonCodes Is Nothing Then Exit For
Next
On Error GoTo 0
If tbReasonCodes Is Nothing Then
    MsgBox "Couldn't find table for reason codes", vbOKOnly
    Exit Sub
End If

'Labels = tbReasonCodes.Columns(2).Value
Labels = tbReasonCodes.Columns(3).Value
Colors = tbReasonCodes.Columns(4).Value
For Each cht In Sheets("Valiram").ChartObjects

    NumPoints = cht.Chart.SeriesCollection(1).Points.Count
    
    For x = 1 To NumPoints
        Set pt = cht.Chart.SeriesCollection(1).Points(x)
        SavePtLabel = ""
        If pt.HasDataLabel = True Then SavePtLabel = pt.DataLabel.Text
        pt.ApplyDataLabels Type:=xlDataLabelsShowLabel, AutoText:=True, HasLeaderLines:=False
        ThisPt = pt.DataLabel.Text
        Set v = Nothing
        On Error Resume Next
        v = Application.Match(ThisPt, Labels, 0)
        On Error GoTo 0
        If Not IsError(v) Then
            pt.Interior.ColorIndex = Colors(v, 1)
        End If
        pt.DataLabel.Text = SavePtLabel
    Next x
Next

End Sub

Open in new window


I added the color index to the table that I am referencing but the slices are are different colors between the 3 charts.
0
Comment
Question by:jmac001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 31

Expert Comment

by:gowflow
ID: 39739349
What do you mean by
 if there is no Reason Code  ?
If value = 0 ?

gowflow
0
 
LVL 15

Expert Comment

by:Simon Ball
ID: 39745836
The code seems to pivk up reason code and colour integer from tbReasonCodes

You can add a row to the table but what will you use to define the "No reason" as a code?

Is it an existing code...?  if not, we'll need to add a further if between lines 37 and 39....

If Not IsError(v) Then
            pt.Interior.ColorIndex = Colors(v, 1)
Else
pt.Interior.ColorIndex = 0
End If

Open in new window


Might work.  might not... not sure about your data... can you post the contents of the tbReason table?
0
 
LVL 31

Expert Comment

by:gowflow
ID: 39746068
@Simon Bal
I had tried this in the code prior to my comment but as it did not give any significant change I responded as above.

I already worked extensively on previous question for this same workbook and fear that the issue is not clear and need to be clarified.

gowflow
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jmac001
ID: 39746525
The value for the cell can be either blank or 0 it will depend on which worksheet the data is being pulled from when creating the pie chart.  If you look at the Complete tab you will see that the Reason Code Value in some instances is  0 and if you look at the Budget worksheet the Reason Code cell is blank.    

The Reason Code tab has the table with the color index that is used in the VBA.
EE-Test-VSBA-Scorecard-2013.11.0.xlsm
0
 
LVL 31

Accepted Solution

by:
gowflow earned 500 total points
ID: 39747209
Hi Jmac001,

Is this what your looking for ? Activate macro CreatePie and check the results.
gowflow
EE-Test-VSBA-Scorecard-2013.12.3.xlsm
0
 

Author Closing Comment

by:jmac001
ID: 39748678
Exactly, what I was looking. Thank you.
0
 
LVL 31

Expert Comment

by:gowflow
ID: 39748778
Great !!! at least we hit in on this one.
Happy New year to you and all the best for 2014.

I would like you to (if you want) repost a question on the 3 graphs that I was not able to work in the past as to top6 and top3 will be glad to assist.

gowflow
0
 

Author Comment

by:jmac001
ID: 39751262
Thank you and Happy New Years  to you as well.  I will be reposting the Top 6 Top 3 just wanted to make sure that there were no changes prior to posting.

Thanks again.
0
 
LVL 31

Expert Comment

by:gowflow
ID: 39751823
ok pls put a link of the new question here.
Rgds/gowflow
0
 
LVL 31

Expert Comment

by:gowflow
ID: 39761534
Any news on your new question ?
gowflow
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question