VBA Chart Matches Cell color, but Legend Won't Follow

alexb523

Board Regular
Joined
Dec 12, 2013
Messages
115
Hello,

I am trying to change all my charts to be the same color as the assigned cell and then catch and conditional formatting and change that specific bar to be that color.
I have been successful so far, as there are many similar codes out there to the one i used.

Below is the code. But how can i get the legend to also change colors?

Code:
Sub CellColorsToChart()


Dim oChart As ChartObject
Dim MySeries As Series
Dim FormulaSplit As Variant
Dim SourceRange As Range
Dim SourceRangeColor As Long
Dim NumberofDataPoints As Long
Dim iPoint As Long
Dim ws As Worksheet


'activate each worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Activate


'loop through all charts in the active sheet
For Each oChart In ActiveSheet.ChartObjects
    'loop through all series in the target chart
    For Each MySeries In oChart.Chart.SeriesCollection
       NumberofDataPoints = MySeries.Points.Count
    For iPoint = 1 To NumberofDataPoints
           'get Source Data Range for the target series
         FormulaSplit = Split(MySeries.Formula, ",")
         
           'capture the first cell in the source range then trap the color
        Set SourceRange = Range(FormulaSplit(2)).Item(1)
        SourceRangeColor = SourceRange.Interior.Color
            'capture the conditional cell in the source range then trap the color
            Set SourceRange = Range(FormulaSplit(2)).Item(iPoint)
            SourceRangeColor = SourceRange.DisplayFormat.Interior.Color
   
           On Error Resume Next
         
           'coloring
         MySeries.Points(iPoint).MarkerBackgroundColor = SourceRangeColor
           MySeries.Points(iPoint).MarkerForegroundColor = SourceRangeColor
           MySeries.Points(iPoint).Format.Line.ForeColor.RGB = SourceRangeColor
           MySeries.Points(iPoint).Format.Line.BackColor.RGB = SourceRangeColor
           MySeries.Points(iPoint).Format.Fill.ForeColor.RGB = SourceRangeColor
       
        Next
    Next MySeries
Next oChart
Next ws


End Sub

Thanks!
alexb523
 
Hi Domenic,
I see what you are saying. Right now, it is looking through each one of the cells and setting the bar to that cell color.

If that is that case, is it possible to have the legend color, or the base color, be the cell with the name of series?

Thanks,
Alex
 
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try...

