Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 65
  • Last Modified:

How to have consistent colouring of excel charts in many tabs

I have an excel file about 20 tabs of data and graphs and I want to have consistence colouring through out all tables
I have created a tap called Lookup Codes which contains all the description id all graphs headings and their associated colour.

I found some code on the web the almost works but I can't get it to work with all graphs in all tabs, any ideas

The attached file is a striped down file I have been using to get a basic solution and my VBA skills are basic
Color-chart-columns-by-cell-color1.xlsm
0
flemingg62
Asked:
flemingg62
  • 7
  • 7
  • 2
  • +1
1 Solution
 
Karen FalandaysTraining SpecialistCommented:
Hi flemingg62,
why do you need VBA? Why not just format one table and save it as a template? Then your formatting and colors will be listed in the chart types as your personal template
kfalandays
0
 
flemingg62Author Commented:
Hi
Thanks I tried templates and I can't get it to keep the same colour for an item. i.e I want  "Vacant" to be blue but Vacant can occur in a different position in the list. Sometime it is 4th, sometime it is 8th. The chart template appears to always assign the same colour to the row. i.e. the 3rd item will always the same colour no matter what the description is .
0
 
Karen FalandaysTraining SpecialistCommented:
Oh I see, that makes sense then to use VBA
kfalandays
0
Industry Leaders: 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!

 
Rgonzo1971Commented:
Hi,
I have created a tap called Lookup Codes which contains all the description id all graphs headings and their associated colour.
Could you send an example of that

Regards
0
 
Rgonzo1971Commented:
pls try
Sub ColorChartColumnsbyCellColor()

For Each sh In Worksheets
    For Each chObj In sh.ChartObjects
        With chObj.Chart.SeriesCollection(1)
            Set vAddress = Range(Split(Split(.Formula, ",")(1), "!")(1))
            For Idx = 1 To vAddress.Count
                res = 0
                On Error Resume Next ' find corresponding XValue
                res = WorksheetFunction.Match(vAddress.Cells(Idx), Sheets("Sheet3").Range("A1:A5"), 0)
                On Error GoTo 0
                If res <> 0 Then ' if found
                    .Points(Idx).Format.Fill.ForeColor.RGB = Sheets("Sheet3").Range("A1").Offset(res - 1, 1).Interior.Color
                End If
            Next
        End With
    Next
Next
End Sub

Open in new window

Regards
Color-chart-columns-by-cell-color1-.xlsm
0
 
flemingg62Author Commented:
Thanks Traveling for the next 2 days, will try on Friday
0
 
flemingg62Author Commented:
Hi
Thanks for the code. I tried it and it works perfectly, bar one problem. Most of my graphs are Power Pivot and I keep getting out of ranges error. I have tried a few variations but no luck

I have attached a file that contains one power pivot tab (and 3 tabs of standard charts), any idea?
Test-colours-Charts-with-Power-Pivo.xlsm
0
 
Rgonzo1971Commented:
Not at my usual place(unable to open the file)

I changed the first code to read directly the values of the chart

Sub ColorChartColumnsbyCellColor()
Dim aXVal()
For Each sh In Worksheets
    For Each chObj In sh.ChartObjects
        With chObj.Chart.SeriesCollection(1)
            aXVal = .XValues
            For Idx = 1 To UBound(aXVal)
                res = 0
                On Error Resume Next ' find corresponding XValue
                res = WorksheetFunction.Match(aXVal(Idx), Sheets("Sheet3").Range("A1:A5"), 0)
                On Error GoTo 0
                If res <> 0 Then ' if found
                    .Points(Idx).Format.Fill.ForeColor.RGB = Sheets("Sheet3").Range("A1").Offset(res - 1, 1).Interior.Color
                End If
            Next
        End With
    Next
Next
End Sub

Open in new window

0
 
Rgonzo1971Commented:
then try ( your labels have to be unique not like in example: 2 "Meeting")
Sub ColorChartColumnsbyCellColor()
Dim aXVal()
For Each sh In Worksheets
    For Each chObj In sh.ChartObjects
        For Each SerCol In chObj.Chart.SeriesCollection
            With SerCol
                aXVal = .XValues
                For Idx = 1 To UBound(aXVal)
                    res = 0
                    On Error Resume Next ' find corresponding XValue
                    res = WorksheetFunction.Match(aXVal(Idx), Sheets("Sheet1").Range("A2:A25"), 0)
                    On Error GoTo 0
                    If res <> 0 Then ' if found
                        .Points(Idx).Format.Fill.ForeColor.RGB = Sheets("Sheet1").Range("A2").Offset(res - 1, 2).Interior.Color
                    End If
                Next
            End With
        Next
    Next
