Column Chart Colours Based on Horizontal Series Point Name

DanMee

New Member
Joined
May 3, 2012
Messages
43
I've searched for this, and can't find the right coding to get this to work!! Here is what I have:

Code:
ChartCol.Select        Charts.Add.NAME = "Option Average Scores"
        ActiveChart.ChartType = xlColumnClustered
        ActiveChart.Location Where:=xlLocationAsObject, NAME:="Visuals"
        Sheets("Visuals").Activate
        ActiveChart.Parent.Height = 250
        ActiveChart.Parent.Width = 400
        ActiveChart.Parent.Left = 830
        ActiveChart.Parent.Top = 585
        With ActiveChart.Axes(xlValue)
            .MaximumScale = 100
            .MinimumScale = 20
        End With
        ActiveChart.SeriesCollection(1).Points(1).Select
        If ActiveChart.SeriesCollection(1).Points(1).DataLabel.Text = "OPTION1" Then
        Else
            If ActiveChart.SeriesCollection(1).Points(1).DataLabel.Text = "OPTION2" Then
                With Selection.Format.Fill
                    .ForeColor.RGB = RGB(255, 0, 0)
                    .Solid
                End With
            Else
                With Selection.Format.Fill
                    .ForeColor.RGB = RGB(255, 102, 204)
                    .Solid
                End With
            End If
        End If
        ActiveChart.SeriesCollection(1).Points(2).Select
        If ActiveChart.SeriesCollection(1).Points(2).DataLabel.Text = "OPTION1" Then
        Else
            If ActiveChart.SeriesCollection(1).Points(2).DataLabel.Text = "OPTION2" Then
                With Selection.Format.Fill
                    .ForeColor.RGB = RGB(255, 0, 0)
                    .Solid
                End With
            Else
                With Selection.Format.Fill
                    .ForeColor.RGB = RGB(255, 102, 204)
                    .Solid
                End With
            End If
        End If
        ActiveChart.SeriesCollection(1).Points(3).Select
        If ActiveChart.SeriesCollection(1).Points(3).DataLabel.Text = "OPTION1" Then
        Else
            If ActiveChart.SeriesCollection(1).Points(3).DataLabel.Text = "OPTION2" Then
                With Selection.Format.Fill
                    .ForeColor.RGB = RGB(255, 0, 0)
                    .Solid
                End With
            Else
                With Selection.Format.Fill
                    .ForeColor.RGB = RGB(255, 102, 204)
                    .Solid
                End With
            End If
        End If
        ActiveChart.HasLegend = False
    End If

The error I get is in relation to the
Code:
If ActiveChart.SeriesCollection(1).Points(1).DataLabel.Text =
part

Method DataLabel of object Point failed.

Now i have since learnt that DataLabel isnt what i want. However I can't find the syntax for what I do want.

The Series will always have at least one of 3 options in it. Each with a specific colour attached. So my question is, How do I get the code to check the names on the horizontal axis and return the right result to the IF after the graph has been created?
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try...

Code:
    Dim theChart As Chart
    Dim XValues As Variant
    Dim i As Long
    
    Set theChart = ActiveChart
    
    XValues = theChart.SeriesCollection(1).XValues
    
    For i = 1 To UBound(XValues)
        If XValues(i) = "OPTION1" Then
            'Do nothing
        Else
            If XValues(i) = "OPTION2" Then
                With theChart.SeriesCollection(1).Points(i)
                    With .Format.Fill
                        .ForeColor.RGB = RGB(255, 0, 0)
                        .Solid
                    End With
                End With
            Else
                With theChart.SeriesCollection(1).Points(i)
                    With .Format.Fill
                        .ForeColor.RGB = RGB(255, 102, 204)
                        .Solid
                    End With
                End With
            End If
        End If
    Next i

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

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