Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBA Color Code Pie Slices

Posted on 2013-12-24
11
Medium Priority
?
540 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

721 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