Next
End Sub

Open in new window

0
 
flemingg62Author Commented:
Hi
Thanks very much that solved the problem with power pivots. But when I run it in my main reports not all graphs are updated. In it I have a tab called Lookup Colours (to be hidden at the end) and 3 tabs update and 3 don't. I have set up a secondary column of grey so I can see the one that work ( I just change the column reference when I want to run a different colour scheme). Any Idea

Thanks again for you help so far.
BI-Depaartmental-Reports-9.xlsm
0
 
Rgonzo1971Commented:
then try
Sub ColorChartColumnsbyCellColor()
Dim aXVal()
For Each sh In Worksheets
Debug.Print sh.Name
    For Each chObj In sh.ChartObjects
        With chObj.Chart.SeriesCollection(1)
            aXVal = .XValues
            For Idx = 1 To UBound(aXVal)
                res = 0
                On Error Resume Next ' find corresponding XValue
                res = WorksheetFunction.Match(aXVal(Idx), Range(Sheets("LookUp Colours").Range("A1"), Sheets("LookUp Colours").Range("A" & Rows.Count).End(xlUp)), 0)
                On Error GoTo 0
                If res <> 0 Then ' if found
                    .Points(Idx).Format.Fill.ForeColor.RGB = Sheets("LookUp Colours").Range("A1").Offset(res - 1, 1).Interior.Color
                End If
            Next
        End With
    Next
Next

End Sub

Open in new window

You change the name of the sheet (case sensitive)
0
 
flemingg62Author Commented:
Hi
Thanks and sorry to be a pain, but there is no change, i.e. graph in W1, W3 & M2 won't colour as per the tab "LookUp Colours".
And I assume the comment about case sensitive refers to "LookUp Colours".
0
 
Rgonzo1971Commented:
runned the macro and this is the result where is the problem exactly
BI-Depaartmental-Reports-9v1.xlsm
0
 
flemingg62Author Commented:
When I run the code only half the charts are updated. If I switch the colour to use the grey colour in Column C of Lookup Colour
 (       .Points(Idx).Format.Fill.ForeColor.RGB = Sheets("LookUp Colours").Range("A1").Offset(res - 1, 2).Interior.Color)
So all charts that change colour are Grey. You will see that not all charts are updated. i.e. Tab W3 won't update but tab W2 will
0
 
Rgonzo1971Commented:
then try
Sub ColorChartColumnsbyCellColor()
Dim aXVal()
For Each sh In Worksheets
Debug.Print sh.Name
    For Each chObj In sh.ChartObjects
        For Each SerCol In chObj.Chart.SeriesCollection
            With SerCol
                res = 0
                On Error Resume Next ' find corresponding SerieName
                res = WorksheetFunction.Match(SerCol.Name, Range(Sheets("LookUp Colours").Range("A1"), Sheets("LookUp Colours").Range("A" & Rows.Count).End(xlUp)), 0)
                On Error GoTo 0
                If res <> 0 Then ' if found$
                    SerCol.Format.Fill.ForeColor.RGB = Sheets("LookUp Colours").Range("A1").Offset(res - 1, 1).Interior.Color
                Else
                    aXVal = .XValues
                    For Idx = 1 To UBound(aXVal)
                        res = 0
                        On Error Resume Next ' find corresponding XValue
                        res = WorksheetFunction.Match(aXVal(Idx), Range(Sheets("LookUp Colours").Range("A1"), Sheets("LookUp Colours").Range("A" & Rows.Count).End(xlUp)), 0)
                        On Error GoTo 0
                        If res <> 0 Then ' if found
                            .Points(Idx).Format.Fill.ForeColor.RGB = Sheets("LookUp Colours").Range("A1").Offset(res - 1, 1).Interior.Color
                        End If
                    Next
                End If
            End With
        Next
    Next
Next

End Sub

Open in new window

1
 
flemingg62Author Commented:
Brilliant works a treat
0
 
frankhelkCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: Rgonzo1971 (https:#a41730640)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

frankhelk
Experts-Exchange Cleanup Volunteer
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!

  • 7
  • 7
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now