Code:
[COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit[/COLOR]

[COLOR=darkblue]Sub[/COLOR] CellColorsToChart()

    [COLOR=darkblue]Dim[/COLOR] Wks [COLOR=darkblue]As[/COLOR] Worksheet
    [COLOR=darkblue]Dim[/COLOR] oChartObj [COLOR=darkblue]As[/COLOR] ChartObject
    [COLOR=darkblue]Dim[/COLOR] oSeries [COLOR=darkblue]As[/COLOR] Series
    [COLOR=darkblue]Dim[/COLOR] sFormula [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] sSeriesName [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] sSeriesValues [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] rSeriesName [COLOR=darkblue]As[/COLOR] Range
    [COLOR=darkblue]Dim[/COLOR] rSeriesValues [COLOR=darkblue]As[/COLOR] Range
    [COLOR=darkblue]Dim[/COLOR] SourceColor [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] PntIndx [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    
    [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] Wks [COLOR=darkblue]In[/COLOR] Worksheets
        [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] oChartObj [COLOR=darkblue]In[/COLOR] Wks.ChartObjects
            [COLOR=darkblue]With[/COLOR] oChartObj.Chart
                [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] oSeries [COLOR=darkblue]In[/COLOR] .SeriesCollection
                    sFormula = oSeries.Formula
                    sSeriesName = Split(sFormula, ",")(0)
                    sSeriesName = Mid(sSeriesName, InStrRev(sSeriesName, "(") + 1)
                    sSeriesValues = Split(sFormula, ",")(2)
                    [COLOR=darkblue]Set[/COLOR] rSeriesName = Range(sSeriesName)
                    [COLOR=darkblue]Set[/COLOR] rSeriesValues = Range(sSeriesValues)
                    oSeries.Format.Line.[COLOR=darkblue]For[/COLOR]eColor.RGB = rSeriesName.DisplayFormat.Interior.Color
                    [COLOR=darkblue]Select[/COLOR] [COLOR=darkblue]Case[/COLOR] oSeries.ChartType
                        [COLOR=darkblue]Case[/COLOR] xlLine, xlLineMarkers, xlLineMarkersStacked, xlLineMarkersStacked100, xlLineStacked, xlLineStacked100
                            For PntIndx = 1 [COLOR=darkblue]To[/COLOR] oSeries.Points.Count
                                SourceColor = rSeriesValues.Cells(PntIndx).DisplayFormat.Interior.Color
                                [COLOR=darkblue]With[/COLOR] oSeries.Points(PntIndx)
                                    .MarkerBackgroundColor = SourceColor
                                    .MarkerForegroundColor = SourceColor
                                [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
                            [COLOR=darkblue]Next[/COLOR] PntIndx
                    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Select[/COLOR]
                [COLOR=darkblue]Next[/COLOR] oSeries
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
        [COLOR=darkblue]Next[/COLOR] oChartObj
    [COLOR=darkblue]Next[/COLOR] Wks
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
Upvote 0
What type of error is it giving you?

When the error occurs, and you hit debug, and it takes to you to that line, move your cursor over the variables sFormula and sSeriesName. What values are assigned to those variables?
 
Upvote 0
Ahh...ic.

Hovering over gave lead me to a chart that did not have a name series. I fixed that, and the code ran, however, it is only working on my line graphs.

Again, thanks for all your help on this.
I will admit, at this point I am totally lost with your code as it is well beyond my levels. I hope to get better. Is there any way you could comment out a few actions so can follow along. I will also try and add a few steps after I finish this coloring challenge and i think knowing at least somewhat of what is happening will help.
I am hoping to copy paste the graphs to one tab (keeping on their original tab and the graphs tab), and then hopefully put them all onto a PowerPoint.

Thanks!
 
Upvote 0
Yeah, I had noticed that I missed a line of code to fill the series for charts other than line charts. I was going to address it after we finished addressing the error. So I'll add that line of code and I'll comment the code. I'll do that shortly.
 
Upvote 0
Here you go...

Code:
[color=green]'Force the explicit delcaration of variables[/color]
[color=darkblue]Option[/color] [color=darkblue]Explicit[/color]

[color=darkblue]Sub[/color] CellColorsToChart()

    [color=green]'Declare the variables[/color]
    [color=darkblue]Dim[/color] Wks [color=darkblue]As[/color] Worksheet
    [color=darkblue]Dim[/color] oChartObj [color=darkblue]As[/color] ChartObject
    [color=darkblue]Dim[/color] oSeries [color=darkblue]As[/color] Series
    [color=darkblue]Dim[/color] sFormula [color=darkblue]As[/color] [color=darkblue]String[/color]
    [color=darkblue]Dim[/color] sSeriesName [color=darkblue]As[/color] [color=darkblue]String[/color]
    [color=darkblue]Dim[/color] sSeriesValues [color=darkblue]As[/color] [color=darkblue]String[/color]
    [color=darkblue]Dim[/color] rSeriesName [color=darkblue]As[/color] Range
    [color=darkblue]Dim[/color] rSeriesValues [color=darkblue]As[/color] Range
    [color=darkblue]Dim[/color] SourceColor [color=darkblue]As[/color] [color=darkblue]Long[/color]
    [color=darkblue]Dim[/color] PntIndx [color=darkblue]As[/color] [color=darkblue]Long[/color]
    
    [color=green]'Loop through each worksheet in the active workbook[/color]
    [color=darkblue]For[/color] [color=darkblue]Each[/color] Wks [color=darkblue]In[/color] Worksheets
        [color=green]'Loop through each chart object in the current worksheet[/color]
        [color=darkblue]For[/color] [color=darkblue]Each[/color] oChartObj [color=darkblue]In[/color] Wks.ChartObjects
            [color=green]'Format the current chart[/color]
            [color=darkblue]With[/color] oChartObj.Chart
                [color=green]'Loop through each series within the current chart[/color]
                [color=darkblue]For[/color] [color=darkblue]Each[/color] oSeries [color=darkblue]In[/color] .SeriesCollection
                    [color=green]'Get the series formula[/color]
                    sFormula = oSeries.Formula
                    [color=green]'Get the string used to reference the series name[/color]
                    sSeriesName = Split(sFormula, ",")(0)
                    sSeriesName = Mid(sSeriesName, InStrRev(sSeriesName, "(") + 1)
                    [color=green]'Get the string used to reference the series values[/color]
                    sSeriesValues = Split(sFormula, ",")(2)
                    [color=green]'Assign the cell containing the series name to an object variable[/color]
                    [color=darkblue]Set[/color] rSeriesName = Range(sSeriesName)
                    [color=green]'Assign the range containing the series values to an object variable[/color]
                    [color=darkblue]Set[/color] rSeriesValues = Range(sSeriesValues)
                    [color=green]'Set the color for the series based on the color of the cell containing the series name[/color]
                    oSeries.Format.Fill.ForeColor.RGB = rSeriesName.DisplayFormat.Interior.Color
                    oSeries.[color=darkblue]For[/color]mat.Line.ForeColor.RGB = rSeriesName.DisplayFormat.Interior.Color
                    [color=green]'Add and/or set the marker color for any type of line chart[/color]
                    [color=darkblue]Select[/color] [color=darkblue]Case[/color] oSeries.ChartType
                        [color=darkblue]Case[/color] xlLine, xlLineMarkers, xlLineMarkersStacked, xlLineMarkersStacked100, xlLineStacked, xlLineStacked100
                            [color=green]'Loop through each point within the current series[/color]
                            For PntIndx = 1 [color=darkblue]To[/color] oSeries.Points.Count
                                [color=green]'Get the color from the corresponding cell in the source range[/color]
                                SourceColor = rSeriesValues.Cells(PntIndx).DisplayFormat.Interior.Color
                                [color=green]'Set the marker color for the current point[/color]
                                [color=darkblue]With[/color] oSeries.Points(PntIndx)
                                    .MarkerBackgroundColor = SourceColor
                                    .MarkerForegroundColor = SourceColor
                                [color=darkblue]End[/color] [color=darkblue]With[/color]
                            [color=darkblue]Next[/color] PntIndx
                    [color=darkblue]End[/color] [color=darkblue]Select[/color]
                [color=darkblue]Next[/color] oSeries
            [color=darkblue]End[/color] [color=darkblue]With[/color]
        [color=darkblue]Next[/color] oChartObj
    [color=darkblue]Next[/color] Wks
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0
Hi Domenic,

Thanks for your help. Fore some reason it did not pick put the conditional formatting. I have basically merged my original code from the comments in on this message board: Automatically Set Chart Series Colors to Match Source Cell Colors – Bacon Bits
and the code you have provided me with to change the legend.

It is probably not the cleanest code as I am looping through all the charts twice, but it gets the job done for now. I got rid of some variables but there is probably more I could do away with.

The code below changes the legend color by the color of the series name cell. It then goes through each cell to change the color of the series points according to the cell that corresponds with that graphs data point. It accounts for conditional formatting.

Code:
'Force the explicit delcaration of variables
Option Explicit


Sub CellColorsToChart()


'Code to Change Legend Color
    'Declare the variables
    Dim Wks As Worksheet
    Dim oChartObj As ChartObject
    Dim oSeries As Series
    Dim sFormula As String
    Dim sSeriesName As String
    Dim sSeriesValues As String
    Dim rSeriesName As Range
    Dim rSeriesValues As Range
    Dim SourceColor As Long
    Dim PntIndx As Long
    
    'Varialbles for Conditional Formatting Code
    Dim wb As Workbook
    Dim KPI As Worksheet


    Dim FormulaSplit As Variant
    Dim SourceRange As Range
    Dim SourceRangeColor As Long
    Dim NumberofDataPoints As Long
    Dim iPoint As Long


    Set wb = ThisWorkbook
    Set KPI = wb.Sheets("KPI")
    
    'Loop through each worksheet in the active workbook
    For Each Wks In Worksheets
        'Loop through each chart object in the current worksheet
        For Each oChartObj In Wks.ChartObjects
            'Format the current chart
            With oChartObj.Chart
                'Loop through each series within the current chart
                For Each oSeries In .SeriesCollection
                    'Get the series formula
                    sFormula = oSeries.Formula
                    'Get the string used to reference the series name
                    sSeriesName = Split(sFormula, ",")(0)
                    sSeriesName = Mid(sSeriesName, InStrRev(sSeriesName, "(") + 1)
                    'Get the string used to reference the series values
                    sSeriesValues = Split(sFormula, ",")(2)
                    'Assign the cell containing the series name to an object variable
                    Set rSeriesName = Range(sSeriesName)
                    'Assign the range containing the series values to an object variable
                    Set rSeriesValues = Range(sSeriesValues)
                    'Set the color for the series based on the color of the cell containing the series name
                    oSeries.Format.Fill.ForeColor.RGB = rSeriesName.DisplayFormat.Interior.Color
                    oSeries.Format.Line.ForeColor.RGB = rSeriesName.DisplayFormat.Interior.Color
                    'Add and/or set the marker color for any type of line chart
                    Select Case oSeries.ChartType
                        Case xlLine, xlLineMarkers, xlLineMarkersStacked, xlLineMarkersStacked100, xlLineStacked, xlLineStacked100
                            'Loop through each point within the current series
                            For PntIndx = 1 To oSeries.Points.Count
                                'Get the color from the corresponding cell in the source range
                                SourceColor = rSeriesValues.Cells(PntIndx).DisplayFormat.Interior.Color
                                'Set the marker color for the current point
                                With oSeries.Points(PntIndx)
                                    .MarkerBackgroundColor = SourceColor
                                    .MarkerForegroundColor = SourceColor
                                End With
                            Next PntIndx
                    End Select
                Next oSeries
            End With
        Next oChartObj
    Next Wks


'Code to Account for Conitional Formatting
    'activate each worksheet
    For Each Wks In ThisWorkbook.Worksheets
    Wks.Activate


    'loop through all charts in the active sheet
    For Each oChartObj In ActiveSheet.ChartObjects
        'loop through all series in the target chart
        For Each oSeries In oChartObj.Chart.SeriesCollection
            NumberofDataPoints = oSeries.Points.Count
        For iPoint = 1 To NumberofDataPoints
        
           'get Source Data Range for the target series
            FormulaSplit = Split(oSeries.Formula, ",")
         
           'capture the first cell in the source range then trap the color
            Set SourceRange = Range(FormulaSplit(2)).Item(1)
            SourceRangeColor = SourceRange.Interior.Color
            'capture the conditional cell in the source range then trap the color
            Set SourceRange = Range(FormulaSplit(2)).Item(iPoint)
            SourceRangeColor = SourceRange.DisplayFormat.Interior.Color
   
        On Error Resume Next
         
           'coloring
           oSeries.Points(iPoint).MarkerBackgroundColor = SourceRangeColor
           oSeries.Points(iPoint).MarkerForegroundColor = SourceRangeColor
           oSeries.Points(iPoint).Format.Line.ForeColor.RGB = SourceRangeColor
           oSeries.Points(iPoint).Format.Line.BackColor.RGB = SourceRangeColor
           oSeries.Points(iPoint).Format.Fill.ForeColor.RGB = SourceRangeColor


        Next
        
        Next oSeries
    Next oChartObj
    Next Wks
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top